ContentTypeMatcher::matches()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.2
cc 4
eloc 9
nc 5
nop 2
crap 4
1
<?php
2
3
namespace Alchemy\Rest\Request;
4
5
use Negotiation\Negotiator;
6
7
class ContentTypeMatcher
8
{
9
    /**
10
     * @var Negotiator
11
     */
12
    private $negotiator;
13
14
    /**
15
     * @param Negotiator|null $negotiator
16
     */
17 42
    public function __construct(Negotiator $negotiator = null)
18
    {
19 42
        $this->negotiator = $negotiator ?: new Negotiator();
20 42
    }
21
22
    /**
23
     * @param string $acceptHeader
24
     * @param string[] $acceptedContentTypes
25
     * @return bool
26
     */
27 40
    public function matches($acceptHeader, array $acceptedContentTypes)
28
    {
29 40
        if (empty($acceptHeader)) {
30 4
            return false;
31
        }
32
33
        // This is in case the submitted header is not standards compliant
34 36
        if (strpos($acceptHeader, ';')) {
35 4
            list($acceptHeader, ) = explode(';', $acceptHeader);
36 4
        }
37
38 36
        $format = $this->negotiator->getBest($acceptHeader, $acceptedContentTypes);
39
40 36
        if ($format) {
41 32
            return true;
42
        }
43
44 4
        return false;
45
    }
46
}
47