Completed
Push — master ( b425fd...816779 )
by Frederik
02:01
created

AutomaticConnection::send()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
ccs 3
cts 3
cp 1
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol;
5
6
final class AutomaticConnection 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
     * @var bool
22
     */
23
    private $connecting = false;
24
25
    /**
26
     * AppendCrlfConnection constructor.
27
     * @param ConnectionInterface $decoratedConnection
28
     * @param \DateInterval $interval
29
     */
30 8
    public function __construct(ConnectionInterface $decoratedConnection, \DateInterval $interval)
31
    {
32 8
        $this->decoratedConnection = $decoratedConnection;
33 8
        $this->interval = $interval;
34 8
    }
35
36
    /**
37
     * @param string $name
38
     * @param \Closure $callback
39
     */
40 7
    public function addListener(string $name, \Closure $callback): void
41
    {
42 7
        $this->decoratedConnection->addListener($name, $callback);
43 7
    }
44
45
    /**
46
     * @return void
47
     */
48 8
    public function connect(): void
49
    {
50 8
        if ($this->connecting === false) {
51 8
            $this->connecting = true;
52 8
            $this->decoratedConnection->connect();
53 4
            $this->connectedAt = new \DateTimeImmutable('now');
54 4
            $this->connecting = false;
55
        }
56 4
    }
57
58
    /**
59
     * @return void
60
     */
61 2
    public function disconnect(): void
62
    {
63 2
        $this->decoratedConnection->disconnect();
64 2
        $this->connectedAt = null;
65 2
    }
66
67
    /**
68
     * @param string $request
69
     * @return int
70
     */
71 8
    public function send(string $request): int
72
    {
73 8
        $this->connectIfNotConnected();
74 4
        return $this->decoratedConnection->send($request);
75
    }
76
77
    /**
78
     * @return string
79
     */
80 3
    public function receive(): string
81
    {
82 3
        $this->connectIfNotConnected();
83 3
        return $this->decoratedConnection->receive();
84
    }
85
86
    /**
87
     * @param int $type
88
     */
89 1
    public function upgrade(int $type): void
90
    {
91 1
        $this->decoratedConnection->upgrade($type);
92 1
    }
93
94
    /**
95
     * @param float $timeout
96
     */
97 1
    public function timeout(float $timeout): void
98
    {
99 1
        $this->decoratedConnection->timeout($timeout);
100 1
    }
101
102
    /**
103
     * @param array $keys
104
     * @return array
105
     */
106 1
    public function getMetaData(array $keys = []): array
107
    {
108 1
        return $this->decoratedConnection->getMetaData($keys);
109
    }
110
111
    /**
112
     *
113
     */
114 8
    private function connectIfNotConnected(): void
115
    {
116 8
        if ($this->connectedAt === null) {
117 6
            $this->connect();
118
        }
119
120 4
        if ($this->connecting === false && $this->connectedAt->add($this->interval) < new \DateTimeImmutable()) {
121 1
            $this->disconnect();
122 1
            $this->connect();
123
        }
124
    }
125
}