Completed
Pull Request — master (#14)
by Márk
03:42
created

RetryPlugin::handleRequest()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 29
ccs 17
cts 17
cp 1
rs 8.5806
cc 4
eloc 15
nc 1
nop 3
crap 4
1
<?php
2
3
namespace Http\Client\Common\Plugin;
4
5
use Http\Client\Common\Plugin;
6
use Http\Client\Exception;
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
11
/**
12
 * Retry the request if an exception is thrown.
13
 *
14
 * By default will retry only one time.
15
 *
16
 * @author Joel Wurtz <[email protected]>
17
 *
18
 * TODO: make class final in version 2.0, once plugins does not extend it anymore.
19
 */
20
/*final*/ class RetryPlugin implements Plugin
21
{
22
    /**
23
     * Number of retry before sending an exception.
24
     *
25
     * @var int
26
     */
27
    private $retry;
28
29
    /**
30
     * Store the retry counter for each request.
31
     *
32
     * @var array
33
     */
34
    private $retryStorage = [];
35
36
    /**
37
     * @param array $config {
38
     *
39
     *     @var int $retries Number of retries to attempt if an exception occurs before letting the exception bubble up.
40
     * }
41
     */
42 6 View Code Duplication
    public function __construct(array $config = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44 6
        $resolver = new OptionsResolver();
45 6
        $resolver->setDefaults([
46 6
            'retries' => 1,
47 6
        ]);
48 6
        $resolver->setAllowedTypes('retries', 'int');
49 6
        $options = $resolver->resolve($config);
50
51 6
        $this->retry = $options['retries'];
52 6
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 4
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
58
    {
59 4
        $chainIdentifier = spl_object_hash((object) $first);
60
61
        return $next($request)->then(function (ResponseInterface $response) use ($request, $chainIdentifier) {
62 3
            if (array_key_exists($chainIdentifier, $this->retryStorage)) {
63 2
                unset($this->retryStorage[$chainIdentifier]);
64 2
            }
65
66 3
            return $response;
67 4
        }, function (Exception $exception) use ($request, $next, $first, $chainIdentifier) {
68 3
            if (!array_key_exists($chainIdentifier, $this->retryStorage)) {
69 3
                $this->retryStorage[$chainIdentifier] = 0;
70 3
            }
71
72 3
            if ($this->retryStorage[$chainIdentifier] >= $this->retry) {
73 1
                unset($this->retryStorage[$chainIdentifier]);
74
75 1
                throw $exception;
76
            }
77
78 3
            ++$this->retryStorage[$chainIdentifier];
79
80
            // Retry in synchrone
81 3
            $promise = $this->handleRequest($request, $next, $first);
82
83 3
            return $promise->wait();
84 4
        });
85
    }
86
}
87