Completed
Push — master ( b2dfe1...1412d2 )
by Sam
02:32
created

Application::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Jalle19\StatusManager;
4
5
use Jalle19\StatusManager\Configuration\Configuration;
6
use Jalle19\StatusManager\Event\Events;
7
use Jalle19\StatusManager\Manager\InstanceStateManager;
8
use Jalle19\StatusManager\Manager\PersistenceManager;
9
use Jalle19\StatusManager\Manager\StatusManager;
10
use Jalle19\StatusManager\Manager\WebSocketManager;
11
use Psr\Log\LoggerInterface;
12
use React\EventLoop\Factory as EventLoopFactory;
13
use Symfony\Component\EventDispatcher\EventDispatcher;
14
15
/**
16
 * Main application object. The main purpose of this class is to hold the configuration, the logger and the dispatcher.
17
 * Events are dispatched to the various managers so they can ultimately work together without contacting each other
18
 * directly.
19
 *
20
 * @package   Jalle19\StatusManager
21
 * @copyright Copyright &copy; Sam Stenvall 2016-
22
 * @license   https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0
23
 */
24
class Application
25
{
26
27
	/**
28
	 * @var Configuration
29
	 */
30
	private $_configuration;
31
32
	/**
33
	 * @var LoggerInterface
34
	 */
35
	private $_logger;
36
37
	/**
38
	 * @var EventDispatcher
39
	 */
40
	private $_eventDispatcher;
41
42
	/**
43
	 * @var StatusManager
44
	 */
45
	private $_statusManager;
46
47
	/**
48
	 * @var InstanceStateManager
49
	 */
50
	private $_instanceStateManager;
51
52
	/**
53
	 * @var WebSocketManager
54
	 */
55
	private $_webSocketManager;
56
57
	/**
58
	 * @var PersistenceManager
59
	 */
60
	private $_persistenceManager;
61
62
63
	/**
64
	 * Application constructor.
65
	 *
66
	 * @param Configuration   $configuration
67
	 * @param LoggerInterface $logger
68
	 */
69
	public function __construct(Configuration $configuration, LoggerInterface $logger)
70
	{
71
		$this->_configuration = $configuration;
72
		$this->_logger        = $logger;
73
	}
74
75
76
	/**
77
	 * Runs the application
78
	 */
79
	public function run()
80
	{
81
		$eventLoop = EventLoopFactory::create();
82
83
		// Configure managers
84
		$this->_statusManager        = new StatusManager($this);
85
		$this->_instanceStateManager = new InstanceStateManager($this);
86
		$this->_webSocketManager     = new WebSocketManager($this, $eventLoop);
87
		$this->_persistenceManager   = new PersistenceManager($this);
88
89
		$this->configureEventDispatcher();
90
91
		// Configure the event loop and start the application
92
		$eventLoop->addPeriodicTimer($this->_configuration->getUpdateInterval(),
1 ignored issue
show
Documentation introduced by
$this->_configuration->getUpdateInterval() is of type double, but the function expects a object<React\EventLoop\numeric>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93
			[$this->_statusManager, 'requestInstances']);
94
95
		$this->_eventDispatcher->dispatch(Events::MAIN_LOOP_STARTING);
96
		$eventLoop->run();
97
	}
98
99
100
	/**
101
	 * @return Configuration
102
	 */
103
	public function getConfiguration()
104
	{
105
		return $this->_configuration;
106
	}
107
108
109
	/**
110
	 * @return LoggerInterface
111
	 */
112
	public function getLogger()
113
	{
114
		return $this->_logger;
115
	}
116
117
118
	/**
119
	 * @return EventDispatcher
120
	 */
121
	public function getEventDispatcher()
122
	{
123
		return $this->_eventDispatcher;
124
	}
125
126
127
	/**
128
	 * Configures the event dispatcher and attaches event listeners to it
129
	 */
130
	private function configureEventDispatcher()
131
	{
132
		$this->_eventDispatcher = new EventDispatcher();
133
134
		$eventDefinitions = [
135
			[Events::MAIN_LOOP_STARTING, $this->_statusManager, 'onMainLoopStarted'],
136
			[Events::MAIN_LOOP_STARTING, $this->_persistenceManager, 'onMainLoopStarted'],
137
			[Events::MAIN_LOOP_STARTING, $this->_webSocketManager, 'onMainLoopStarted'],
138
			[Events::INSTANCE_COLLECTION_REQUEST, $this->_instanceStateManager, 'onInstanceCollectionRequest'],
139
			[Events::INSTANCE_COLLECTION, $this->_statusManager, 'onInstanceCollection'],
140
			[Events::INSTANCE_STATUS_UPDATES, $this->_webSocketManager, 'onInstanceStatusUpdates'],
141
			[Events::INSTANCE_STATE_REACHABLE, $this->_instanceStateManager, 'onInstanceReachable'],
142
			[Events::INSTANCE_STATE_UNREACHABLE, $this->_instanceStateManager, 'onInstanceUnreachable'],
143
			[Events::INSTANCE_STATE_MAYBE_REACHABLE, $this->_instanceStateManager, 'onInstanceMaybeReachable'],
144
			[Events::INSTANCE_SEEN, $this->_persistenceManager, 'onInstanceSeen'],
145
			[Events::CONNECTION_SEEN, $this->_persistenceManager, 'onConnectionSeen'],
146
			[Events::INPUT_SEEN, $this->_persistenceManager, 'onInputSeen'],
147
			[Events::SUBSCRIPTION_SEEN, $this->_persistenceManager, 'onSubscriptionSeen'],
148
			[Events::SUBSCRIPTION_STATE_CHANGE, $this->_persistenceManager, 'onSubscriptionStateChange'],
149
		];
150
151
		foreach ($eventDefinitions as $eventDefinition)
152
			$this->_eventDispatcher->addListener($eventDefinition[0], [$eventDefinition[1], $eventDefinition[2]]);
153
	}
154
155
}
156