Completed
Push — master ( a22976...bdfc9e )
by Vladimir
03:53
created

Contains   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 10
cts 12
cp 0.8333
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A make() 0 4 1
A matches() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Conversation\Activators;
6
7
use Illuminate\Support\Collection;
8
use FondBot\Events\MessageReceived;
9
use FondBot\Contracts\Conversation\Activator;
10
11
class Contains implements Activator
12
{
13
    protected $needles;
14
15
    /**
16
     * @param array|string $needles
17
     */
18 4
    protected function __construct($needles)
19
    {
20 4
        if ($needles instanceof Collection) {
21
            $needles = $needles->toArray();
22
        }
23
24 4
        $this->needles = $needles;
25 4
    }
26
27
    /**
28
     * @param array|string $needles
29
     *
30
     * @return static
31
     */
32 4
    public static function make($needles)
33
    {
34 4
        return new static($needles);
35
    }
36
37
    /**
38
     * Result of matching activator.
39
     *
40
     * @param MessageReceived $message
41
     *
42
     * @return bool
43
     */
44 3
    public function matches(MessageReceived $message): bool
45
    {
46 3
        $text = $message->getText();
47 3
        if ($text === null) {
48
            return false;
49
        }
50
51 3
        return str_contains($text, (array) $this->needles);
52
    }
53
}
54