Test Failed
Pull Request — master (#128)
by
unknown
06:53
created

RetryTransportWrapper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 66
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getTransport() 0 3 1
B send() 0 15 7
1
<?php
2
3
namespace Gelf\Transport;
4
5
use Gelf\MessageInterface as Message;
6
use RuntimeException;
7
8
class RetryTransportWrapper extends AbstractTransport
9
{
10
    /**
11
     * @const string
12
     */
13
    const NO_RESPONSE = "Graylog-Server didn't answer properly, expected 'HTTP/1.x 202 Accepted', response is ''";
14
15
    /**
16
     * @var HttpTransport
17
     */
18
    protected $transport;
19
20
    /**
21
     * @var int
22
     */
23
    protected $maxRetries;
24
25
    /**
26
     * @var callable|null
27
     */
28
    protected $exceptionMatcher;
29
30
    /**
31
     * KeepAliveRetryTransportWrapper constructor.
32
     *
33
     * @param TransportInterface $transport
34
     * @param int $maxRetries
35
     * @param callable|null $exceptionMatcher
36
     */
37
    public function __construct(TransportInterface $transport, $maxRetries, $exceptionMatcher = null)
38
    {
39
        $this->transport = $transport;
0 ignored issues
show
Documentation Bug introduced by
$transport is of type Gelf\Transport\TransportInterface, but the property $transport was declared to be of type Gelf\Transport\HttpTransport. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
40
        $this->maxRetries = $maxRetries;
41
        $this->exceptionMatcher = $exceptionMatcher;
42
    }
43
44
    /**
45
     * @return TransportInterface
46
     */
47
    public function getTransport()
48
    {
49
        return $this->transport;
50
    }
51
52
    /**
53
     * Sends a Message over this transport.
54
     *
55
     * @param Message $message
56
     *
57
     * @return int calls function to send message
58
     */
59
    public function send(Message $message)
60
    {
61
        $tries = 0;
62
63
        while (true) {
64
            try {
65
                $tries++;
66
                return $this->transport->send($message);
67
            } catch (RuntimeException $e) {
68
                if ($this->maxRetries !== 0 && $tries > $this->maxRetries) {
69
                    throw $e;
70
                }
71
72
                if ($this->exceptionMatcher && !call_user_func($this->exceptionMatcher, $e)) {
73
                    throw $e;
74
                }
75
            }
76
        }
77
    }
78
}
79