Completed
Pull Request — master (#1)
by Romain
02:17
created

File   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 67
rs 10
c 0
b 0
f 0

3 Methods

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