Completed
Push — master ( ed3a3e...3e13b9 )
by Vladimir
02:37
created

ConversationManager::matchIntent()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
cc 5
eloc 7
nc 4
nop 1
ccs 0
cts 7
cp 0
crap 30
rs 8.8571
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Conversation;
6
7
use FondBot\Events\MessageReceived;
8
use FondBot\Contracts\Conversation\Manager;
9
10
class ConversationManager implements Manager
11
{
12
    private $intents = [];
13
    private $fallbackIntent;
14
15
    /**
16
     * Register intent.
17
     *
18
     * @param string $class
19
     */
20
    public function registerIntent(string $class): void
21
    {
22
        $this->intents[] = $class;
23
    }
24
25
    /**
26
     * Register fallback intent.
27
     *
28
     * @param string $class
29
     */
30 83
    public function registerFallbackIntent(string $class): void
31
    {
32 83
        $this->fallbackIntent = $class;
33 83
    }
34
35
    /**
36
     * Get all registered intents.
37
     *
38
     * @return array
39
     */
40
    public function getIntents(): array
41
    {
42
        return $this->intents;
43
    }
44
45
    /**
46
     * Match intent by received message.
47
     *
48
     * @param MessageReceived $messageReceived
49
     *
50
     * @return Intent|null
51
     */
52
    public function matchIntent(MessageReceived $messageReceived): ?Intent
53
    {
54
        foreach ($this->intents as $intent) {
55
            /** @var Intent $intent */
56
            $intent = resolve($intent);
0 ignored issues
show
Documentation introduced by
$intent is of type object<FondBot\Conversation\Intent>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57
58
            foreach ($intent->activators() as $activator) {
59
                if ($activator->matches($messageReceived) && $intent->passesAuthorization($messageReceived)) {
60
                    return $intent;
61
                }
62
            }
63
        }
64
65
        // Otherwise, return fallback intent
66
        return resolve($this->fallbackIntent);
67
    }
68
}
69