|
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\Retry\Strategy; |
|
13
|
|
|
|
|
14
|
|
|
use Ivory\HttpAdapter\Message\InternalRequestInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author GeLo <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class CallbackRetryStrategy extends AbstractRetryStrategyChain |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var callable|null |
|
23
|
|
|
*/ |
|
24
|
|
|
private $verifyCallback; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var callable|null |
|
28
|
|
|
*/ |
|
29
|
|
|
private $delayCallback; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param callable|null $verifyCallback |
|
33
|
|
|
* @param callable|null $delayCallback |
|
34
|
|
|
* @param RetryStrategyChainInterface|null $next |
|
35
|
|
|
*/ |
|
36
|
72 |
|
public function __construct($verifyCallback = null, $delayCallback = null, RetryStrategyChainInterface $next = null) |
|
37
|
|
|
{ |
|
38
|
72 |
|
parent::__construct($next); |
|
39
|
|
|
|
|
40
|
72 |
|
$this->setVerifyCallback($verifyCallback); |
|
41
|
72 |
|
$this->setDelayCallback($delayCallback); |
|
42
|
72 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return bool |
|
46
|
|
|
*/ |
|
47
|
45 |
|
public function hasVerifyCallback() |
|
48
|
|
|
{ |
|
49
|
45 |
|
return $this->verifyCallback !== null; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return callable|null |
|
54
|
|
|
*/ |
|
55
|
27 |
|
public function getVerifyCallback() |
|
56
|
|
|
{ |
|
57
|
27 |
|
return $this->verifyCallback; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param callable|null $verifyCallback |
|
62
|
|
|
*/ |
|
63
|
72 |
|
public function setVerifyCallback($verifyCallback) |
|
64
|
|
|
{ |
|
65
|
72 |
|
$this->verifyCallback = $verifyCallback; |
|
66
|
72 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
45 |
|
public function hasDelayCallback() |
|
72
|
|
|
{ |
|
73
|
45 |
|
return $this->delayCallback !== null; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return callable|null |
|
78
|
|
|
*/ |
|
79
|
27 |
|
public function getDelayCallback() |
|
80
|
|
|
{ |
|
81
|
27 |
|
return $this->delayCallback; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param callable|null $delayCallback |
|
86
|
|
|
*/ |
|
87
|
72 |
|
public function setDelayCallback($delayCallback) |
|
88
|
|
|
{ |
|
89
|
72 |
|
$this->delayCallback = $delayCallback; |
|
90
|
72 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
18 |
|
protected function doVerify(InternalRequestInterface $request) |
|
96
|
|
|
{ |
|
97
|
18 |
|
if ($this->hasVerifyCallback()) { |
|
98
|
9 |
|
return call_user_func($this->verifyCallback, $request); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
9 |
|
return parent::doVerify($request); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* {@inheritdoc} |
|
106
|
|
|
*/ |
|
107
|
18 |
|
protected function doDelay(InternalRequestInterface $request) |
|
108
|
|
|
{ |
|
109
|
18 |
|
if ($this->hasDelayCallback()) { |
|
110
|
9 |
|
return call_user_func($this->delayCallback, $request); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
9 |
|
return parent::doDelay($request); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|