Completed
Push — master ( 61c032...c23418 )
by Alessandro
03:27
created

InfluxDbEventListener::onPointsCollected()   C

Complexity

Conditions 7
Paths 15

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 27
ccs 15
cts 15
cp 1
rs 6.7272
cc 7
eloc 15
nc 15
nop 1
crap 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Algatux\InfluxDbBundle\Events\Listeners;
6
7
use Algatux\InfluxDbBundle\Events\DeferredInfluxDbEvent;
8
use Algatux\InfluxDbBundle\Events\DeferredUdpEvent;
9
use Algatux\InfluxDbBundle\Events\InfluxDbEvent;
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
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 5
    public function __construct(string $connection, bool $isDefault, Database $httpDatabase, Database $udpDatabase)
55
    {
56 5
        $this->connection = $connection;
57 5
        $this->isDefault = $isDefault;
58 5
        $this->httpDatabase = $httpDatabase;
59 5
        $this->udpDatabase = $udpDatabase;
60 5
        $this->initStorage();
61 5
    }
62
63 5
    public function onPointsCollected(InfluxDbEvent $event): bool
64
    {
65 5
        $isConcerned = $this->connection === $event->getConnection() || !$event->getConnection() && $this->isDefault;
0 ignored issues
show
Bug Best Practice introduced by
The expression $event->getConnection() of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
66 5
        if (!$isConcerned) {
67 1
            return false;
68
        }
69
70 4
        $points = $event->getPoints();
71 4
        $precision = $event->getPrecision();
72
73 4
        if ($event instanceof DeferredInfluxDbEvent) {
74 2
            $typeKey = $event instanceof DeferredUdpEvent ? static::STORAGE_KEY_UDP : static::STORAGE_KEY_HTTP;
75 2
            $this->addPointsToStorage($typeKey, $precision, $points);
76
77 2
            return true;
78
        }
79
80 2
        if ($event instanceof UdpEvent) {
81 1
            $this->writeUdpPoints($points, $precision);
82
83 1
            return true;
84
        }
85
86 1
        $this->writeHttpPoints($points, $precision);
87
88 1
        return true;
89
    }
90
91
    /**
92
     * @param Event $event
93
     *
94
     * @return bool
95
     */
96 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...
97
    {
98 2
        foreach ($this->storage[static::STORAGE_KEY_UDP] as $precision => $points) {
99 1
            $this->writeUdpPoints($points, $precision);
100
        }
101
102 2
        foreach ($this->storage[static::STORAGE_KEY_HTTP] as $precision => $points) {
103 1
            $this->writeHttpPoints($points, $precision);
104
        }
105
106
        // Reset the storage after writing points.
107 2
        $this->initStorage();
108
109 2
        return true;
110
    }
111
112 5
    private function initStorage()
113
    {
114 5
        $this->storage = [
115 5
            static::STORAGE_KEY_UDP => [],
116 5
            static::STORAGE_KEY_HTTP => [],
117
        ];
118 5
    }
119
120
    /**
121
     * @param Point[] $points
122
     * @param string  $precision
123
     */
124 2
    private function writeUdpPoints(array $points, string $precision)
125
    {
126 2
        $this->udpDatabase->writePoints($points, $precision);
127 2
    }
128
129
    /**
130
     * @param Point[] $points
131
     * @param string  $precision
132
     */
133 2
    private function writeHttpPoints(array $points, string $precision)
134
    {
135 2
        $this->httpDatabase->writePoints($points, $precision);
136 2
    }
137
138
    /**
139
     * @param string $typeKey
140
     * @param string $precision
141
     * @param array  $points
142
     */
143 2
    private function addPointsToStorage(string $typeKey, string $precision, array $points)
144
    {
145 2
        if (array_key_exists($precision, $this->storage[$typeKey])) {
146 2
            $this->storage[$typeKey][$precision] = array_merge($this->storage[$typeKey][$precision], $points);
147
148 2
            return;
149
        }
150
151 2
        $this->storage[$typeKey][$precision] = $points;
152 2
    }
153
}
154