LegacyGuzzleReader   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 57
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A getContents() 0 4 1
A getContentsAsStream() 0 8 2
A buildRequest() 0 4 1
1
<?php
2
3
namespace Alchemy\Zippy\Resource\Reader\Guzzle;
4
5
use Alchemy\Zippy\Resource\Resource as ZippyResource;
6
use Alchemy\Zippy\Resource\ResourceReader;
7
use Guzzle\Http\Client;
8
use Guzzle\Http\ClientInterface;
9
use Guzzle\Http\EntityBodyInterface;
10
11
class LegacyGuzzleReader implements ResourceReader
12
{
13
    /**
14
     * @var ClientInterface
15
     */
16
    private $client;
17
18
    /**
19
     * @var \Alchemy\Zippy\Resource\Resource $resource
20
     */
21
    private $resource;
22
23
    /**
24
     * This is necessary to prevent the underlying PHP stream from being destroyed
25
     * @link https://github.com/guzzle/guzzle/issues/366#issuecomment-20295409
26
     * @var EntityBodyInterface|null
27
     */
28
    private $stream = null;
29
30
    /**
31
     * @param ZippyResource   $resource
32
     * @param ClientInterface $client
33
     */
34
    public function __construct(ZippyResource $resource, ClientInterface $client = null)
35
    {
36
        $this->client = $client ?: new Client();
37
        $this->resource = $resource;
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function getContents()
44
    {
45
        return $this->buildRequest()->send()->getBody(true);
46
    }
47
48
    /**
49
     * @return resource
50
     */
51
    public function getContentsAsStream()
52
    {
53
        if (!$this->stream) {
54
            $this->stream = $this->buildRequest()->send()->getBody(false);
55
        }
56
57
        return $this->stream->getStream();
58
    }
59
60
    /**
61
     * @return \Guzzle\Http\Message\RequestInterface
62
     */
63
    private function buildRequest()
64
    {
65
        return $this->client->get($this->resource->getOriginal());
66
    }
67
}
68