RetryTransportWrapper   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 40
rs 10
ccs 7
cts 7
cp 1
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getTransport() 0 3 1
A send() 0 15 6
1
<?php
2
declare(strict_types=1);
3
4
namespace Gelf\Transport;
5
6
use Closure;
7
use Gelf\MessageInterface as Message;
8
use Throwable;
9
10
class RetryTransportWrapper implements TransportInterface
11
{
12
    protected Closure $exceptionMatcher;
13
14
    /**
15
     * KeepAliveRetryTransportWrapper constructor.
16
     *
17
     * @param null|callable(Throwable):bool $exceptionMatcher
18
     */
19
    public function __construct(
20
        private TransportInterface $transport,
21
        private int $maxRetries,
22
        ?callable $exceptionMatcher = null
23
    ) {
24
        $this->exceptionMatcher = Closure::fromCallable($exceptionMatcher ?? fn (Throwable $_) => true);
0 ignored issues
show
Unused Code introduced by
The parameter $_ is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

24
        $this->exceptionMatcher = Closure::fromCallable($exceptionMatcher ?? fn (/** @scrutinizer ignore-unused */ Throwable $_) => true);

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
    }
26
27
    public function getTransport(): TransportInterface
28
    {
29
        return $this->transport;
30
    }
31
32
    /**
33
     * @inheritDoc
34
     */
35
    public function send(Message $message): int
36
    {
37 4
        $tries = 0;
38
39 4
        while (true) {
40 4
            try {
41 4
                $tries++;
42 4
                return $this->transport->send($message);
43
            } catch (Throwable $e) {
44
                if ($this->maxRetries !== 0 && $tries > $this->maxRetries) {
45
                    throw $e;
46
                }
47 1
48
                if (!call_user_func($this->exceptionMatcher, $e)) {
49 1
                    throw $e;
50
                }
51
            }
52
        }
53
    }
54
}
55