Completed
Push — master ( 002884...295803 )
by Alessandro
02:11
created

InfluxDbEventListener   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 106
ccs 38
cts 38
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A onPointsCollected() 0 16 2
A onKernelTerminate() 0 11 3
A writePoints() 0 10 3
A initCollection() 0 10 3
A mergeCollections() 0 12 1
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
        $points = $event->getPoints();
42
43 4
        if ($event instanceof DeferredInfluxDbEvent) {
44
45 2
            $this->initCollection($event->getWriteMode(),$points->getPrecision());
46 2
            $this->mergeCollections($event->getWriteMode(), $points);
47
48 2
            return true;
49
        }
50
51 2
        $this->writePoints($event->getWriteMode(), $points);
52
53 2
        return true;
54
    }
55
56
    /**
57
     * @param Event $event
58
     * @return bool
59
     */
60 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...
61
    {
62 2
        foreach ($this->pointCollections as $writeMode => $precisionGroup) {
63
            /** @var PointsCollection $pointsCollection */
64 2
            foreach ($precisionGroup as $precision => $pointsCollection) {
65 2
                $this->writePoints($writeMode, $pointsCollection);
66
            }
67
        }
68
69 2
        return true;
70
    }
71
72
    /**
73
     * @param string $writemode
74
     * @param $points
75
     */
76 4
    private function writePoints(string $writemode, $points)
77
    {
78 4
        if ($writemode === ClientInterface::UDP_CLIENT) {
79 2
            $this->udpWriter->write($points);
80
        }
81
82 4
        if ($writemode === ClientInterface::HTTP_CLIENT) {
83 2
            $this->httpWriter->write($points);
84
        }
85 4
    }
86
87
    /**
88
     * @param string $getWriteMode
89
     * @param string $getPrecision
90
     */
91 2
    private function initCollection(string $getWriteMode, string $getPrecision)
92
    {
93 2
        if (! isset($this->pointCollections[$getWriteMode])) {
94 2
            $this->pointCollections[$getWriteMode] = null;
95
        }
96
97 2
        if (!isset($this->pointCollections[$getWriteMode][$getPrecision])) {
98 2
            $this->pointCollections[$getWriteMode][$getPrecision] = new PointsCollection([], $getPrecision);
99
        }
100 2
    }
101
102
    /**
103
     * @param string $writemode
104
     * @param PointsCollection $points
105
     * @internal param InfluxDbEvent $event
106
     */
107 2
    private function mergeCollections(string $writemode, PointsCollection $points)
108
    {
109
        /** @var PointsCollection $actualCollection */
110 2
        $actualCollection = $this->pointCollections[$writemode][$points->getPrecision()];
111
112 2
        $mergedCollection = new PointsCollection(array_merge(
113 2
            $actualCollection->toArray(),
114 2
            $points->toArray()
115 2
        ), $points->getPrecision());
116
117 2
        $this->pointCollections[$writemode][$points->getPrecision()] = $mergedCollection;
118 2
    }
119
120
}
121