Completed
Push — master ( afce1d...f8221d )
by Vladimir
09:37
created

IntentManager::find()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 8.8571
cc 5
eloc 6
nc 4
nop 1
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Conversation;
6
7
use FondBot\Drivers\ReceivedMessage;
8
9
class IntentManager
10
{
11
    /** @var Intent[] */
12
    private $intents = [];
13
14
    /** @var Intent */
15
    private $fallbackIntent;
16
17
    /**
18
     * Register intents.
19
     *
20
     * @param array  $intents
21
     * @param string $fallbackIntent
22
     */
23
    public function register(array $intents, string $fallbackIntent): void
0 ignored issues
show
Unused Code introduced by
The parameter $intents is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fallbackIntent is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
24
    {
25
    }
26
27
    /**
28
     * Find intent.
29
     *
30
     * @param ReceivedMessage $message
31
     *
32
     * @return Intent|null
33
     */
34 3
    public function find(ReceivedMessage $message): ?Intent
35
    {
36 3
        foreach ($this->intents as $intent) {
37 3
            foreach ($intent->activators() as $activator) {
38 3
                if ($activator->matches($message) && $intent->passesAuthorization($message)) {
39 3
                    return $intent;
40
                }
41
            }
42
        }
43
44
        // Otherwise, return fallback intent
45 2
        return $this->fallbackIntent;
46
    }
47
48
    /**
49
     * Add intent.
50
     *
51
     * @param Intent $intent
52
     */
53 3
    public function add(Intent $intent): void
54
    {
55 3
        if (!in_array($intent, $this->intents, true)) {
56 3
            $this->intents[] = $intent;
57
        }
58 3
    }
59
60
    /**
61
     * Set fallback intent.
62
     *
63
     * @param Intent $intent
64
     */
65 1
    public function setFallbackIntent(Intent $intent): void
66
    {
67 1
        $this->fallbackIntent = $intent;
68 1
    }
69
}
70