Completed
Push — master ( abe4a2...2abbc3 )
by Sam
05:40
created

InputErrorManager::handleInputErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
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 &copy; 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
			if ($inputErrors->getCreated() < new \DateTime('-1 minute'))
90
				$this->handleInputErrors($input, $inputErrors);
91
		}
92
	}
93
94
95
	/**
96
	 * Triggers PERSIST_INPUT_ERROR events for all currently active inputs
97
	 */
98
	public function onSubscriptionStateChange()
99
	{
100
		foreach ($this->_inputErrorCollection as $input)
101
			$this->handleInputErrors($input, $this->_inputErrorCollection->getInfo());
102
	}
103
104
105
	/**
106
	 * @param Input                $input
107
	 * @param InputErrorCumulative $inputErrors
108
	 */
109
	private function handleInputErrors(Input $input, InputErrorCumulative $inputErrors)
110
	{
111
		$this->eventDispatcher->dispatch(Events::PERSIST_INPUT_ERROR,
112
			new PersistInputErrorEvent($input, $inputErrors));
113
114
		$this->_inputErrorCollection->detach($input);
115
	}
116
117
}
118