Completed
Push — master ( e130e8...8fa866 )
by Łukasz
37:22 queued 13:48
created

UserLanguagePreferenceProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\MVC\Symfony\Locale;
10
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\RequestStack;
13
14
class UserLanguagePreferenceProvider implements UserLanguagePreferenceProviderInterface
15
{
16
    /**
17
     * @var \Symfony\Component\HttpFoundation\RequestStack
18
     */
19
    private $requestStack;
20
21
    /**
22
     * @var array
23
     */
24
    private $languageCodesMap;
25
26
    /**
27
     * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
28
     * @param array $languageCodesMap
29
     */
30
    public function __construct(RequestStack $requestStack, array $languageCodesMap)
31
    {
32
        $this->requestStack = $requestStack;
33
        $this->languageCodesMap = $languageCodesMap;
34
    }
35
36
    public function getPreferredLocales(Request $request = null): array
37
    {
38
        $request = $request ?? $this->requestStack->getCurrentRequest();
39
        $preferredLocales = $request->headers->get('Accept-Language') ?? '';
40
        $preferredLocales = array_reduce(
41
            explode(',', $preferredLocales),
42
            function (array $result, string $languageWithQuality) {
43
                [$language, $quality] = array_merge(explode(';q=', $languageWithQuality), [1]);
0 ignored issues
show
Bug introduced by
The variable $language does not exist. Did you mean $languageWithQuality?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $quality does not exist. Did you mean $languageWithQuality?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
44
                $result[$language] = (float) $quality;
0 ignored issues
show
Bug introduced by
The variable $language does not exist. Did you mean $languageWithQuality?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $quality does not exist. Did you mean $languageWithQuality?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
45
46
                return $result;
47
            },
48
            []
49
        );
50
        arsort($preferredLocales);
51
52
        return array_keys($preferredLocales);
53
    }
54
55
    public function getPreferredLanguages(): array
56
    {
57
        $languageCodes = [];
58
        foreach ($this->getPreferredLocales() as $locale) {
59
            $locale = strtolower($locale);
60
            if (isset($this->languageCodesMap[$locale])) {
61
                $languageCodes = array_merge($languageCodes, $this->languageCodesMap[$locale]);
62
            } elseif (preg_match('/^([a-z]{3})-([a-z]{2})$/', $locale, $matches)) {
63
                // if the given locale is already in the eZ format
64
                $languageCodes[] = strtolower($matches[1]) . '-' . strtoupper($matches[2]);
65
            }
66
        }
67
68
        return array_unique($languageCodes);
69
    }
70
}
71