1: <?php
2:
3: namespace Guzzle\Plugin\Backoff;
4:
5: use Guzzle\Common\Exception\InvalidArgumentException;
6: use Guzzle\Http\Message\RequestInterface;
7: use Guzzle\Http\Message\Response;
8: use Guzzle\Http\Exception\HttpException;
9:
10: /**
11: * Strategy that will invoke a closure to determine whether or not to retry with a delay
12: */
13: class CallbackBackoffStrategy extends AbstractBackoffStrategy
14: {
15: /**
16: * @var \Closure|array|mixed Callable method to invoke
17: */
18: protected $callback;
19:
20: /**
21: * @var bool Whether or not this strategy makes a retry decision
22: */
23: protected $decision;
24:
25: /**
26: * @param \Closure|array|mixed $callback Callable method to invoke
27: * @param bool $decision Set to true if this strategy makes a backoff decision
28: * @param BackoffStrategyInterface $next The optional next strategy
29: *
30: * @throws InvalidArgumentException
31: */
32: public function __construct($callback, $decision, BackoffStrategyInterface $next = null)
33: {
34: if (!is_callable($callback)) {
35: throw new InvalidArgumentException('The callback must be callable');
36: }
37: $this->callback = $callback;
38: $this->decision = (bool) $decision;
39: $this->next = $next;
40: }
41:
42: /**
43: * {@inheritdoc}
44: */
45: public function makesDecision()
46: {
47: return $this->decision;
48: }
49:
50: /**
51: * {@inheritdoc}
52: */
53: protected function getDelay($retries, RequestInterface $request, Response $response = null, HttpException $e = null)
54: {
55: return call_user_func($this->callback, $retries, $request, $response, $e);
56: }
57: }
58: