Completed
Push — master ( 2a8ee7...6bb313 )
by Alessandro
8s
created

InfluxDbEventListener::writeEventPoints()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Algatux\InfluxDbBundle\Events\Listeners;
6
7
use Algatux\InfluxDbBundle\Events\AbstractDeferredInfluxDbEvent;
8
use Algatux\InfluxDbBundle\Events\AbstractInfluxDbEvent;
9
use Algatux\InfluxDbBundle\Events\DeferredUdpEvent;
10
use Algatux\InfluxDbBundle\Events\UdpEvent;
11
use InfluxDB\Database;
12
use InfluxDB\Point;
13
use Symfony\Component\EventDispatcher\Event;
14
15
/**
16
 * @internal
17
 */
18
final class InfluxDbEventListener
19
{
20
    const STORAGE_KEY_UDP = 'udp';
21
    const STORAGE_KEY_HTTP = 'http';
22
23
    /**
24
     * @var string
25
     */
26
    private $connection;
27
28
    /**
29
     * @var bool
30
     */
31
    private $isDefault;
32
33
    /**
34
     * @var Database
35
     */
36
    private $httpDatabase;
37
38
    /**
39
     * @var Database
40
     */
41
    private $udpDatabase;
42
43
    /**
44
     * @var array
45
     */
46
    private $storage;
47
48
    /**
49
     * @param string   $connection
50
     * @param bool     $isDefault
51
     * @param Database $httpDatabase
52
     * @param Database $udpDatabase
53
     */
54 7
    public function __construct(string $connection, bool $isDefault, Database $httpDatabase, Database $udpDatabase = null)
55
    {
56 7
        $this->connection = $connection;
57 7
        $this->isDefault = $isDefault;
58 7
        $this->httpDatabase = $httpDatabase;
59 7
        $this->udpDatabase = $udpDatabase;
60 7
        $this->initStorage();
61 7
    }
62
63
    /**
64
     * @param AbstractInfluxDbEvent $event
65
     *
66
     * @return bool
67
     */
68 7
    public function onPointsCollected(AbstractInfluxDbEvent $event): bool
69
    {
70 7
        if (!$this->isConcerned($event)) {
71 1
            return false;
72
        }
73
74 6
        $this->testUdpConnection($event);
75
76 4
        if ($event instanceof AbstractDeferredInfluxDbEvent) {
77 2
            $this->addEventPointsToStorage($event);
78
79 2
            return true;
80
        }
81
82 2
        $this->writeEventPoints($event);
83
84 2
        return true;
85
    }
86
87
    /**
88
     * @param Event $event
89
     *
90
     * @return bool
91
     */
92 2
    public function onKernelTerminate(Event $event): bool
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
    {
94 2
        foreach ($this->storage[static::STORAGE_KEY_UDP] as $precision => $points) {
95 1
            $this->writeUdpPoints($points, $precision);
96
        }
97
98 2
        foreach ($this->storage[static::STORAGE_KEY_HTTP] as $precision => $points) {
99 1
            $this->writeHttpPoints($points, $precision);
100
        }
101
102
        // Reset the storage after writing points.
103 2
        $this->initStorage();
104
105 2
        return true;
106
    }
107
108 7
    private function initStorage()
109
    {
110 7
        $this->storage = [
111 7
            static::STORAGE_KEY_UDP => [],
112 7
            static::STORAGE_KEY_HTTP => [],
113
        ];
114 7
    }
115
116
    /**
117
     * @param Point[] $points
118
     * @param string  $precision
119
     */
120 2
    private function writeUdpPoints(array $points, string $precision)
121
    {
122 2
        $this->udpDatabase->writePoints($points, $precision);
123 2
    }
124
125
    /**
126
     * @param Point[] $points
127
     * @param string  $precision
128
     */
129 2
    private function writeHttpPoints(array $points, string $precision)
130
    {
131 2
        $this->httpDatabase->writePoints($points, $precision);
132 2
    }
133
134
    /**
135
     * @param AbstractInfluxDbEvent $event
136
     */
137 2
    private function addEventPointsToStorage(AbstractInfluxDbEvent $event)
138
    {
139 2
        $typeKey = $event instanceof DeferredUdpEvent ? static::STORAGE_KEY_UDP : static::STORAGE_KEY_HTTP;
140 2
        $precision = $event->getPrecision();
141 2
        $points = $event->getPoints();
142
143 2
        if (array_key_exists($precision, $this->storage[$typeKey])) {
144 2
            $this->storage[$typeKey][$precision] = array_merge($this->storage[$typeKey][$precision], $points);
145
146 2
            return;
147
        }
148
149 2
        $this->storage[$typeKey][$precision] = $points;
150 2
    }
151
152
    /**
153
     * @param AbstractInfluxDbEvent $event
154
     *
155
     * @return bool
156
     */
157 7
    private function isConcerned(AbstractInfluxDbEvent $event): bool
158
    {
159 7
        return $this->connection === $event->getConnection() || is_null($event->getConnection()) && $this->isDefault;
160
    }
161
162
    /**
163
     * @param AbstractInfluxDbEvent $event
164
     */
165 6
    private function testUdpConnection(AbstractInfluxDbEvent $event)
166
    {
167 6
        if (!$this->udpDatabase && ($event instanceof UdpEvent || $event instanceof DeferredUdpEvent)) {
168 2
            throw new \RuntimeException(
169 2
                'No UDP connection available for database "'.$this->httpDatabase->getName().'". '
170 2
                .'You must enable it on the configuration to use it.'
171
            );
172
        }
173 4
    }
174
175
    /**
176
     * @param AbstractInfluxDbEvent $event
177
     */
178 2
    private function writeEventPoints(AbstractInfluxDbEvent $event)
179
    {
180 2
        $points = $event->getPoints();
181 2
        $precision = $event->getPrecision();
182
183 2
        if ($event instanceof UdpEvent) {
184 1
            $this->writeUdpPoints($points, $precision);
185
186 1
            return;
187
        }
188
189 1
        $this->writeHttpPoints($points, $precision);
190 1
    }
191
}
192