Completed
Branch develop (a0a623)
by Romain
02:33
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
/**
3
 * Created by PhpStorm.
4
 * User: rmo
5
 * Date: 09/11/2016
6
 * Time: 17:17
7
 */
8
9
namespace Kerox\Messenger\Message\Attachment;
10
11
use Kerox\Messenger\Message\Attachment;
12
13
class File extends Attachment
14
{
15
16
    /**
17
     * @var null|string
18
     */
19
    protected $url;
20
21
    /**
22
     * @var null|bool
23
     */
24
    protected $reusable;
25
26
    /**
27
     * @var null|string
28
     */
29
    protected $attachmentId;
30
31
    /**
32
     * File constructor.
33
     *
34
     * @param string $url
35
     * @param bool|null $reusable
36
     * @param string $type
37
     */
38
    public function __construct($url, bool $reusable = null, $type = Attachment::TYPE_FILE)
39
    {
40
        parent::__construct($type);
41
42
        if ($this->isAttachmentId($url)) {
43
            $this->attachmentId = $url;
44
        } else {
45
            $this->isValidUrl($url);
46
            $this->url = $url;
47
        }
48
49
        $this->reusable = $reusable;
50
    }
51
52
    /**
53
     * @param $value
54
     * @return bool
55
     */
56
    private function isAttachmentId($value): bool
57
    {
58
        return preg_match('/^[\d]+$/', $value);
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    public function jsonSerialize(): array
65
    {
66
        $payload = [
67
            'url' => $this->url,
68
            'is_reusable' => $this->reusable,
69
            'attachment_id' => $this->attachmentId,
70
        ];
71
72
        $json = parent::jsonSerialize();
73
        $json += [
74
            'payload' => array_filter($payload),
75
        ];
76
77
        return $json;
78
    }
79
}