1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spike library |
4
|
|
|
* @author Tao <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Spike\Client\Subscriber; |
7
|
|
|
|
8
|
|
|
use Slince\Event\Event; |
9
|
|
|
use Spike\Client\Application; |
10
|
|
|
use Spike\Client\EventStore; |
11
|
|
|
use Spike\Logger\Logger; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @codeCoverageIgnore |
16
|
|
|
*/ |
17
|
|
|
class LoggerSubscriber extends Subscriber |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Logger |
21
|
|
|
*/ |
22
|
|
|
protected $logger; |
23
|
|
|
|
24
|
|
|
public function __construct(Application $client) |
25
|
|
|
{ |
26
|
|
|
parent::__construct($client); |
27
|
|
|
$this->logger = $client->getLogger(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
public function getEvents() |
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
|
|
EventStore::CLIENT_RUN => 'onClientRun', |
35
|
|
|
EventStore::RECEIVE_MESSAGE => 'onReceiveMessage', |
36
|
|
|
EventStore::SOCKET_ERROR => 'onSocketError', |
37
|
|
|
EventStore::CONNECTION_ERROR => 'onConnectionError', |
38
|
|
|
EventStore::CONNECT_TO_SERVER => 'onConnectToServer', |
39
|
|
|
]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function onReceiveMessage(Event $event) |
43
|
|
|
{ |
44
|
|
|
$this->logger->info("Received a message:\r\n" . $event->getArgument('message')); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function onClientRun(Event $event) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$this->logger->info("The client is running ..."); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function onSocketError(Event $event) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$this->logger->error("Client error."); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function onConnectionError(Event $event) |
58
|
|
|
{ |
59
|
|
|
$this->logger->warning($event->getArgument('exception')->getMessage()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function onConnectToServer(Event $event) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$this->logger->info("The client has connected to the server."); |
65
|
|
|
} |
66
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.