|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MessageApp; |
|
4
|
|
|
|
|
5
|
|
|
use League\Tactician\CommandBus; |
|
6
|
|
|
use League\Tactician\Plugins\NamedCommand\NamedCommand; |
|
7
|
|
|
use MessageApp\Message\MessageFactory; |
|
8
|
|
|
use MessageApp\Message\Sender\MessageSender; |
|
9
|
|
|
use MessageApp\Parser\Exception\MessageParserException; |
|
10
|
|
|
use MessageApp\Parser\MessageParser; |
|
11
|
|
|
use MessageApp\User\UndefinedApplicationUser; |
|
12
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
13
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
14
|
|
|
use Psr\Log\NullLogger; |
|
15
|
|
|
|
|
16
|
|
|
class MessageApplication implements LoggerAwareInterface |
|
17
|
|
|
{ |
|
18
|
|
|
use LoggerAwareTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var MessageSender |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $messageSender; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var MessageParser |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $messageParser; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var MessageFactory |
|
32
|
|
|
*/ |
|
33
|
|
|
private $messageFactory; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var CommandBus |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $commandBus; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Constructor |
|
42
|
|
|
* |
|
43
|
|
|
* @param MessageSender $messageSender |
|
44
|
|
|
* @param MessageParser $messageParser |
|
45
|
|
|
* @param MessageFactory $messageFactory |
|
46
|
|
|
* @param CommandBus $commandBus |
|
47
|
|
|
*/ |
|
48
|
12 |
|
public function __construct( |
|
49
|
|
|
MessageSender $messageSender, |
|
50
|
|
|
MessageParser $messageParser, |
|
51
|
|
|
MessageFactory $messageFactory, |
|
52
|
|
|
CommandBus $commandBus |
|
53
|
|
|
) { |
|
54
|
12 |
|
$this->messageSender = $messageSender; |
|
55
|
12 |
|
$this->messageParser = $messageParser; |
|
56
|
12 |
|
$this->messageFactory = $messageFactory; |
|
57
|
12 |
|
$this->commandBus = $commandBus; |
|
58
|
12 |
|
$this->logger = new NullLogger(); |
|
59
|
12 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Handle a message |
|
63
|
|
|
* |
|
64
|
|
|
* @param mixed $message |
|
65
|
|
|
* @return void |
|
66
|
|
|
*/ |
|
67
|
12 |
|
public function handle($message) |
|
68
|
|
|
{ |
|
69
|
12 |
|
$this->logger->info($message); |
|
70
|
12 |
|
$command = $this->parseMessage($message); |
|
71
|
12 |
|
$this->handleCommand($command); |
|
72
|
12 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Parse the message |
|
76
|
|
|
* |
|
77
|
|
|
* @param mixed $message |
|
78
|
|
|
* @return NamedCommand |
|
79
|
|
|
*/ |
|
80
|
12 |
|
private function parseMessage($message) |
|
81
|
|
|
{ |
|
82
|
|
|
try { |
|
83
|
12 |
|
return $this->messageParser->parse($message); |
|
84
|
6 |
|
} catch (MessageParserException $e) { |
|
85
|
6 |
|
$this->logger->error('Error parsing or executing command', ['exception' => $e->getMessage()]); |
|
86
|
|
|
|
|
87
|
6 |
|
$errorMessage = $this->messageFactory->buildMessage( |
|
88
|
6 |
|
[ $this->getMessageUser($e) ], |
|
89
|
|
|
$e |
|
90
|
4 |
|
); |
|
91
|
|
|
|
|
92
|
6 |
|
if (!$errorMessage) { |
|
93
|
3 |
|
$this->logger->warning('Message could not be generated'); |
|
94
|
3 |
|
return null; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
3 |
|
$this->messageSender->send($errorMessage, $message); |
|
98
|
3 |
|
return null; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Handles the command |
|
104
|
|
|
* |
|
105
|
|
|
* @param NamedCommand $command |
|
106
|
|
|
*/ |
|
107
|
12 |
|
private function handleCommand(NamedCommand $command = null) |
|
108
|
|
|
{ |
|
109
|
12 |
|
if ($command === null) { |
|
110
|
9 |
|
$this->logger->info('Message ignored'); |
|
111
|
9 |
|
return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
3 |
|
$this->commandBus->handle($command); |
|
115
|
3 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param MessageParserException $exception |
|
119
|
|
|
* |
|
120
|
|
|
* @return UndefinedApplicationUser |
|
121
|
|
|
*/ |
|
122
|
6 |
|
private function getMessageUser(MessageParserException $exception) |
|
123
|
|
|
{ |
|
124
|
6 |
|
return new UndefinedApplicationUser($exception->getUser()->getAccount()); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|