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
|
|
|
try { |
73
|
3 |
|
$options = $this->getOptions($modifiedSince); |
74
|
|
|
|
75
|
3 |
|
return new Response($this->guzzleClient->request('get', $url, $options)); |
76
|
2 |
|
} catch (BadResponseException $e) { |
77
|
2 |
|
switch ((int) $e->getResponse()->getStatusCode()) { |
78
|
2 |
|
case 404: |
79
|
1 |
|
throw new NotFoundException($e->getMessage()); |
80
|
|
|
default: |
81
|
1 |
|
throw new ServerErrorException($e->getMessage()); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param iterable $requests |
88
|
|
|
* @param ReaderInterface $reader |
89
|
|
|
* @return \Generator |
90
|
|
|
*/ |
91
|
4 |
|
public function getPromises(iterable $requests, ReaderInterface $reader) : \Generator |
92
|
|
|
{ |
93
|
4 |
|
foreach ($requests as $request) { |
94
|
4 |
|
yield $this->getPromise($request, $reader); |
95
|
|
|
} |
96
|
4 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Request $request |
100
|
|
|
* @param ReaderInterface $reader |
101
|
|
|
* @return PromiseInterface |
102
|
|
|
*/ |
103
|
4 |
|
protected function getPromise(Request $request, ReaderInterface $reader) : PromiseInterface |
104
|
|
|
{ |
105
|
4 |
|
$promise = $this->newPromise($request); |
106
|
|
|
|
107
|
|
|
$promise->then(function ($psrResponse) use ($request, $reader) { |
108
|
|
|
try { |
109
|
3 |
|
$request->setResponse(new Response($psrResponse)); |
110
|
3 |
|
$reader->handle($request); |
111
|
1 |
|
} catch (\Exception $e) { |
112
|
1 |
|
$reader->handleError($request, $e); |
113
|
|
|
} |
114
|
|
|
}, function ($error) use ($request, $reader) { |
115
|
1 |
|
$reader->handleError($request, $error); |
116
|
4 |
|
}); |
117
|
|
|
|
118
|
4 |
|
return $promise; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param Request $request |
123
|
|
|
* @return PromiseInterface |
124
|
|
|
*/ |
125
|
4 |
|
protected function newPromise(Request $request) : PromiseInterface |
126
|
|
|
{ |
127
|
4 |
|
$options = $this->getOptions($request->getModifiedSince()); |
128
|
|
|
|
129
|
4 |
|
return $this->guzzleClient->requestAsync('GET', $request->getUrl(), $options); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param \DateTime $modifiedSince |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
7 |
|
protected function getOptions(\DateTime $modifiedSince) : array |
137
|
|
|
{ |
138
|
|
|
return [ |
139
|
|
|
'headers' => [ |
140
|
7 |
|
'User-Agent' => $this->userAgent, |
141
|
7 |
|
'If-Modified-Since' => $modifiedSince->format(\DateTime::RFC2822) |
142
|
|
|
] |
143
|
|
|
]; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|