AutomaticConnection::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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
    /**
14
     * @var \DateTimeImmutable|null
15
     */
16
    private $connectedAt;
17
18
    /**
19
     * @var \DateInterval
20
     */
21
    private $interval;
22
23
    /**
24
     * @var bool
25
     */
26
    private $connecting = false;
27
28
    /**
29
     * @param ConnectionInterface $decoratedConnection
30
     * @param \DateInterval $interval
31
     */
32 17
    public function __construct(ConnectionInterface $decoratedConnection, \DateInterval $interval)
33
    {
34 17
        $this->decoratedConnection = $decoratedConnection;
35 17
        $this->interval = $interval;
36 17
    }
37
38
    /**
39
     * @param string $name
40
     * @param \Closure $callback
41
     */
42 15
    public function addListener(string $name, \Closure $callback): void
43
    {
44 15
        $this->decoratedConnection->addListener($name, $callback);
45 15
    }
46
47
    /**
48
     * @return void
49
     */
50 17
    public function connect(): void
51
    {
52 17
        if ($this->connecting === false) {
53
            try {
54 17
                $this->connecting = true;
55 17
                $this->decoratedConnection->connect();
56 8
                $this->connectedAt = new \DateTimeImmutable('now');
57 8
            } finally {
58 17
                $this->connecting = false;
59
            }
60
        }
61 8
    }
62
63
    /**
64
     * @return void
65
     */
66 2
    public function disconnect(): void
67
    {
68 2
        $this->decoratedConnection->disconnect();
69 2
        $this->connectedAt = null;
70 2
    }
71
72
    /**
73
     * @param string $request
74
     * @return int
75
     */
76 15
    public function send(string $request): int
77
    {
78 15
        $this->connectIfNotConnected();
79 7
        return $this->decoratedConnection->send($request);
80
    }
81
82
    /**
83
     * @return string
84
     */
85 7
    public function receive(): string
86
    {
87 7
        $this->connectIfNotConnected();
88 7
        return $this->decoratedConnection->receive();
89
    }
90
91
    /**
92
     * @param int $type
93
     */
94 1
    public function upgrade(int $type): void
95
    {
96 1
        $this->decoratedConnection->upgrade($type);
97 1
    }
98
99
    /**
100
     * @param float $timeout
101
     */
102 1
    public function timeout(float $timeout): void
103
    {
104 1
        $this->decoratedConnection->timeout($timeout);
105 1
    }
106
107
    /**
108
     * @param array<int, string> $keys
109
     * @return array<string, mixed>
0 ignored issues
show
Documentation introduced by
The doc-type array<string, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
110
     */
111 1
    public function getMetaData(array $keys = []): array
112
    {
113 1
        return $this->decoratedConnection->getMetaData($keys);
114
    }
115
    
116 16
    private function connectIfNotConnected(): void
117
    {
118 16
        if ($this->connectedAt === null) {
119 14
            $this->connect();
120
        }
121
122 8
        $now = new \DateTimeImmutable();
123 8
        if ($this->connecting === false && $this->connectedAt && $this->connectedAt->add($this->interval) < $now) {
124 1
            $this->disconnect();
125 1
            $this->connect();
126
        }
127 8
    }
128
}
129