RetryTransportWrapper::send()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 10
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 15
rs 9.2222
ccs 7
cts 7
cp 1
crap 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