1
|
|
|
<?php |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
|
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.