Completed
Push — master ( 7d72c0...456733 )
by Nils
02:48
created

Retriever   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 8
c 4
b 1
f 1
lcom 1
cbo 4
dl 0
loc 66
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getComingFrom() 0 4 1
A init() 0 5 1
A setHttpClient() 0 5 1
A getSystems() 0 8 1
A prepareUrl() 0 7 1
A next() 0 13 2
A getOriginUri() 0 4 1
1
<?php
2
3
namespace whm\Smoke\Extensions\SmokeResponseRetriever\Retriever\Koalamon;
4
5
use Ivory\HttpAdapter\HttpAdapterInterface;
6
use Ivory\HttpAdapter\Message\Request;
7
use Psr\Http\Message\UriInterface;
8
use whm\Html\Uri;
9
use whm\Smoke\Extensions\SmokeResponseRetriever\Retriever\Retriever as SmokeRetriever;
10
11
class Retriever implements SmokeRetriever
12
{
13
    private $apiKey;
14
    private $systems;
15
    private $project;
16
17
    /**
18
     * @var HttpAdapterInterface
19
     */
20
    private $client;
21
22
    const ENDPOINT_SYSTEMS = 'http://www.koalamon.com/rest/#project#/systems/?api_key=#api_key#';
23
24
    public function init($apiKey, $project)
25
    {
26
        $this->apiKey = $apiKey;
27
        $this->project = $project;
28
    }
29
30
    public function setHttpClient(HttpAdapterInterface $httpClient)
31
    {
32
        $this->client = $httpClient;
33
        $this->systems = $this->getSystems($httpClient);
34
    }
35
36
    public function getSystems(HttpAdapterInterface $httpClient)
37
    {
38
        $url = $this->prepareUrl(self::ENDPOINT_SYSTEMS);
39
40
        $systems = $httpClient->get(new Uri($url));
41
42
        return json_decode($systems->getBody(), true);
43
    }
44
45
    private function prepareUrl($url)
46
    {
47
        $preparedUrl = str_replace('#project#', $this->project, $url);
48
        $preparedUrl = str_replace('#api_key#', $this->apiKey, $preparedUrl);
49
50
        return $preparedUrl;
51
    }
52
53
    public function next()
54
    {
55
        if (empty($this->systems)) {
56
            return false;
57
        }
58
59
        $system = array_pop($this->systems);
60
61
        $request = new Request(new Uri($system['url']), 'GET', 'php://memory', ['Accept-Encoding' => 'gzip']);
62
        $responses = $this->client->sendRequests(array($request));
63
64
        return $responses[0];
65
    }
66
67
    public function getComingFrom(UriInterface $uri)
68
    {
69
        return new Uri('http://www.koalamon.com');
70
    }
71
72
    public function getOriginUri(UriInterface $uri)
73
    {
74
        return $uri;
75
    }
76
}
77