Passed
Pull Request — master (#123)
by Romain
02:12
created

MediaElement::isAttachmentId()   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 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model\Message\Attachment\Template\Element;
6
7
use Kerox\Messenger\Exception\MessengerException;
8
use Kerox\Messenger\Helper\ValidatorTrait;
9
use Kerox\Messenger\Model\Common\Button\AbstractButton;
10
11
class MediaElement implements \JsonSerializable
12
{
13
    use ValidatorTrait;
14
15
    public const TYPE_IMAGE = 'image';
16
    public const TYPE_VIDEO = 'video';
17
18
    /**
19
     * @var string
20
     */
21
    protected $mediaType;
22
23
    /**
24
     * @var null|string
25
     */
26
    protected $attachmentId;
27
28
    /**
29
     * @var null|string
30
     */
31
    protected $url;
32
33
    /**
34
     * @var null|\Kerox\Messenger\Model\Common\Button\AbstractButton[]
35
     */
36
    protected $buttons;
37
38
    /**
39
     * MediaElement constructor.
40
     *
41
     * @param        $url
42
     * @param string $mediaType
43
     *
44
     * @throws \Kerox\Messenger\Exception\MessengerException
45
     */
46 3
    public function __construct($url, string $mediaType = self::TYPE_IMAGE)
47
    {
48 3
        if ($this->isAttachmentId($url)) {
49
            $this->attachmentId = $url;
50
        } else {
51 3
            $this->isValidUrl($url);
52 3
            $this->url = $url;
53
        }
54
55 3
        $this->isValidMediaType($mediaType);
56 2
        $this->mediaType = $mediaType;
57 2
    }
58
59
    /**
60
     * @param        $url
61
     * @param string $mediaType
62
     *
63
     * @throws \Kerox\Messenger\Exception\MessengerException
64
     *
65
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\Element\MediaElement
66
     */
67 3
    public static function create($url, string $mediaType = self::TYPE_IMAGE): self
68
    {
69 3
        return new self($url, $mediaType);
70
    }
71
72
    /**
73
     * @param \Kerox\Messenger\Model\Common\Button\AbstractButton[] $buttons
74
     *
75
     * @throws \Kerox\Messenger\Exception\MessengerException
76
     *
77
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\Element\MediaElement
78
     */
79 2
    public function setButtons(array $buttons): self
80
    {
81 2
        $this->isValidArray($buttons, 1);
82 2
        $this->isValidButtons($buttons, $this->getAllowedButtonsType());
83
84 1
        $this->buttons = $buttons;
85
86 1
        return $this;
87
    }
88
89
    /**
90
     * @return array
91
     */
92 2
    protected function getAllowedButtonsType(): array
93
    {
94
        return [
95 2
            AbstractButton::TYPE_WEB_URL,
96
        ];
97
    }
98
99
    /**
100
     * @param $value
101
     *
102
     * @return bool
103
     */
104 3
    private function isAttachmentId($value): bool
105
    {
106 3
        return (bool) preg_match('/^[\d]+$/', $value);
107
    }
108
109
    /**
110
     * @param $mediaType
111
     *
112
     * @throws \Kerox\Messenger\Exception\MessengerException
113
     */
114 3
    private function isValidMediaType($mediaType): void
115
    {
116 3
        $allowedMediaType = $this->getAllowedMediaType();
117 3
        if (!\in_array($mediaType, $allowedMediaType, true)) {
118 1
            throw new MessengerException(sprintf(
119 1
                'mediaType must be either "%s".',
120 1
                implode(', ', $allowedMediaType)
121
            ));
122
        }
123 2
    }
124
125
    /**
126
     * @return array
127
     */
128 3
    protected function getAllowedMediaType(): array
129
    {
130
        return [
131 3
            self::TYPE_IMAGE,
132 3
            self::TYPE_VIDEO,
133
        ];
134
    }
135
136
    /**
137
     * @return array
138
     */
139 1
    public function toArray(): array
140
    {
141
        $array = [
142 1
            'media_type' => $this->mediaType,
143 1
            'attachment_id' => $this->attachmentId,
144 1
            'url' => $this->url,
145 1
            'buttons' => $this->buttons,
146
        ];
147
148 1
        return array_filter($array);
149
    }
150
151
    /**
152
     * @return array
153
     */
154 1
    public function jsonSerialize(): array
155
    {
156 1
        return $this->toArray();
157
    }
158
}
159