Completed
Push — master ( 69c63f...51b5f1 )
by Thibaud
8s
created

GuzzleReader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 4
dl 0
loc 51
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\Resource\Reader\Guzzle;
4
5
use Alchemy\Zippy\Resource\Resource;
6
use Alchemy\Zippy\Resource\ResourceReader;
7
use GuzzleHttp\ClientInterface;
8
9
class GuzzleReader implements ResourceReader
10
{
11
    /**
12
     * @var ClientInterface
13
     */
14
    private $client;
15
16
    /**
17
     * @var \Alchemy\Zippy\Resource\Resource
18
     */
19
    private $resource;
20
21
    /**
22
     * @param \Alchemy\Zippy\Resource\Resource $resource
23
     * @param ClientInterface $client
24
     */
25
    public function __construct(Resource $resource, ClientInterface $client = null)
26
    {
27
        $this->resource = $resource;
28
        $this->client = $client;
29
    }
30
31
    /**
32
     * @return string
33
     */
34
    public function getContents()
35
    {
36
        return $this->buildRequest()->getBody()->getContents();
37
    }
38
39
    /**
40
     * @return resource
41
     */
42
    public function getContentsAsStream()
43
    {
44
        $response = $this->buildRequest()->getBody()->getContents();
45
        $stream = fopen('php://temp', 'r+');
46
47
        if ($response != '') {
48
            fwrite($stream, $response);
49
            fseek($stream, 0);
50
        }
51
52
        return $stream;
53
    }
54
55
    private function buildRequest()
56
    {
57
        return $this->client->request('GET', $this->resource->getOriginal());
58
    }
59
}
60