Completed
Push — master ( e09e40...2564b7 )
by Nils
02:09
created

Retriever::setSessionContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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