ResponseBuilder::__construct()   A
last analyzed

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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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
        }
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