|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PamiModule\Listener; |
|
4
|
|
|
|
|
5
|
|
|
use Zend\EventManager\EventInterface; |
|
6
|
|
|
use Zend\EventManager\EventManagerInterface; |
|
7
|
|
|
use Zend\EventManager\ListenerAggregateInterface; |
|
8
|
|
|
use Zend\EventManager\ListenerAggregateTrait; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class ConnectionStatusListener. |
|
12
|
|
|
*/ |
|
13
|
|
|
class ConnectionStatusListener implements ListenerAggregateInterface |
|
14
|
|
|
{ |
|
15
|
|
|
use ListenerAggregateTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var bool |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $connected = false; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Attach one or more listeners. |
|
24
|
|
|
* |
|
25
|
|
|
* Implementors may add an optional $priority argument; the EventManager |
|
26
|
|
|
* implementation will pass this to the aggregate. |
|
27
|
|
|
* |
|
28
|
|
|
* @param EventManagerInterface $events |
|
29
|
|
|
* @param int $priority |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public function attach(EventManagerInterface $events, $priority = 1) |
|
32
|
|
|
{ |
|
33
|
1 |
|
$this->listeners[] = $events->attach('connect.pre', [$this, 'onConnect'], $priority); |
|
34
|
1 |
|
$this->listeners[] = $events->attach('disconnect.pre', [$this, 'onDisconnect'], $priority); |
|
35
|
1 |
|
$this->listeners[] = $events->attach('process.pre', [$this, 'onProcess'], $priority); |
|
36
|
1 |
|
$this->listeners[] = $events->attach('sendAction.pre', [$this, 'onSendAction'], $priority); |
|
37
|
1 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Triggered on connect. |
|
41
|
|
|
* |
|
42
|
|
|
* @param EventInterface $event The triggered event |
|
43
|
|
|
*/ |
|
44
|
4 |
|
public function onConnect(EventInterface $event) |
|
45
|
|
|
{ |
|
46
|
4 |
|
if ($this->connected) { |
|
47
|
1 |
|
$event->stopPropagation(true); |
|
48
|
1 |
|
} |
|
49
|
4 |
|
$this->connected = true; |
|
50
|
4 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Triggered on disconnect. |
|
54
|
|
|
* |
|
55
|
|
|
* @param EventInterface $event The triggered event |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function onDisconnect(EventInterface $event) |
|
58
|
|
|
{ |
|
59
|
1 |
|
if (!$this->connected) { |
|
60
|
1 |
|
$event->stopPropagation(true); |
|
61
|
1 |
|
} else { |
|
62
|
1 |
|
$this->connected = false; |
|
63
|
|
|
} |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Triggered on process. |
|
68
|
|
|
* |
|
69
|
|
|
* @param EventInterface $event The triggered event |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function onProcess(EventInterface $event) |
|
72
|
|
|
{ |
|
73
|
1 |
|
if (!$this->connected) { |
|
74
|
|
|
/** @var \PamiModule\Service\Client $client */ |
|
75
|
1 |
|
$client = $event->getTarget(); |
|
76
|
1 |
|
$client->connect(); |
|
77
|
1 |
|
} |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Triggered on sendAction. |
|
82
|
|
|
* |
|
83
|
|
|
* @param EventInterface $event The triggered event |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public function onSendAction(EventInterface $event) |
|
86
|
|
|
{ |
|
87
|
1 |
|
if (!$this->connected) { |
|
88
|
|
|
/** @var \PamiModule\Service\Client $client */ |
|
89
|
1 |
|
$client = $event->getTarget(); |
|
90
|
1 |
|
$client->connect(); |
|
91
|
1 |
|
} |
|
92
|
1 |
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|