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

GuzzleReader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 51
wmc 5
lcom 1
cbo 3
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getContents() 0 4 1
A getContentsAsStream() 0 12 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 GuzzleHttp\ClientInterface;
9
10
class GuzzleReader implements ResourceReader
11
{
12
    /**
13
     * @var ClientInterface
14
     */
15
    private $client;
16
17
    /**
18
     * @var \Alchemy\Zippy\Resource\Resource
19
     */
20
    private $resource;
21
22
    /**
23
     * @param ResourceUri $resource
24
     * @param ClientInterface $client
25
     */
26
    public function __construct(ResourceUri $resource, ClientInterface $client = null)
27
    {
28
        $this->resource = $resource;
0 ignored issues
show
Documentation Bug introduced by
It seems like $resource of type object<Alchemy\Zippy\Resource\ResourceUri> is incompatible with the declared type object<Alchemy\Zippy\Resource\Resource> of property $resource.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
29
        $this->client = $client;
30
    }
31
32
    /**
33
     * @return string
34
     */
35
    public function getContents()
36
    {
37
        return $this->buildRequest()->getBody()->getContents();
38
    }
39
40
    /**
41
     * @return resource
42
     */
43
    public function getContentsAsStream()
44
    {
45
        $response = $this->buildRequest()->getBody()->getContents();
46
        $stream = fopen('php://temp', 'r+');
47
48
        if ($response != '') {
49
            fwrite($stream, $response);
50
            fseek($stream, 0);
51
        }
52
53
        return $stream;
54
    }
55
56
    private function buildRequest()
57
    {
58
        return $this->client->request('GET', $this->resource->getUri());
59
    }
60
}
61