ResponseBuilder   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95.65%

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 8
c 6
b 0
f 1
lcom 1
cbo 2
dl 0
loc 51
ccs 22
cts 23
cp 0.9565
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 8 1
A applyContentTypeHeader() 0 5 1
A setAvailableContentTypes() 0 4 1
A attemptSettingContentType() 0 15 4
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