Passed
Pull Request — master (#128)
by
unknown
01:53
created

RetryTransportWrapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
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 TransportInterface
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 4
    public function __construct(TransportInterface $transport, $maxRetries, $exceptionMatcher = null)
38
    {
39 4
        $this->transport = $transport;
40 4
        $this->maxRetries = $maxRetries;
41 4
        $this->exceptionMatcher = $exceptionMatcher;
42 4
    }
43
44
    /**
45
     * @return TransportInterface
46
     */
47 1
    public function getTransport()
48
    {
49 1
        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 3
    public function send(Message $message)
60
    {
61 3
        $tries = 0;
62
63 3
        while (true) {
64
            try {
65 3
                $tries++;
66 3
                return $this->transport->send($message);
67 3
            } catch (RuntimeException $e) {
68 3
                if ($this->maxRetries !== 0 && $tries > $this->maxRetries) {
69 2
                    throw $e;
70
                }
71
72 3
                if ($this->exceptionMatcher && !call_user_func($this->exceptionMatcher, $e)) {
73 1
                    throw $e;
74
                }
75
            }
76
        }
77
    }
78
}
79