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

GuzzleRequestHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 7
c 5
b 2
f 0
lcom 1
cbo 4
dl 0
loc 64
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setHandlerStack() 0 6 1
A getHandlerStack() 0 8 2
A setClient() 0 6 1
A getClient() 0 8 2
A request() 0 5 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