Completed
Push — master ( 752264...caff68 )
by Mārtiņš
7s
created

ResponseBuilder::attemptSettingContentType()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 15
ccs 9
cts 10
cp 0.9
rs 9.2
cc 4
eloc 8
nc 4
nop 1
crap 4.016
1
<?php
2
3
namespace Fracture\Http;
4
5
class ResponseBuilder
6
{
7
8
    private $request;
9
10
    private $contentTypes = [];
11
12 3
    public function __construct($request)
13
    {
14 3
        $this->request = $request;
15 3
    }
16
17
18 3
    public function create()
19
    {
20 3
        $instance = new Response;
21
22 3
        $this->attemptSettingContentType($instance);
23
24 3
        return $instance;
25
    }
26
27
28 2
    private function attemptSettingContentType($instance)
29
    {
30 2
        $header = $this->request->getAcceptHeader();
31
32 2
        if ($header === null) {
33 1
            return;
34
        }
35
36 1
        foreach ($this->contentTypes as $candidate) {
37 1
            if ($header->contains($candidate)) {
38 1
                $this->applyContentTypeHeader($instance, $candidate);
39 1
                return;
40
            }
41 1
        }
42
    }
43
44
45 1
    private function applyContentTypeHeader($instance, $value)
46
    {
47 1
        $header = new Headers\ContentType($value);
48 1
        $instance->addHeader($header);
49 1
    }
50
51 2
    public function setAvailableContentTypes($contentTypes)
52
    {
53 2
        $this->contentTypes = $contentTypes;
54 2
    }
55
}
56