1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jalle19\StatusManager\Manager; |
4
|
|
|
|
5
|
|
|
use Jalle19\StatusManager\Configuration\Configuration; |
6
|
|
|
use Jalle19\StatusManager\Database\Input; |
7
|
|
|
use Jalle19\StatusManager\Database\InputQuery; |
8
|
|
|
use Jalle19\StatusManager\Event\Events; |
9
|
|
|
use Jalle19\StatusManager\Event\InputSeenEvent; |
10
|
|
|
use Jalle19\StatusManager\Event\PersistInputErrorEvent; |
11
|
|
|
use Jalle19\StatusManager\Instance\InputErrorCumulative; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
14
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class InputErrorManager |
18
|
|
|
* @package Jalle19\StatusManager\Manager |
19
|
|
|
* @copyright Copyright © Sam Stenvall 2016- |
20
|
|
|
* @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0 |
21
|
|
|
*/ |
22
|
|
|
class InputErrorManager extends AbstractManager implements EventSubscriberInterface |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \SplObjectStorage |
27
|
|
|
*/ |
28
|
|
|
private $_inputErrorCollection; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
*/ |
34
|
|
|
public function __construct(Configuration $configuration, LoggerInterface $logger, EventDispatcher $eventDispatcher) |
35
|
|
|
{ |
36
|
|
|
parent::__construct($configuration, $logger, $eventDispatcher); |
37
|
|
|
|
38
|
|
|
$this->_inputErrorCollection = new \SplObjectStorage(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
|
|
public static function getSubscribedEvents() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
Events::INPUT_SEEN => [ |
49
|
|
|
// Ensure this runs after all other listeners |
50
|
|
|
['onInputSeen', -100], |
51
|
|
|
], |
52
|
|
|
Events::MAIN_LOOP_TICK => 'onMainLoopTick', |
53
|
|
|
Events::SUBSCRIPTION_STATE_CHANGE => 'onSubscriptionStateChange', |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param InputSeenEvent $event |
60
|
|
|
*/ |
61
|
|
|
public function onInputSeen(InputSeenEvent $event) |
62
|
|
|
{ |
63
|
|
|
$inputStatus = $event->getInputStatus(); |
64
|
|
|
$input = InputQuery::create()->findOneByUuid($inputStatus->uuid); |
65
|
|
|
|
66
|
|
|
// This should definitely never happen |
67
|
|
|
if ($input === null) |
68
|
|
|
return; |
69
|
|
|
|
70
|
|
|
if (!$this->_inputErrorCollection->contains($input)) |
71
|
|
|
$this->_inputErrorCollection->attach($input, new InputErrorCumulative()); |
72
|
|
|
|
73
|
|
|
/* @var InputErrorCumulative $inputErrors */ |
74
|
|
|
$inputErrors = $this->_inputErrorCollection[$input]; |
75
|
|
|
$inputErrors->accumulate($inputStatus); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Triggers PERSIST_INPUT_ERROR events for all inputs older than one minute |
81
|
|
|
*/ |
82
|
|
|
public function onMainLoopTick() |
83
|
|
|
{ |
84
|
|
|
foreach ($this->_inputErrorCollection as $input) |
85
|
|
|
{ |
86
|
|
|
/* @var InputErrorCumulative $inputErrors */ |
87
|
|
|
$inputErrors = $this->_inputErrorCollection->getInfo(); |
88
|
|
|
|
89
|
|
|
/* @var Input $input */ |
90
|
|
|
if ($inputErrors->getCreated() < new \DateTime('-1 minute')) |
91
|
|
|
$this->handleInputErrors($input, $inputErrors); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Triggers PERSIST_INPUT_ERROR events for all currently active inputs |
98
|
|
|
*/ |
99
|
|
|
public function onSubscriptionStateChange() |
100
|
|
|
{ |
101
|
|
|
foreach ($this->_inputErrorCollection as $input) |
102
|
|
|
{ |
103
|
|
|
/* @var Input $input */ |
104
|
|
|
$this->handleInputErrors($input, $this->_inputErrorCollection->getInfo()); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param Input $input |
111
|
|
|
* @param InputErrorCumulative $inputErrors |
112
|
|
|
*/ |
113
|
|
|
private function handleInputErrors(Input $input, InputErrorCumulative $inputErrors) |
114
|
|
|
{ |
115
|
|
|
$this->eventDispatcher->dispatch(Events::PERSIST_INPUT_ERROR, |
116
|
|
|
new PersistInputErrorEvent($input, $inputErrors)); |
117
|
|
|
|
118
|
|
|
$this->_inputErrorCollection->detach($input); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|