Completed
Push — master ( 0a1c77...2f1c32 )
by Vladimir
02:48
created

Exact::caseSensitive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Conversation\Activators;
6
7
use Illuminate\Support\Str;
8
use FondBot\Events\MessageReceived;
9
use FondBot\Contracts\Conversation\Activator;
10
11
class Exact implements Activator
12
{
13
    protected $value;
14
    protected $caseSensitive;
15
16 6
    public function __construct(string $value, bool $caseSensitive = false)
17
    {
18 6
        $this->value = $value;
19 6
        $this->caseSensitive = $caseSensitive;
20 6
    }
21
22
    public function caseSensitive(bool $caseSensitive): self
23
    {
24
        $this->caseSensitive = $caseSensitive;
25
26
        return $this;
27
    }
28
29
    /**
30
     * Result of matching activator.
31
     *
32
     * @param MessageReceived $message
33
     *
34
     * @return bool
35
     */
36 5
    public function matches(MessageReceived $message): bool
37
    {
38 5
        $text = $message->getText();
39 5
        if ($text === null) {
40
            return false;
41
        }
42
43 5
        if (!$this->caseSensitive) {
44 3
            $text = Str::lower($text);
45 3
            $this->value = Str::lower($this->value);
46
        }
47
48 5
        return hash_equals($this->value, $text);
49
    }
50
}
51