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

LegacyGuzzleReaderFactory::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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