Client::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace TomPHP\HalClient;
4
5
use Assert\Assertion;
6
use TomPHP\HalClient\Exception\UnknownContentTypeException;
7
use TomPHP\HalClient\HttpClient\GuzzleHttpClient;
8
use TomPHP\HalClient\Processor\HalJsonProcessor;
9
10
final class Client implements ResourceFetcher
11
{
12
    /** @var HttpClient */
13
    private $httpClient;
14
15
    /** @var Processor[] */
16
    private $processors;
17
18
    /** @return self */
19
    public static function create()
20
    {
21
        return new self(new GuzzleHttpClient(), [new HalJsonProcessor()]);
22
    }
23
24
    /** @param Processor[] $processors */
25
    public function __construct(HttpClient $httpClient, array $processors)
26
    {
27
        Assertion::allIsInstanceOf($processors, Processor::class);
28
29
        $this->httpClient = $httpClient;
30
31
        foreach ($processors as $processor) {
32
            $this->processors[$processor->getContentType()] = $processor;
33
        }
34
    }
35
36
    public function get($url)
37
    {
38
        $response = $this->httpClient->get($url);
39
40
        $contentTypes = $response->getHeader('content-type');
41
        $contentType = array_shift($contentTypes);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
42
43
        if (!array_key_exists($contentType, $this->processors)) {
44
            throw new UnknownContentTypeException($contentType);
45
        }
46
47
        $processor = $this->processors[$contentType];
48
49
        return $processor->process($response, $this);
50
    }
51
}
52