Completed
Push — master ( 3af321...67fa71 )
by Ehsan
02:53
created

CommandExtractor::getConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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