Completed
Pull Request — master (#14)
by Márk
14:10 queued 11:56
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
final class RetryPlugin implements Plugin
19
{
20
    /**
21
     * Number of retry before sending an exception.
22
     *
23
     * @var int
24
     */
25
    private $retry;
26
27
    /**
28
     * Store the retry counter for each request.
29
     *
30
     * @var array
31
     */
32
    private $retryStorage = [];
33
34
    /**
35
     * @param array $config {
36
     *
37
     *     @var int $retries Number of retries to attempt if an exception occurs before letting the exception bubble up.
38
     * }
39
     */
40 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...
41
    {
42 6
        $resolver = new OptionsResolver();
43 6
        $resolver->setDefaults([
44 6
            'retries' => 1,
45 6
        ]);
46 6
        $resolver->setAllowedTypes('retries', 'int');
47 6
        $options = $resolver->resolve($config);
48
49 6
        $this->retry = $options['retries'];
50 6
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 4
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
56
    {
57 4
        $chainIdentifier = spl_object_hash((object) $first);
58
59
        return $next($request)->then(function (ResponseInterface $response) use ($request, $chainIdentifier) {
60 3
            if (array_key_exists($chainIdentifier, $this->retryStorage)) {
61 2
                unset($this->retryStorage[$chainIdentifier]);
62 2
            }
63
64 3
            return $response;
65 4
        }, function (Exception $exception) use ($request, $next, $first, $chainIdentifier) {
66 3
            if (!array_key_exists($chainIdentifier, $this->retryStorage)) {
67 3
                $this->retryStorage[$chainIdentifier] = 0;
68 3
            }
69
70 3
            if ($this->retryStorage[$chainIdentifier] >= $this->retry) {
71 1
                unset($this->retryStorage[$chainIdentifier]);
72
73 1
                throw $exception;
74
            }
75
76 3
            ++$this->retryStorage[$chainIdentifier];
77
78
            // Retry in synchrone
79 3
            $promise = $this->handleRequest($request, $next, $first);
80
81 3
            return $promise->wait();
82 4
        });
83
    }
84
}
85