Passed
Push — master ( f9e4d8...212a05 )
by Ehsan
02:36
created

CommandExtractor::validateCommandObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
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 1
    public function calculateKeywordSimilarity($message)
38
    {
39 1
        $commandContainer = $this->getCommandContainer();
40 1
        $commands = $commandContainer->getAllAsObject();
41
42 1
        $similarities = [];
43 1
        foreach ($commands as $commandKey => $commandObject) {
44 1
            if (empty($commandObject->getKeywords())) {
45 1
                continue;
46
            }
47
48 1
            similar_text($message, implode(' ', $commandObject->getKeywords()), $percent);
49 1
            $similarities[$commandKey] = $percent;
50
        }
51
52 1
        return $similarities;
53
    }
54
55
    /**
56
     * @param $message
57
     *
58
     * @return Command|void
59
     */
60 6
    private function getCommandObjectByMessage($message)
61
    {
62 6
        $command = $this->getMessageUtility()->extractCommandName($message);
63
64
        // check command name
65 6
        if (empty($command)) {
66
            // get the default command if no command is find in the message
67 2
            $command = $this->getConfig()->get('defaultCommand');
68
69 2
            if (empty($command)) {
70 2
                $this->setError($this->getDictionary()->get('generic-messages')['noCommandMessage']);
71
72 2
                return;
73
            }
74
        }
75
76 4
        return $this->getCommandObjectByCommand($command);
77
    }
78
79
    /**
80
     * @param $command
81
     *
82
     * @return Command|void
83
     */
84 4
    private function getCommandObjectByCommand($command)
85
    {
86 4
        $commandObject = $this->getCommandContainer()->getAsObject($command);
87
88 4
        if ($this->validateCommandObject($commandObject) !== true) {
89 1
            return;
90
        }
91
92 4
        return $commandObject;
93
    }
94
95
    /**
96
     * Validate the command object.
97
     *
98
     * @param Command|null $commandObject
99
     *
100
     * @return bool
101
     */
102 4
    private function validateCommandObject($commandObject)
103
    {
104
        // check command details
105 4
        if (empty($commandObject)) {
106 1
            $this->setError(
107 1
                $this->getDictionary()->getValueByKey('generic-messages', 'unknownCommandMessage')
108
            );
109
110 1
            return false;
111
        }
112
113 4
        return true;
114
    }
115
116
    /**
117
     * @return string
118
     */
119 4
    public function getError()
120
    {
121 4
        return $this->error;
122
    }
123
124
    /**
125
     * @param string $error
126
     */
127 4
    public function setError($error)
128
    {
129 4
        $this->error = $error;
130 4
    }
131
132
    /**
133
     * @return Config
134
     */
135 3
    public function getConfig()
136
    {
137 3
        if ($this->config === null) {
138 2
            $this->config = (new Config());
139
        }
140
141 3
        return $this->config;
142
    }
143
144
    /**
145
     * @param Config $config
146
     */
147 1
    public function setConfig(Config $config)
148
    {
149 1
        $this->config = $config;
150 1
    }
151
152
    /**
153
     * @return MessageUtility
154
     */
155 6
    public function getMessageUtility()
156
    {
157 6
        if (!isset($this->messageUtility)) {
158 6
            $this->setMessageUtility(new MessageUtility());
159
        }
160
161 6
        return $this->messageUtility;
162
    }
163
164
    /**
165
     * @param MessageUtility $messageUtility
166
     */
167 6
    public function setMessageUtility(MessageUtility $messageUtility)
168
    {
169 6
        $this->messageUtility = $messageUtility;
170 6
    }
171
172
    /**
173
     * @return Dictionary
174
     */
175 3
    public function getDictionary()
176
    {
177 3
        if (!isset($this->dictionary)) {
178 3
            $this->setDictionary(new Dictionary());
179
        }
180
181 3
        return $this->dictionary;
182
    }
183
184
    /**
185
     * @param Dictionary $dictionary
186
     */
187 3
    public function setDictionary(Dictionary $dictionary)
188
    {
189 3
        $this->dictionary = $dictionary;
190 3
    }
191
192
    /**
193
     * @return CommandContainer
194
     */
195 5
    public function getCommandContainer()
196
    {
197 5
        if (!isset($this->commandContainer)) {
198 4
            $this->setCommandContainer(new CommandContainer());
199
        }
200
201 5
        return $this->commandContainer;
202
    }
203
204
    /**
205
     * @param CommandContainer $commandContainer
206
     */
207 5
    public function setCommandContainer(CommandContainer $commandContainer)
208
    {
209 5
        $this->commandContainer = $commandContainer;
210 5
    }
211
}
212