Retry   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 50
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A getStrategy() 0 4 1
A setStrategy() 0 4 1
A retry() 0 15 4
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;
13
14
use Ivory\HttpAdapter\Event\Retry\Strategy\ExponentialDelayedRetryStrategy;
15
use Ivory\HttpAdapter\Event\Retry\Strategy\LimitedRetryStrategy;
16
use Ivory\HttpAdapter\Event\Retry\Strategy\RetryStrategyInterface;
17
use Ivory\HttpAdapter\Message\InternalRequestInterface;
18
19
/**
20
 * @author GeLo <[email protected]>
21
 */
22
class Retry implements RetryInterface
23
{
24
    /**
25
     * @var RetryStrategyInterface
26
     */
27
    private $strategy;
28
29
    /**
30
     * @param RetryStrategyInterface|null $strategy
31
     */
32 63
    public function __construct(RetryStrategyInterface $strategy = null)
33
    {
34 63
        $this->setStrategy($strategy ?: new LimitedRetryStrategy(3, new ExponentialDelayedRetryStrategy()));
35 63
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 27
    public function getStrategy()
41
    {
42 27
        return $this->strategy;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 63
    public function setStrategy(RetryStrategyInterface $strategy)
49
    {
50 63
        $this->strategy = $strategy;
51 63
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 27
    public function retry(InternalRequestInterface $internalRequest, $wait = true)
57
    {
58 27
        if (!$this->strategy->verify($internalRequest)) {
59 9
            return false;
60
        }
61
62 18
        if ($wait && ($delay = $this->strategy->delay($internalRequest)) > 0) {
63 9
            usleep($delay * 1000000);
64 7
        }
65
66 18
        return $internalRequest->withParameter(
67 18
            self::RETRY_COUNT,
68 18
            $internalRequest->getParameter(self::RETRY_COUNT) + 1
69 14
        );
70
    }
71
}
72