Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
SetLocale | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
process | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Engelsystem\Middleware; |
6 | |
7 | use Engelsystem\Helpers\Authenticator; |
8 | use Engelsystem\Helpers\Translation\Translator; |
9 | use Psr\Http\Message\ResponseInterface; |
10 | use Psr\Http\Message\ServerRequestInterface; |
11 | use Psr\Http\Server\MiddlewareInterface; |
12 | use Psr\Http\Server\RequestHandlerInterface; |
13 | use Symfony\Component\HttpFoundation\Session\Session; |
14 | |
15 | class SetLocale implements MiddlewareInterface |
16 | { |
17 | public function __construct( |
18 | protected Translator $translator, |
19 | protected Session $session, |
20 | protected Authenticator $auth |
21 | ) { |
22 | } |
23 | |
24 | /** |
25 | * Process an incoming server request and setting the locale if required |
26 | */ |
27 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
28 | { |
29 | $query = $request->getQueryParams(); |
30 | if (isset($query['set-locale']) && $this->translator->hasLocale($query['set-locale'])) { |
31 | $locale = $query['set-locale']; |
32 | |
33 | $this->translator->setLocale($locale); |
34 | $this->session->set('locale', $locale); |
35 | |
36 | $user = $this->auth->user(); |
37 | if ($user) { |
38 | $user->settings->language = $locale; |
39 | $user->settings->save(); |
40 | } |
41 | } |
42 | |
43 | return $handler->handle($request); |
44 | } |
45 | } |