QA::setQuestions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
     * @throws \Exception
17
     *
18
     * @return string
19
     */
20 3
    public function index()
21
    {
22 3
        $questions = $this->getQuestions();
23 3
        $stringUtility = new StringUtility();
24
25 3
        if (empty($questions)) {
26 1
            return '';
27
        }
28
29 2
        foreach ($questions as $question => $questionInfo) {
30 2
            if ($stringUtility->findInString($question, $this->getSlackbot()->getListener()->getMessage())) {
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
     * @throws \Exception
43
     *
44
     * @return array
45
     */
46 3
    public function getQuestions(): array
47
    {
48 3
        if (!isset($this->questions)) {
49 2
            $this->setQuestions($this->getDictionary()->get('question-answer'));
50
        }
51
52 3
        return $this->questions;
53
    }
54
55
    /**
56
     * @param array $questions
57
     */
58 3
    public function setQuestions(array $questions)
59
    {
60 3
        $this->questions = $questions;
61 3
    }
62
}
63