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

InArray   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 78.56%

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 11
cts 14
cp 0.7856
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A strict() 0 6 1
A matches() 0 10 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