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

QA   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 50
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B index() 0 22 4
A getQuestions() 0 8 2
A setQuestions() 0 4 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
     * @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