RequestStackLocaleProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 4
A providePreferredLocale() 0 9 2
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Copyright 2015 SURFnet bv
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
20
21
namespace Surfnet\StepupSelfService\SelfServiceBundle\Locale;
22
23
use Surfnet\StepupSelfService\SelfServiceBundle\Exception\InvalidArgumentException;
24
use Surfnet\StepupSelfService\SelfServiceBundle\Locale\PreferredLocaleProvider;
25
use Symfony\Component\HttpFoundation\RequestStack;
26
27
/**
28
 * Fetches the client's preferred locale from the request (specifically from the Accept header), falling back to a
29
 * default locale if no preferred locale can be determined.
30
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
31
final class RequestStackLocaleProvider implements PreferredLocaleProvider
32
{
33
    private readonly string $defaultLocale;
0 ignored issues
show
Coding Style introduced by
Private member variable "defaultLocale" must be prefixed with an underscore
Loading history...
34
35
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
36
     * @var string[]
37
     */
38
    private $supportedLocales;
0 ignored issues
show
Coding Style introduced by
Private member variable "supportedLocales" must be prefixed with an underscore
Loading history...
39
40
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $requestStack should have a doc-comment as per coding-style.
Loading history...
41
     * @param string $defaultLocale
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Doc comment for parameter $defaultLocale does not match actual variable name $requestStack
Loading history...
42
     * @param string[] $supportedLocales
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Doc comment for parameter $supportedLocales does not match actual variable name $defaultLocale
Loading history...
43
     */
44
    public function __construct(
45
        private readonly RequestStack $requestStack,
46
        $defaultLocale,
47
        $supportedLocales
48
    ) {
49
        if (!is_string($defaultLocale)) {
0 ignored issues
show
introduced by
The condition is_string($defaultLocale) is always true.
Loading history...
50
            throw InvalidArgumentException::invalidType('string', 'defaultLocale', $defaultLocale);
51
        }
52
53
        foreach ($supportedLocales as $key => $supportedLocale) {
54
            if (!is_string($supportedLocale)) {
55
                $parameterName = sprintf('supportedLocales[%s]', $key);
56
                throw InvalidArgumentException::invalidType('string', $parameterName, $supportedLocale);
57
            }
58
        }
59
        $this->defaultLocale = $defaultLocale;
0 ignored issues
show
Bug introduced by
The property defaultLocale is declared read-only in Surfnet\StepupSelfServic...uestStackLocaleProvider.
Loading history...
60
        $this->supportedLocales = $supportedLocales;
61
    }
62
63
    public function providePreferredLocale(): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function providePreferredLocale()
Loading history...
64
    {
65
        $preferredLocale = $this->requestStack->getCurrentRequest()->getPreferredLanguage($this->supportedLocales);
66
67
        if (!$preferredLocale) {
68
            return $this->defaultLocale;
69
        }
70
71
        return $preferredLocale;
72
    }
73
}
74