Passed
Push — master ( e7123a...24f2e4 )
by Ehsan
02:39
created

CommandExtractor::getError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Botonomous;
4
5
use Botonomous\utility\MessageUtility;
6
7
/**
8
 * Class CommandExtractor.
9
 */
10
class CommandExtractor
11
{
12
    private $config;
13
    private $error;
14
    private $messageUtility;
15
    private $dictionary;
16
    private $commandContainer;
17
18
    /**
19
     * @param null $message
20
     *
21
     * @return Command|void
22
     */
23 7
    public function getCommandByMessage($message)
24
    {
25 7
        if (empty($message)) {
26 1
            $this->setError('Message is empty');
27
28 1
            return;
29
        }
30
31
        /*
32
         * Process the message.
33
         */
34 6
        return $this->getCommandObjectByMessage($message);
35
    }
36
37
    /**
38
     * @param $message
39
     *
40
     * @return Command|void
41
     */
42 6
    private function getCommandObjectByMessage($message)
43
    {
44 6
        $command = $this->getMessageUtility()->extractCommandName($message);
45
46
        // check command name
47 6
        if (empty($command)) {
48
            // get the default command if no command is find in the message
49 2
            $command = $this->getConfig()->get('defaultCommand');
50
51 2
            if (empty($command)) {
52 2
                $this->setError($this->getDictionary()->get('generic-messages')['noCommandMessage']);
53
54 2
                return;
55
            }
56
        }
57
58 4
        return $this->getCommandObjectByCommand($command);
59
    }
60
61
    /**
62
     * @param $command
63
     *
64
     * @return Command|void
65
     */
66 4
    private function getCommandObjectByCommand($command)
67
    {
68 4
        $commandObject = $this->getCommandContainer()->getAsObject($command);
69
70 4
        if ($this->validateCommandObject($commandObject) !== true) {
71 1
            return;
72
        }
73
74 4
        return $commandObject;
75
    }
76
77
    /**
78
     * Validate the command object.
79
     *
80
     * @param Command|null $commandObject
81
     *
82
     * @return bool
83
     */
84 4
    private function validateCommandObject($commandObject)
85
    {
86
        // check command details
87 4
        if (empty($commandObject)) {
88 1
            $this->setError(
89 1
                $this->getDictionary()->getValueByKey('generic-messages', 'unknownCommandMessage')
90
            );
91
92 1
            return false;
93
        }
94
95 4
        return true;
96
    }
97
98
    /**
99
     * @return string
100
     */
101 4
    public function getError()
102
    {
103 4
        return $this->error;
104
    }
105
106
    /**
107
     * @param string $error
108
     */
109 4
    public function setError($error)
110
    {
111 4
        $this->error = $error;
112 4
    }
113
114
    /**
115
     * @return Config
116
     */
117 2
    public function getConfig()
118
    {
119 2
        if ($this->config === null) {
120 2
            $this->config = (new Config());
121
        }
122
123 2
        return $this->config;
124
    }
125
126
    /**
127
     * @param Config $config
128
     */
129
    public function setConfig(Config $config)
130
    {
131
        $this->config = $config;
132
    }
133
134
    /**
135
     * @return MessageUtility
136
     */
137 6
    public function getMessageUtility()
138
    {
139 6
        if (!isset($this->messageUtility)) {
140 6
            $this->setMessageUtility(new MessageUtility());
141
        }
142
143 6
        return $this->messageUtility;
144
    }
145
146
    /**
147
     * @param MessageUtility $messageUtility
148
     */
149 6
    public function setMessageUtility(MessageUtility $messageUtility)
150
    {
151 6
        $this->messageUtility = $messageUtility;
152 6
    }
153
154
    /**
155
     * @return Dictionary
156
     */
157 3
    public function getDictionary()
158
    {
159 3
        if (!isset($this->dictionary)) {
160 3
            $this->setDictionary(new Dictionary());
161
        }
162
163 3
        return $this->dictionary;
164
    }
165
166
    /**
167
     * @param Dictionary $dictionary
168
     */
169 3
    public function setDictionary(Dictionary $dictionary)
170
    {
171 3
        $this->dictionary = $dictionary;
172 3
    }
173
174
    /**
175
     * @return CommandContainer
176
     */
177 4
    public function getCommandContainer()
178
    {
179 4
        if (!isset($this->commandContainer)) {
180 4
            $this->setCommandContainer(new CommandContainer());
181
        }
182
183 4
        return $this->commandContainer;
184
    }
185
186
    /**
187
     * @param CommandContainer $commandContainer
188
     */
189 4
    public function setCommandContainer(CommandContainer $commandContainer)
190
    {
191 4
        $this->commandContainer = $commandContainer;
192 4
    }
193
}
194