Passed
Push — master ( 1b6940...c5a534 )
by Dominik
01:45
created

AcceptLanguageNegotiator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\ApiHttp\Negotiation;
6
7
use Psr\Http\Message\ServerRequestInterface as Request;
8
9
/**
10
 * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
11
 */
12
final class AcceptLanguageNegotiator implements NegotiatorInterface
13
{
14
    /**
15
     * @var array
16
     */
17
    private $supportedLocales;
18
19
    /**
20
     * @param array $supportedLocales
21
     */
22 6
    public function __construct(array $supportedLocales)
23
    {
24 6
        $this->supportedLocales = $supportedLocales;
25 6
    }
26
27
    /**
28
     * @param Request $request
29
     *
30
     * @return NegotiatedValue|null
31
     */
32 6
    public function negotiate(Request $request)
33
    {
34 6
        if ([] === $this->supportedLocales) {
35 1
            return null;
36
        }
37
38 5
        if (!$request->hasHeader('Accept-Language')) {
39 1
            return null;
40
        }
41
42 4
        $aggregatedValues = $this->aggregatedValues($request->getHeaderLine('Accept-Language'));
43
44 4
        return $this->compareAgainstSupportedLocales($aggregatedValues);
45
    }
46
47
    /**
48
     * @param string $header
49
     *
50
     * @return array
51
     */
52 4 View Code Duplication
    private function aggregatedValues(string $header): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54 4
        $values = [];
55 4
        foreach (explode(',', $header) as $headerValue) {
56 4
            $headerValueParts = explode(';', $headerValue);
57 4
            $locale = trim(array_shift($headerValueParts));
58 4
            $attributes = [];
59 4
            foreach ($headerValueParts as $attribute) {
60 4
                list($attributeKey, $attributeValue) = explode('=', $attribute);
61 4
                $attributes[trim($attributeKey)] = trim($attributeValue);
62
            }
63
64 4
            if (!isset($attributes['q'])) {
65 3
                $attributes['q'] = '1.0';
66
            }
67
68 4
            $values[$locale] = $attributes;
69
        }
70
71 4
        uasort($values, function (array $a, array $b) {
72 4
            return $b['q'] <=> $a['q'];
73 4
        });
74
75 4
        return $values;
76
    }
77
78
    /**
79
     * @param array $aggregatedValues
80
     *
81
     * @return NegotiatedValue|null
82
     */
83 4
    private function compareAgainstSupportedLocales(array $aggregatedValues)
84
    {
85 4
        foreach ($aggregatedValues as $locale => $attributes) {
86 4
            if ('*' === $locale) {
87 1
                return new NegotiatedValue(reset($this->supportedLocales), $attributes);
88
            }
89
90 4
            if (in_array($locale, $this->supportedLocales, true)) {
91 4
                return new NegotiatedValue($locale, $attributes);
92
            }
93
        }
94
95 1
        return null;
96
    }
97
}
98