Passed
Push — master ( ff83e3...5f4a5c )
by Ehsan
03:54
created

QA::index()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 11
cts 11
cp 1
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 0
crap 4
1
<?php
2
3
namespace Botonomous\plugin\qa;
4
5
use Botonomous\plugin\AbstractPlugin;
6
use Botonomous\utility\StringUtility;
7
8
/**
9
 * Class QA.
10
 */
11
class QA extends AbstractPlugin
12
{
13
    private $questions;
14
15
    /**
16
     * @return string
17
     */
18 3
    public function index()
19
    {
20 3
        $questions = $this->getQuestions();
21
22 3
        $stringUtility = new StringUtility();
23 3
        $text = $this->getSlackbot()->getListener()->getMessage();
24
25 3
        if (empty($questions)) {
26 1
            return '';
27
        }
28
29 2
        foreach ($questions as $question => $questionInfo) {
30 2
            if ($stringUtility->findInString($question, $text)) {
31
                // found - return random answer
32 1
                $answers = $questionInfo['answers'];
33
34 2
                return $answers[array_rand($answers)];
35
            }
36
        }
37
38 1
        return '';
39
    }
40
41
    /**
42
     * @return array
43
     */
44 3
    public function getQuestions()
45
    {
46 3
        if (!isset($this->questions)) {
47 2
            $this->setQuestions($this->getDictionary()->get('question-answer'));
48
        }
49
50 3
        return $this->questions;
51
    }
52
53
    /**
54
     * @param array $questions
55
     */
56 3
    public function setQuestions(array $questions)
57
    {
58 3
        $this->questions = $questions;
59 3
    }
60
}
61