Completed
Push — master ( d975fd...8f2bc0 )
by Alex
06:20
created

Client::getDuration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo\Adapter\Guzzle;
12
13
use FeedIo\Adapter\ClientInterface;
14
use FeedIo\Adapter\Guzzle\Async\ReaderInterface;
15
use FeedIo\Adapter\NotFoundException;
16
use FeedIo\Adapter\ResponseInterface;
17
use FeedIo\Adapter\ServerErrorException;
18
use FeedIo\Async\Request;
19
use \GuzzleHttp\Promise\PromiseInterface;
20
use GuzzleHttp\Exception\BadResponseException;
21
22
/**
23
 * Guzzle dependent HTTP client
24
 */
25
class Client implements ClientInterface
26
{
27
28
    /**
29
     * Default user agent provided with the package
30
     */
31
    const DEFAULT_USER_AGENT = 'Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1';
32
33
    /**
34
     * @var \GuzzleHttp\ClientInterface
35
     */
36
    protected $guzzleClient;
37
38
    /**
39
     * @var string
40
     */
41
    protected $userAgent;
42
43
    /**
44
     * @param \GuzzleHttp\ClientInterface $guzzleClient
45
     * @param string $userAgent
46
     */
47 11
    public function __construct(\GuzzleHttp\ClientInterface $guzzleClient, string $userAgent = self::DEFAULT_USER_AGENT)
48
    {
49 11
        $this->guzzleClient = $guzzleClient;
50 11
        $this->userAgent = $userAgent;
51 11
    }
52
53
    /**
54
     * @param  string $userAgent The new user-agent
55
     * @return Client
56
     */
57
    public function setUserAgent(string $userAgent) : Client
58
    {
59
        $this->userAgent = $userAgent;
60
61
        return $this;
62
    }
63
64
    /**
65
     * @param string $url
66
     * @param \DateTime $modifiedSince
67
     * @return ResponseInterface
68
     * @throws \GuzzleHttp\Exception\GuzzleException
69
     */
70 3
    public function getResponse(string $url, \DateTime $modifiedSince) : ResponseInterface
71
    {
72
        $start = microtime(true);
73 3
        try {
74
            $options = $this->getOptions($modifiedSince);
75 3
            $psrResponse = $this->guzzleClient->request('get', $url, $options);
76 2
            $duration = $this->getDuration($start);
77 2
            return new Response($psrResponse, $duration);
78 2
        } catch (BadResponseException $e) {
79 1
            $duration = $this->getDuration($start);
80
            switch ((int) $e->getResponse()->getStatusCode()) {
81 1
                case 404:
82
                    $notFoundException = new NotFoundException($e->getMessage());
83
                    $notFoundException->setDuration($duration);
84
                    throw $notFoundException;
85
                default:
86
                    $serverErrorException = new ServerErrorException($e->getMessage());
87
                    $serverErrorException->setResponse($e->getResponse());
88
                    $serverErrorException->setDuration($duration);
89
                    throw $serverErrorException;
90
            }
91 4
        }
92
    }
93 4
94 4
    /**
95
     * @param float $start
96 4
     * @return int
97
     */
98
    protected function getDuration(float $start): int
99
    {
100
        return intval(round(microtime(true) - $start, 3) * 1000);
101
    }
102
    /**
103 4
     * @param iterable $requests
104
     * @param ReaderInterface $reader
105 4
     * @return \Generator
106
     */
107 4
    public function getPromises(iterable $requests, ReaderInterface $reader) : \Generator
108
    {
109 3
        foreach ($requests as $request) {
110 3
            yield $this->getPromise($request, $reader);
111 1
        }
112 1
    }
113
114 4
    /**
115 1
     * @param Request $request
116 4
     * @param ReaderInterface $reader
117
     * @return PromiseInterface
118 4
     */
119
    protected function getPromise(Request $request, ReaderInterface $reader) : PromiseInterface
120
    {
121
        $promise = $this->newPromise($request);
122
123
        $promise->then(function ($psrResponse) use ($request, $reader) {
124
            try {
125 4
                $request->setResponse(new Response($psrResponse, 0));
126
                $reader->handle($request);
127 4
            } catch (\Exception $e) {
128
                $reader->handleError($request, $e);
129 4
            }
130
        }, function ($error) use ($request, $reader) {
131
            $reader->handleError($request, $error);
132
        });
133
134
        return $promise;
135
    }
136 7
137
    /**
138
     * @param Request $request
139
     * @return PromiseInterface
140 7
     */
141 7
    protected function newPromise(Request $request) : PromiseInterface
142
    {
143
        $options = $this->getOptions($request->getModifiedSince());
144
145
        return $this->guzzleClient->requestAsync('GET', $request->getUrl(), $options);
146
    }
147
148
    /**
149
     * @param \DateTime $modifiedSince
150
     * @return array
151
     */
152
    protected function getOptions(\DateTime $modifiedSince) : array
153
    {
154
        return [
155
            'headers' => [
156
                'Accept-Encoding' => 'gzip, deflate',
157
                'User-Agent' => $this->userAgent,
158
                'If-Modified-Since' => $modifiedSince->format(\DateTime::RFC2822)
159
            ]
160
        ];
161
    }
162
}
163