ContentTypeMatcher   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 6
c 4
b 0
f 0
lcom 1
cbo 1
dl 0
loc 40
ccs 13
cts 13
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A matches() 0 19 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