Failed Conditions
Pull Request — master (#26)
by Matthijs
02:38 queued 22s
created

GuzzleRequestHandler::setClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace VDB\Spider\RequestHandler;
3
4
use GuzzleHttp\Client;
5
use GuzzleHttp\HandlerStack;
6
use GuzzleHttp\Message\Request;
7
use GuzzleHttp\Message\RequestInterface;
8
use VDB\Spider\RequestHandler\RequestHandlerInterface;
9
use VDB\Spider\Resource;
10
use VDB\Spider\Uri\DiscoveredUri;
11
12
/**
13
 * @author Matthijs van den Bos <[email protected]>
14
 * @copyright 2013 Matthijs van den Bos
15
 */
16
class GuzzleRequestHandler implements RequestHandlerInterface
17
{
18
    /** @var Client */
19
    private $client;
20
21
    /** @var HandlerStack */
22
    private $handlerStack;
23
24
    /**
25
     * @param HandlerStack $handlerStack
26
     * @return RequestHandlerInterface
27
     */
28
    public function setHandlerStack(HandlerStack $handlerStack)
29
    {
30
        $this->handlerStack = $handlerStack;
31
32
        return $this;
33
    }
34
35
    /**
36
     * @return HandlerStack
37
     */
38
    public function getHandlerStack()
39
    {
40
        if (!$this->handlerStack) {
41
            $this->handlerStack = HandlerStack::create();
42
        }
43
44
        return $this->handlerStack;
45
    }
46
47
    /**
48
     * @param Client $client
49
     * @return RequestHandlerInterface
50
     */
51
    public function setClient(Client $client)
52
    {
53
        $this->client = $client;
54
55
        return $this;
56
    }
57
58
    /**
59
     * @return Client
60
     */
61
    public function getClient()
62
    {
63
        if (!$this->client) {
64
            $this->client = new Client(['config' => $this->getHandlerStack()]);
65
        }
66
67
        return $this->client;
68
    }
69
70
    /**
71
     * @param DiscoveredUri $uri
72
     * @return Resource
73
     */
74
    public function request(DiscoveredUri $uri)
75
    {
76
        $response = $this->getClient()->get($uri->toString());
77
        return new Resource($uri, $response);
78
    }
79
}
80