1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the slince/spike package. |
5
|
|
|
* |
6
|
|
|
* (c) Slince <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Spike\Client\Listener; |
13
|
|
|
|
14
|
|
|
use Slince\EventDispatcher\Event; |
15
|
|
|
use Slince\EventDispatcher\SubscriberInterface; |
16
|
|
|
use Spike\Client\Client; |
17
|
|
|
use Spike\Client\Event\Events; |
18
|
|
|
use Spike\Client\Event\FilterActionHandlerEvent; |
19
|
|
|
use Spike\Common\Logger\Logger; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @codeCoverageIgnore |
23
|
|
|
*/ |
24
|
|
|
class LoggerListener implements SubscriberInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var Client |
28
|
|
|
*/ |
29
|
|
|
protected $client; |
30
|
|
|
|
31
|
|
|
public function __construct(Client $client) |
32
|
|
|
{ |
33
|
|
|
$this->client = $client; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return Logger |
38
|
|
|
*/ |
39
|
|
|
protected function getLogger() |
40
|
|
|
{ |
41
|
|
|
return $this->client->getLogger(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public static function getSubscribedEvents() |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
|
|
Events::CLIENT_RUN => 'onClientRun', |
51
|
|
|
Events::CLIENT_CONNECT => 'onConnectToServer', |
52
|
|
|
Events::CANNOT_CONNECT_SERVER => 'onCannotConnectToServer', |
53
|
|
|
Events::CLIENT_ACTION => 'onReceiveMessage', |
54
|
|
|
Events::CONNECTION_ERROR => 'onConnectionError', |
55
|
|
|
Events::AUTH_ERROR => 'onAuthError', |
56
|
|
|
Events::AUTH_SUCCESS => 'onAuthSuccess', |
57
|
|
|
Events::REGISTER_TUNNEL_ERROR => 'onRegisterTunnelError', |
58
|
|
|
Events::DISCONNECT_FROM_SERVER => 'onDisconnectFromServer', |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function onAuthError(Event $event) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$this->getLogger()->error('Auth error, please check your configuration file'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function onAuthSuccess(Event $event) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$this->getLogger()->info('Auth success'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function onRegisterTunnelError(Event $event) |
73
|
|
|
{ |
74
|
|
|
$this->getLogger()->error(sprintf('Registers tunnel "%s" error, message: "%s"', |
75
|
|
|
$event->getArgument('tunnel'), |
76
|
|
|
$event->getArgument('errorMessage') |
77
|
|
|
)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function onDisconnectFromServer(Event $event) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$this->getLogger()->error('Disconnect from the server'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function onReceiveMessage(FilterActionHandlerEvent $event) |
86
|
|
|
{ |
87
|
|
|
$this->getLogger()->info("Received a message:\r\n".$event->getMessage()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function onClientRun(Event $event) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$this->getLogger()->info('The client is running ...'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function onConnectionError(Event $event) |
96
|
|
|
{ |
97
|
|
|
$this->getLogger()->warning(sprintf('Got a bad protocol message: "%s" from "%s"', |
98
|
|
|
$event->getArgument('exception')->getMessage(), |
99
|
|
|
$event->getArgument('connection')->getRemoteAddress() |
100
|
|
|
)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function onConnectToServer(Event $event) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$this->getLogger()->info('The client has connected to the server.'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function onCannotConnectToServer(Event $event) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$this->getLogger()->error('Cannot connect to the server. the server may not be available'); |
111
|
|
|
} |
112
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.