Completed
Push — master ( d06bab...02e31a )
by Eugene
03:46
created

Retryable::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Tarantool\Client\Connection;
4
5
use Tarantool\Client\Exception\ConnectionException;
6
7
class Retryable implements Connection
8
{
9
    const DEFAULT_MAX_RETRIES = 3;
10
11
    private $connection;
12
    private $maxRetries;
13
14 8
    public function __construct(Connection $connection, $maxRetries = null)
15
    {
16 8
        $this->connection = $connection;
17 8
        $this->maxRetries = null === $maxRetries ? self::DEFAULT_MAX_RETRIES : $maxRetries;
18 8
    }
19
20 5
    public function open()
21
    {
22 5
        $retry = 0;
23
24
        do {
25
            try {
26 5
                return $this->connection->open();
27 4
            } catch (ConnectionException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
28
            }
29 3
            ++$retry;
30 3
        } while ($retry <= $this->maxRetries);
31
32 1
        throw $e;
33
    }
34
35 1
    public function close()
36
    {
37 1
        $this->connection->close();
38 1
    }
39
40 1
    public function isClosed()
41
    {
42 1
        return $this->connection->isClosed();
43
    }
44
45 1
    public function send($data)
46
    {
47 1
        return $this->connection->send($data);
48
    }
49
}
50