|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Ivory Http Adapter package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Ivory\HttpAdapter\Event\Subscriber; |
|
13
|
|
|
|
|
14
|
|
|
use Ivory\HttpAdapter\Event\Events; |
|
15
|
|
|
use Ivory\HttpAdapter\Event\MultiRequestErroredEvent; |
|
16
|
|
|
use Ivory\HttpAdapter\Event\RequestErroredEvent; |
|
17
|
|
|
use Ivory\HttpAdapter\Event\Retry\Retry; |
|
18
|
|
|
use Ivory\HttpAdapter\Event\Retry\RetryInterface; |
|
19
|
|
|
use Ivory\HttpAdapter\HttpAdapterException; |
|
20
|
|
|
use Ivory\HttpAdapter\MultiHttpAdapterException; |
|
21
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @author GeLo <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class RetrySubscriber implements EventSubscriberInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var RetryInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $retry; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param RetryInterface|null $retry |
|
35
|
|
|
*/ |
|
36
|
81 |
|
public function __construct(RetryInterface $retry = null) |
|
37
|
|
|
{ |
|
38
|
81 |
|
$this->retry = $retry ?: new Retry(); |
|
39
|
81 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return RetryInterface |
|
43
|
|
|
*/ |
|
44
|
18 |
|
public function getRetry() |
|
45
|
|
|
{ |
|
46
|
18 |
|
return $this->retry; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param RequestErroredEvent $event |
|
51
|
|
|
*/ |
|
52
|
27 |
|
public function onRequestErrored(RequestErroredEvent $event) |
|
53
|
|
|
{ |
|
54
|
27 |
|
if (($request = $this->retry->retry($event->getException()->getRequest())) === false) { |
|
|
|
|
|
|
55
|
9 |
|
return; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
18 |
|
$event->getException()->setRequest($request); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
try { |
|
61
|
18 |
|
$event->setResponse($event->getHttpAdapter()->sendRequest($request)); |
|
|
|
|
|
|
62
|
16 |
|
} catch (HttpAdapterException $e) { |
|
63
|
9 |
|
$event->setException($e); |
|
64
|
|
|
} |
|
65
|
18 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param MultiRequestErroredEvent $event |
|
69
|
|
|
*/ |
|
70
|
27 |
|
public function onMultiResponseErrored(MultiRequestErroredEvent $event) |
|
71
|
|
|
{ |
|
72
|
27 |
|
$retryRequests = []; |
|
73
|
|
|
|
|
74
|
27 |
|
foreach ($event->getExceptions() as $exception) { |
|
75
|
27 |
|
if (($request = $this->retry->retry($exception->getRequest(), false)) !== false) { |
|
|
|
|
|
|
76
|
18 |
|
$retryRequests[] = $request; |
|
77
|
20 |
|
$event->removeException($exception); |
|
78
|
14 |
|
} |
|
79
|
21 |
|
} |
|
80
|
|
|
|
|
81
|
27 |
|
if (empty($retryRequests)) { |
|
82
|
9 |
|
return; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
try { |
|
86
|
18 |
|
$event->addResponses($event->getHttpAdapter()->sendRequests($retryRequests)); |
|
87
|
16 |
|
} catch (MultiHttpAdapterException $e) { |
|
88
|
9 |
|
$event->addResponses($e->getResponses()); |
|
89
|
9 |
|
$event->addExceptions($e->getExceptions()); |
|
90
|
|
|
} |
|
91
|
18 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* {@inheritdoc} |
|
95
|
|
|
*/ |
|
96
|
9 |
|
public static function getSubscribedEvents() |
|
97
|
|
|
{ |
|
98
|
|
|
return [ |
|
99
|
9 |
|
Events::REQUEST_ERRORED => ['onRequestErrored', 0], |
|
100
|
7 |
|
Events::MULTI_REQUEST_ERRORED => ['onMultiResponseErrored', 0], |
|
101
|
7 |
|
]; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: