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

LegacyGuzzleReaderFactory::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace Alchemy\Zippy\Adapter\Guzzle;
4
5
use Alchemy\Zippy\Adapter\Guzzle\LegacyGuzzleReader;
6
use Alchemy\Zippy\Resource\Resource;
7
use Alchemy\Zippy\Resource\ResourceReader;
8
use Alchemy\Zippy\Resource\ResourceReaderFactory;
9
use Alchemy\Zippy\Resource\ResourceUri;
10
use Guzzle\Http\Client;
11
use Guzzle\Http\ClientInterface;
12
use Guzzle\Plugin\Backoff\BackoffPlugin;
13
use Symfony\Component\EventDispatcher\Event;
14
15
class LegacyGuzzleReaderFactory implements ResourceReaderFactory
16
{
17
    /**
18
     * @var ClientInterface|null
19
     */
20
    private $client = null;
21
22
    public function __construct(ClientInterface $client = null)
23
    {
24
        $this->client = $client;
25
26
        if (!$this->client) {
27
            $this->client = new Client();
28
29
            $this->client->getEventDispatcher()->addListener('request.error', function(Event $event) {
30
                // override guzzle default behavior of throwing exceptions
31
                // when 4xx & 5xx responses are encountered
32
                $event->stopPropagation();
33
            }, -254);
34
35
            $this->client->addSubscriber(BackoffPlugin::getExponentialBackoff(5, array(500, 502, 503, 408)));
36
        }
37
    }
38
39
    /**
40
     * @param ResourceUri $resource
41
     * @return ResourceReader
42
     */
43
    public function getReader(ResourceUri $resource)
44
    {
45
        return new LegacyGuzzleReader($resource, $this->client);
46
    }
47
}
48