Completed
Push — master ( 4ba672...13e9af )
by Rémi
04:12
created

MessageApplication::handleCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace MessageApp;
4
5
use League\Tactician\CommandBus;
6
use League\Tactician\Plugins\NamedCommand\NamedCommand;
7
use MessageApp\Message\DefaultMessage;
8
use MessageApp\Message\MessageFactory;
9
use MessageApp\Message\Sender\MessageSender;
10
use MessageApp\Parser\Exception\MessageParserException;
11
use MessageApp\Parser\MessageParser;
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([$e->getUser()], $e);
88
89 6
            if (!$errorMessage) {
90 3
                $this->logger->warning('Message could not be generated');
91 3
                return null;
92
            }
93
94 3
            $this->messageSender->send($errorMessage, $message);
95 3
            return null;
96
        }
97
    }
98
99
    /**
100
     * Handles the command
101
     *
102
     * @param NamedCommand $command
103
     */
104 12
    private function handleCommand(NamedCommand $command = null)
105
    {
106 12
        if ($command === null) {
107 9
            $this->logger->info('Message ignored');
108 9
            return;
109
        }
110
111 3
        $this->commandBus->handle($command);
112 3
    }
113
}
114