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\Async; |
12
|
|
|
|
13
|
|
|
use \GuzzleHttp\ClientInterface; |
14
|
|
|
use \GuzzleHttp\Promise\PromiseInterface; |
15
|
|
|
use \FeedIo\Adapter\Guzzle\Response; |
16
|
|
|
|
17
|
|
|
class Client |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \GuzzleHttp\ClientInterface |
22
|
|
|
*/ |
23
|
|
|
protected $guzzleClient; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Reader |
27
|
|
|
*/ |
28
|
|
|
protected $reader; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Client constructor. |
32
|
|
|
* @param ClientInterface $guzzleClient |
33
|
|
|
* @param Reader $reader |
34
|
|
|
*/ |
35
|
|
|
public function __construct(ClientInterface $guzzleClient, Reader $reader) |
36
|
|
|
{ |
37
|
|
|
$this->guzzleClient = $guzzleClient; |
38
|
|
|
$this->reader = $reader; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param iterable $requests |
44
|
|
|
* @return \Generator |
45
|
|
|
*/ |
46
|
|
|
public function getPromises(iterable $requests) : \Generator |
47
|
|
|
{ |
48
|
|
|
foreach ($requests as $request) { |
49
|
|
|
yield $this->getPromise($request); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Request $request |
55
|
|
|
* @return PromiseInterface |
56
|
|
|
*/ |
57
|
|
|
protected function getPromise(Request $request) : PromiseInterface |
58
|
|
|
{ |
59
|
|
|
$options = [ |
60
|
|
|
'headers' => [ |
61
|
|
|
'User-Agent' => 'Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1', |
62
|
|
|
'If-Modified-Since' => $request->getModifiedSince()->format(\DateTime::RFC2822) |
63
|
|
|
] |
64
|
|
|
]; |
65
|
|
|
$promise = $this->guzzleClient->requestAsync('GET', $request->getUrl(), $options); |
66
|
|
|
|
67
|
|
|
$reader= $this->reader; |
68
|
|
|
$promise->then(function ($psrResponse) use ($request, $reader) { |
69
|
|
|
$request->setResponse(new Response($psrResponse)); |
70
|
|
|
$reader->handle($request); |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
return $promise; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|