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

Retriever::getOriginUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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