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