LocaleSelector::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Endpoint\Web\Middleware;
6
7
use Psr\Http\Message\ResponseInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ResponseInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Psr\Http\Message\ServerRequestInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ServerRequestInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Psr\Http\Server\MiddlewareInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Server\MiddlewareInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Psr\Http\Server\RequestHandlerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Server\RequestHandlerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Spiral\Translator\Translator;
0 ignored issues
show
Bug introduced by
The type Spiral\Translator\Translator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
/**
14
 * The middleware that sets the application locale based on the "Accept-Language" header.
15
 * List of available locales is taken from the translator.
16
 */
17
final class LocaleSelector implements MiddlewareInterface
18
{
19
    /** @var string[] */
20
    private array $availableLocales;
21
22
    public function __construct(
23
        private readonly Translator $translator,
24
    ) {
25
        $this->availableLocales = $this->translator->getCatalogueManager()->getLocales();
26
    }
27
28
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
29
    {
30
        $defaultLocale = $this->translator->getLocale();
31
32
        try {
33
            foreach ($this->fetchLocales($request) as $locale) {
34
                if ($locale !== '' && \in_array($locale, $this->availableLocales, true)) {
35
                    $this->translator->setLocale($locale);
36
                    break;
37
                }
38
            }
39
40
            return $handler->handle($request);
41
        } finally {
42
            // restore
43
            $this->translator->setLocale($defaultLocale);
44
        }
45
    }
46
47
    public function fetchLocales(ServerRequestInterface $request): \Generator
48
    {
49
        $header = $request->getHeaderLine('accept-language');
50
        foreach (\explode(',', $header) as $value) {
51
            $pos = \strpos($value, ';');
52
            if ($pos !== false) {
53
                yield \substr($value, 0, $pos);
54
            }
55
56
            yield $value;
57
        }
58
    }
59
}
60