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 \FeedIo\Adapter\Guzzle\Client; |
14
|
|
|
use FeedIo\Adapter\Guzzle\Async\ReaderInterface; |
15
|
|
|
use \GuzzleHttp\Promise\EachPromise; |
16
|
|
|
use \FeedIo\Reader as MainReader; |
17
|
|
|
use \FeedIo\Reader\Result; |
18
|
|
|
use \FeedIo\FeedInterface; |
19
|
|
|
|
20
|
|
|
class Reader implements ReaderInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var \FeedIo\Reader |
24
|
|
|
*/ |
25
|
|
|
protected $reader; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \FeedIo\Adapter\Guzzle\Client |
29
|
|
|
*/ |
30
|
|
|
protected $client; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var CallbackInterface |
34
|
|
|
*/ |
35
|
|
|
protected $callback; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $feedClass; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Reader constructor. |
44
|
|
|
* @param MainReader $reader |
45
|
|
|
* @param Client $client |
46
|
|
|
* @param CallbackInterface $callback |
47
|
|
|
* @param string $feedClass |
48
|
|
|
*/ |
49
|
3 |
|
public function __construct(MainReader $reader, Client $client, CallbackInterface $callback, string $feedClass) |
50
|
|
|
{ |
51
|
3 |
|
$this->reader = $reader; |
52
|
3 |
|
$this->client = $client; |
53
|
3 |
|
$this->callback = $callback; |
54
|
3 |
|
$this->feedClass = $feedClass; |
55
|
3 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param iterable $requests |
59
|
|
|
*/ |
60
|
3 |
|
public function process(iterable $requests) : void |
61
|
|
|
{ |
62
|
3 |
|
$promises = $this->client->getPromises($requests, $this); |
63
|
|
|
|
64
|
3 |
|
(new EachPromise($promises))->promise()->wait(); |
65
|
3 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param Request $request |
69
|
|
|
*/ |
70
|
2 |
|
public function handle(Request $request) : void |
71
|
|
|
{ |
72
|
2 |
|
$feed = $this->newFeed(); |
73
|
2 |
|
$document = $this->reader->handleResponse($request->getResponse(), $feed); |
74
|
2 |
|
$result = new Result($document, $feed, $request->getModifiedSince(), $request->getResponse(), $request->getUrl()); |
75
|
2 |
|
$this->callback->process($result); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param Request $request |
80
|
|
|
* @param \Exception $e |
81
|
|
|
*/ |
82
|
2 |
|
public function handleError(Request $request, \Exception $e) : void |
83
|
|
|
{ |
84
|
2 |
|
$this->callback->handleError($request, $e); |
85
|
2 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return FeedInterface |
89
|
|
|
*/ |
90
|
2 |
|
public function newFeed() : FeedInterface |
91
|
|
|
{ |
92
|
2 |
|
$reflection = new \ReflectionClass($this->feedClass); |
93
|
|
|
|
94
|
2 |
|
return $reflection->newInstanceArgs(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|