Completed
Push — master ( 7d6428...397f57 )
by Frederik
02:14
created

AutomaticConnection::connectIfNotConnected()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

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