Completed
Branch master (44133e)
by Alessandro
02:07
created

InfluxDbEventListener::mergeCollections()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 2
crap 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 16 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 Algatux\InfluxDbBundle\Services\PointsCollectionStorage;
10
use Symfony\Component\EventDispatcher\Event;
11
12
/**
13
 * Class InfluxDbEventListener
14
 * @package Algatux\InfluxDbBundle\Events\Listeners
15
 */
16
class InfluxDbEventListener
17
{
18
19
    /** @var WriterClient  */
20
    private $httpWriter;
21
22
    /** @var WriterClient  */
23
    private $udpWriter;
24
25
    /** @var PointsCollectionStorage  */
26
    private $collectionStorage;
27
28
    /**
29
     * InfluxDbEventListener constructor.
30
     * @param WriterClient $httpWriter
31
     * @param WriterClient $udpWriter
32
     * @param PointsCollectionStorage $collectionStorage
33
     */
34 4
    public function __construct(
35
        WriterClient $httpWriter,
36
        WriterClient $udpWriter,
37
        PointsCollectionStorage $collectionStorage
38
    )
39
    {
40 4
        $this->httpWriter = $httpWriter;
41 4
        $this->udpWriter = $udpWriter;
42 4
        $this->collectionStorage = $collectionStorage;
43 4
    }
44
45 4
    public function onPointsCollected(InfluxDbEvent $event): bool
46
    {
47 4
        $points = $event->getPoints();
48
49 4
        if ($event instanceof DeferredInfluxDbEvent) {
50
51 2
            $this->collectionStorage->storeCollection($points, $event->getWriteMode());
52
53 2
            return true;
54
        }
55
56 2
        $this->writePoints($event->getWriteMode(), $points);
57
58 2
        return true;
59
    }
60
61
    /**
62
     * @param Event $event
63
     * @return bool
64
     */
65 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...
66
    {
67 2
        $collections = $this->collectionStorage->getStoredCollections();
68
69 2
        foreach ($collections as $writeMode => $precisionGroup) {
70
            /** @var PointsCollection $pointsCollection */
71 2
            foreach ($precisionGroup as $precision => $pointsCollection) {
72 2
                $this->writePoints($writeMode, $pointsCollection);
73
            }
74
        }
75
76 2
        return true;
77
    }
78
79
    /**
80
     * @param string $writemode
81
     * @param $points
82
     */
83 4
    private function writePoints(string $writemode, $points)
84
    {
85 4
        if ($writemode === ClientInterface::UDP_CLIENT) {
86 2
            $this->udpWriter->write($points);
87
        }
88
89 4
        if ($writemode === ClientInterface::HTTP_CLIENT) {
90 2
            $this->httpWriter->write($points);
91
        }
92 4
    }
93
94
95
96
}
97