Completed
Push — master ( a423c2...002884 )
by Alessandro
02:21
created

InfluxDbEventListener::initCollection()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 5
nc 4
nop 2
crap 3
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 15 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
declare(strict_types=1);
3
namespace Algatux\InfluxDbBundle\Events\Listeners;
4
use Algatux\InfluxDbBundle\Events\DeferredInfluxDbEvent;
5
use Algatux\InfluxDbBundle\Events\InfluxDbEvent;
6
use Algatux\InfluxDbBundle\Model\PointsCollection;
7
use Algatux\InfluxDbBundle\Services\Clients\Contracts\ClientInterface;
8
use Algatux\InfluxDbBundle\Services\Clients\WriterClient;
9
use Symfony\Component\EventDispatcher\Event;
10
11
/**
12
 * Class InfluxDbEventListener
13
 * @package Algatux\InfluxDbBundle\Events\Listeners
14
 */
15
class InfluxDbEventListener
16
{
17
18
    /** @var WriterClient  */
19
    private $httpWriter;
20
21
    /** @var WriterClient  */
22
    private $udpWriter;
23
24
    /** @var \SplQueue  */
25
    private $pointCollections;
26
27
    /**
28
     * InfluxDbEventListener constructor.
29
     * @param WriterClient $httpWriter
30
     * @param WriterClient $udpWriter
31
     */
32 4
    public function __construct(WriterClient $httpWriter, WriterClient $udpWriter)
33
    {
34 4
        $this->httpWriter = $httpWriter;
35 4
        $this->udpWriter = $udpWriter;
36 4
        $this->pointCollections = null;
37 4
    }
38
39 4
    public function onPointsCollected(InfluxDbEvent $event): bool
40
    {
41 4
        if ($event->isPropagationStopped()) {
42
            return false;
43
        }
44
45 4
        $points = $event->getPoints();
46
47 4
        if ($event instanceof DeferredInfluxDbEvent) {
48
49 2
            $this->initCollection($event->getWriteMode(),$points->getPrecision());
50
51
            /** @var PointsCollection $actualCollection */
52 2
            $actualCollection = $this->pointCollections[$event->getWriteMode()][$points->getPrecision()];
53
54 2
            $mergedCollection = new PointsCollection(array_merge(
55 2
                $actualCollection->toArray(),
56 2
                $points->toArray()
57 2
            ), $points->getPrecision());
58
59 2
            $this->pointCollections[$event->getWriteMode()][$points->getPrecision()] = $mergedCollection;
60
61 2
            return true;
62
        }
63
64 2
        $this->writePoints($event->getWriteMode(), $points);
65
66 2
        return true;
67
    }
68
69
    /**
70
     * @param Event $event
71
     * @return bool
72
     */
73 2
    public function onKernelTerminate(Event $event): bool
74
    {
75 2
        if ($event->isPropagationStopped()) {
76
            return false;
77
        }
78
79 2
        if (! empty($this->pointCollections)) {
80
81 2
            foreach ($this->pointCollections as $writeMode => $precisionGroup) {
82
                /** @var PointsCollection $pointsCollection */
83 2
                foreach ($precisionGroup as $precision => $pointsCollection) {
84 2
                    $this->writePoints($writeMode, $pointsCollection);
85
                }
86
            }
87
88
        }
89
90 2
        return true;
91
    }
92
93
    /**
94
     * @param string $writemode
95
     * @param $points
96
     */
97 4
    private function writePoints(string $writemode, $points)
98
    {
99 4
        if ($writemode === ClientInterface::UDP_CLIENT) {
100 2
            $this->udpWriter->write($points);
101
        }
102
103 4
        if ($writemode === ClientInterface::HTTP_CLIENT) {
104 2
            $this->httpWriter->write($points);
105
        }
106 4
    }
107
108
    /**
109
     * @param string $getWriteMode
110
     * @param string $getPrecision
111
     */
112 2
    private function initCollection(string $getWriteMode, string $getPrecision)
113
    {
114 2
        if (! isset($this->pointCollections[$getWriteMode])) {
115 2
            $this->pointCollections[$getWriteMode] = null;
116
        }
117
118 2
        if (!isset($this->pointCollections[$getWriteMode][$getPrecision])) {
119 2
            $this->pointCollections[$getWriteMode][$getPrecision] = new PointsCollection([], $getPrecision);
120
        }
121 2
    }
122
123
}
124