Completed
Push — master ( 503b9d...92f54d )
by Frederik
03:38
created

ReconnectAfterConnection   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 112
rs 10
c 0
b 0
f 0
ccs 36
cts 36
cp 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addListener() 0 4 1
A connect() 0 5 1
A disconnect() 0 5 1
A send() 0 5 1
A receive() 0 5 1
A upgrade() 0 4 1
A timeout() 0 4 1
A getMetaData() 0 4 1
A validateConnection() 0 11 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol;
5
6
final class ReconnectAfterConnection implements ConnectionInterface
7
{
8
    /**
9
     * @var ConnectionInterface
10
     */
11
    private $decoratedConnection;
12
    /**
13
     * @var \DateTimeImmutable
14
     */
15
    private $connectedAt;
16
    /**
17
     * @var \DateInterval
18
     */
19
    private $interval;
20
21
    /**
22
     * AppendCrlfConnection constructor.
23
     * @param ConnectionInterface $decoratedConnection
24
     * @param \DateInterval $interval
25
     */
26 3
    public function __construct(ConnectionInterface $decoratedConnection, \DateInterval $interval)
27
    {
28 3
        $this->decoratedConnection = $decoratedConnection;
29 3
        $this->interval = $interval;
30 3
    }
31
32
    /**
33
     * @param string $name
34
     * @param \Closure $callback
35
     */
36 1
    public function addListener(string $name, \Closure $callback): void
37
    {
38 1
        $this->decoratedConnection->addListener($name, $callback);
39 1
    }
40
41
    /**
42
     * @return void
43
     */
44 2
    public function connect(): void
45
    {
46 2
        $this->decoratedConnection->connect();
47 2
        $this->connectedAt = new \DateTimeImmutable('now');
48 2
    }
49
50
    /**
51
     * @return void
52
     */
53 2
    public function disconnect(): void
54
    {
55 2
        $this->decoratedConnection->disconnect();
56 2
        $this->connectedAt = null;
57 2
    }
58
59
    /**
60
     * @param string $request
61
     * @return int
62
     */
63 3
    public function send(string $request): int
64
    {
65 3
        $this->validateConnection();
66 2
        return $this->decoratedConnection->send($request);
67
    }
68
69
    /**
70
     * @return string
71
     */
72 1
    public function receive(): string
73
    {
74 1
        $this->validateConnection();
75 1
        return $this->decoratedConnection->receive();
76
    }
77
78
    /**
79
     * @param int $type
80
     */
81 1
    public function upgrade(int $type): void
82
    {
83 1
        $this->decoratedConnection->upgrade($type);
84 1
    }
85
86
    /**
87
     * @param float $timeout
88
     */
89 1
    public function timeout(float $timeout): void
90
    {
91 1
        $this->decoratedConnection->timeout($timeout);
92 1
    }
93
94
    /**
95
     * @param array $keys
96
     * @return array
97
     */
98 1
    public function getMetaData(array $keys = []): array
99
    {
100 1
        return $this->decoratedConnection->getMetaData($keys);
101
    }
102
103
    /**
104
     *
105
     */
106 3
    private function validateConnection(): void
107
    {
108 3
        if ($this->connectedAt === null) {
109 1
            throw new \RuntimeException('Never connected at all');
110
        }
111
112 2
        if ($this->connectedAt->add($this->interval) < new \DateTimeImmutable()) {
113 1
            $this->disconnect();
114 1
            $this->connect();
115
        }
116
    }
117
}