Completed
Branch full-rewrite (4754d3)
by Thibaud
03:13
created

LegacyGuzzleReader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

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

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
    }
68
}
69