Version 1
[yaffs-website] / vendor / alchemy / zippy / src / Resource / Reader / Guzzle / LegacyGuzzleReader.php
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 }