Completed
Push — master ( 36db99...98d8fe )
by Dominik
03:00
created

AcceptNegotiator::negotiate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2.032
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\ApiHttp\Negotiation;
6
7
/**
8
 * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
9
 */
10
final class AcceptNegotiator implements NegotiatorInterface
11
{
12
    /**
13
     * @param string $header
14
     * @param array  $supported
15
     *
16
     * @return NegotiatedValue|null
17
     */
18 6
    public function negotiate(string $header, array $supported)
19
    {
20 6
        if ([] === $supported) {
21
            return null;
22
        }
23
24 6
        $aggregatedValues = $this->aggregatedValues($header);
25
26 6
        return $this->compareAgainstSupportedTypes($aggregatedValues, $supported);
27
    }
28
29
    /**
30
     * @param string $header
31
     *
32
     * @return array
33
     */
34 6
    private function aggregatedValues(string $header): array
35
    {
36 6
        $values = [];
37 6
        foreach (explode(',', $header) as $headerValue) {
38 6
            $headerValueParts = explode(';', $headerValue);
39 6
            $mimeType = trim(array_shift($headerValueParts));
40 6
            $attributes = [];
41 6 View Code Duplication
            foreach ($headerValueParts as $attribute) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
42 5
                list($attributeKey, $attributeValue) = explode('=', $attribute);
43 5
                $attributes[trim($attributeKey)] = trim($attributeValue);
44
            }
45
46 6
            if (!isset($attributes['q'])) {
47 6
                $attributes['q'] = '1.0';
48
            }
49
50 6
            $values[$mimeType] = $attributes;
51
        }
52
53 6
        uasort($values, function (array $a, array $b) {
54 6
            return $b['q'] <=> $a['q'];
55 6
        });
56
57 6
        return $values;
58
    }
59
60
    /**
61
     * @param array $aggregatedValues
62
     * @param array $supportedMimeTypes
63
     *
64
     * @return NegotiatedValue|null
65
     */
66 6
    private function compareAgainstSupportedTypes(array $aggregatedValues, array $supportedMimeTypes)
67
    {
68 6
        foreach ($aggregatedValues as $mimeType => $attributes) {
69 6
            if ('*/*' === $mimeType) {
70 1
                return new NegotiatedValue(reset($supportedMimeTypes), $attributes);
71
            }
72
73 6
            list($type, $subType) = explode('/', $mimeType);
74
75 6
            if ('*' === $type && '*' !== $subType) { // skip invalid value
76 1
                continue;
77
            }
78
79 5
            $subTypePattern = $subType !== '*' ? preg_quote($subType) : '.+';
80
81 5
            foreach ($supportedMimeTypes as $supportedMimeType) {
82 5
                if (1 === preg_match('/^'.preg_quote($type).'\/'.$subTypePattern.'$/', $supportedMimeType)) {
83 5
                    return new NegotiatedValue($supportedMimeType, $attributes);
84
                }
85
            }
86
        }
87
88 2
        return null;
89
    }
90
}
91