RetryPlugin::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 9
cts 9
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Http\Client\Plugin;
4
5 1
@trigger_error('The '.__NAMESPACE__.'\RetryPlugin class is deprecated since version 1.1 and will be removed in 2.0. Use Http\Client\Common\Plugin\RetryPlugin instead.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
6
7
use Http\Client\Exception;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
12
/**
13
 * Retry the request if an exception is thrown.
14
 *
15
 * By default will retry only one time.
16
 *
17
 * @author Joel Wurtz <[email protected]>
18
 *
19
 * @deprecated since since version 1.1, and will be removed in 2.0. Use {@link \Http\Client\Common\Plugin\RetryPlugin} instead.
20
 */
21
class RetryPlugin implements Plugin
0 ignored issues
show
Deprecated Code introduced by
The interface Http\Client\Plugin\Plugin has been deprecated with message: since since version 1.1, and will be removed in 2.0. Use {@link \Http\Client\Common\Plugin} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

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