Attachment::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Conversation\Activators;
6
7
use FondBot\Contracts\Activator;
8
use FondBot\Events\MessageReceived;
9
use FondBot\Templates\Attachment as Template;
10
11
class Attachment implements Activator
12
{
13
    protected $type;
14
15 10
    protected function __construct(string $type = null)
16
    {
17 10
        $this->type = $type;
18 10
    }
19
20 10
    public static function make(string $type = null)
21
    {
22 10
        return new static($type);
23
    }
24
25
    public function file(): self
26
    {
27
        $this->type = Template::TYPE_FILE;
28
29
        return $this;
30
    }
31
32
    public function image(): self
33
    {
34
        $this->type = Template::TYPE_IMAGE;
35
36
        return $this;
37
    }
38
39
    public function audio(): self
40
    {
41
        $this->type = Template::TYPE_AUDIO;
42
43
        return $this;
44
    }
45
46
    public function video(): self
47
    {
48
        $this->type = Template::TYPE_VIDEO;
49
50
        return $this;
51
    }
52
53
    /**
54
     * Result of matching activator.
55
     *
56
     * @param MessageReceived $message
57
     *
58
     * @return bool
59
     */
60 10
    public function matches(MessageReceived $message): bool
61
    {
62 10
        if ($this->type === null) {
63 2
            return $message->getAttachment() !== null;
64
        }
65
66 8
        return hash_equals($message->getAttachment()->getType(), $this->type);
67
    }
68
}
69