Passed
Push — master ( 6c011a...8a7749 )
by Romain
36s
created

File::create()   A

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