File   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 67
rs 10
c 0
b 0
f 0
ccs 18
cts 18
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A create() 0 4 1
A isAttachmentId() 0 4 1
A toArray() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model\Message\Attachment;
6
7
use Kerox\Messenger\Model\Message\AbstractAttachment;
8
9
class File extends AbstractAttachment
10
{
11
    /**
12
     * @var string|null
13
     */
14
    protected $url;
15
16
    /**
17
     * @var bool|null
18
     */
19
    protected $reusable;
20
21
    /**
22
     * @var string|null
23
     */
24
    protected $attachmentId;
25
26
    /**
27
     * File constructor.
28
     *
29
     * @param string $type
30
     *
31
     * @throws \Kerox\Messenger\Exception\MessengerException
32
     */
33 6
    public function __construct(string $url, ?bool $reusable = null, $type = AbstractAttachment::TYPE_FILE)
34
    {
35 6
        parent::__construct($type);
36
37 6
        if ($this->isAttachmentId($url)) {
38 1
            $this->attachmentId = $url;
39
        } else {
40 5
            $this->isValidUrl($url);
41 5
            $this->url = $url;
42
        }
43
44 6
        $this->reusable = $reusable;
45 6
    }
46
47
    /**
48
     * @throws \Kerox\Messenger\Exception\MessengerException
49
     *
50
     * @return \Kerox\Messenger\Model\Message\Attachment\File
51
     */
52 2
    public static function create(string $url, ?bool $reusable = null): self
53
    {
54 2
        return new self($url, $reusable);
55
    }
56
57 6
    private function isAttachmentId(string $value): bool
58
    {
59 6
        return (bool) preg_match('/^[\d]+$/', $value);
60
    }
61
62 6
    public function toArray(): array
63
    {
64 6
        $array = parent::toArray();
65
        $array += [
66
            'payload' => [
67 6
                'url' => $this->url,
68 6
                'is_reusable' => $this->reusable,
69 6
                'attachment_id' => $this->attachmentId,
70
            ],
71
        ];
72
73 6
        return $this->arrayFilter($array);
74
    }
75
}
76