Completed
Push — master ( eef8f6...a3a7a3 )
by Vladimir
02:39
created

InArray::matches()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 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 InArray implements Activator
12
{
13
    protected $values;
14
    protected $strict;
15
16
    /**
17
     * InArray constructor.
18
     *
19
     * @param array|Collection $values
20
     * @param bool             $strict
21
     */
22 6
    public function __construct($values, bool $strict = true)
23
    {
24 6
        if (!is_array($values) && !$values instanceof Collection) {
25 1
            $values = str_getcsv($values);
26
        }
27
28 6
        $this->values = $values;
29 6
        $this->strict = $strict;
30 6
    }
31
32
    public function strict(bool $strict): self
33
    {
34
        $this->strict = $strict;
35
36
        return $this;
37
    }
38
39
    /**
40
     * Result of matching activator.
41
     *
42
     * @param MessageReceived $message
43
     *
44
     * @return bool
45
     */
46 4
    public function matches(MessageReceived $message): bool
47
    {
48 4
        $haystack = $this->values;
49
50 4
        if ($haystack instanceof Collection) {
51 2
            $haystack = $haystack->toArray();
52
        }
53
54 4
        return in_array($message->getText(), $haystack, $this->strict);
55
    }
56
}
57