Completed
Push — master ( 393226...f49a8d )
by Romain
11s
created

ValidatorTrait::isValidSenderAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 2
eloc 3
nc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Helper;
6
7
use InvalidArgumentException;
8
use Kerox\Messenger\Model\Common\Button\AbstractButton;
9
use Kerox\Messenger\Model\Message;
10
use Kerox\Messenger\Model\Message\Attachment;
11
use Kerox\Messenger\SendInterface;
12
13
trait ValidatorTrait
14
{
15 2
    /**
16
     * @param string $value
17 2
     *
18 1
     * @throws \InvalidArgumentException
19
     */
20 1
    protected function isValidColor(string $value): void
21
    {
22
        if (!preg_match('/^#[A-Fa-f0-9]{6}$/', $value)) {
23
            throw new InvalidArgumentException('The color must be expressed in #rrggbb format.');
24
        }
25
    }
26
27
    /**
28 31
     * @param string $value
29
     * @param int    $length
30 31
     *
31 1
     * @throws \InvalidArgumentException
32
     */
33 30
    protected function isValidString(string $value, int $length = 20): void
34
    {
35
        if (mb_strlen($value) > $length) {
36
            throw new InvalidArgumentException("String should not exceed {$length} characters.");
37
        }
38
    }
39
40 28
    /**
41
     * @param string $value
42 28
     *
43 1
     * @throws \InvalidArgumentException
44
     */
45 27
    protected function isValidUrl(string $value): void
46
    {
47
        if (!preg_match('/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/=]*)$/', $value)) {
48
            throw new InvalidArgumentException("{$value} is not a valid url.");
49
        }
50
    }
51
52 8
    /**
53
     * @param string $value
54 8
     *
55 1
     * @throws \InvalidArgumentException
56
     */
57 7
    protected function isValidLocale(string $value): void
58
    {
59
        if (!preg_match('/^[a-z]{2}_[A-Z]{2}$/', $value)) {
60
            throw new InvalidArgumentException(
61
                "{$value} is not valid. Locale must be in ISO-639-1 and ISO-3166-1 format like fr_FR."
62
            );
63
        }
64 2
    }
65
66 2
    /**
67 1
     * @param string $value
68
     *
69 1
     * @throws \InvalidArgumentException
70
     */
71
    protected function isValidCountry(string $value): void
72
    {
73
        if (!preg_match('/^[A-Z]{2}$/', $value)) {
74
            throw new InvalidArgumentException(
75
                "{$value} is not valid. Country must be in ISO 3166 Alpha-2 format like FR."
76 11
            );
77
        }
78 11
    }
79 1
80
    /**
81 10
     * @param string $value
82
     *
83
     * @throws \InvalidArgumentException
84
     */
85
    protected function isValidDateTime(string $value): void
86
    {
87
        if (!preg_match('/^(\d{4})-(0[1-9]|1[0-2])-([12]\d|0[1-9]|3[01])T(0[0-9]|1\d|2[0-3]):([0-5]\d)$/', $value)) {
88
            throw new InvalidArgumentException(
89
                "{$value} is not valid. DateTime must be in ISO-8601 AAAA-MM-JJThh:mm format"
90 24
            );
91
        }
92 24
    }
93 24
94 1
    /**
95
     * @param array $array
96 23
     * @param int   $maxSize
97 3
     * @param int   $minSize
98
     *
99 22
     * @throws \InvalidArgumentException
100
     */
101
    protected function isValidArray(array $array, int $maxSize, ?int $minSize = null): void
102
    {
103
        $countArray = \count($array);
104
        if ($minSize !== null && $countArray < $minSize) {
105
            throw new InvalidArgumentException("The minimum number of items for this array is {$minSize}.");
106 6
        }
107
        if ($countArray > $maxSize) {
108 6
            throw new InvalidArgumentException("The maximum number of items for this array is {$maxSize}.");
109 1
        }
110
    }
111 5
112
    /**
113
     * @param string $value
114
     *
115
     * @throws \InvalidArgumentException
116
     */
117
    protected function isValidCurrency(string $value): void
118
    {
119 3
        if (!preg_match('/^SGD|RON|EUR|TRY|SEK|ZAR|HKD|CHF|NIO|JPY|ISK|TWD|NZD|CZK|AUD|THB|BOB|BRL|MXN|USD|ILS|HNL|MOP|COP|UYU|CRC|DKK|QAR|PYG|CAD|INR|KRW|GTQ|AED|VEF|SAR|NOK|CNY|ARS|PLN|GBP|PEN|PHP|VND|RUB|HUF|MYR|CLP|IDR$/', $value)) {
120
            throw new InvalidArgumentException(
121 3
                "{$value} is not a valid currency. Currency must be in ISO-4217-3 format."
122 3
            );
123 1
        }
124
    }
125 2
126
    /**
127
     * @param string $filename
128
     * @param array  $allowedExtension
129
     *
130
     * @throws \InvalidArgumentException
131 6
     */
132
    protected function isValidExtension(string $filename, array $allowedExtension): void
133
    {
134 6
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
135 6
        if (empty($ext) || !\in_array($ext, $allowedExtension, true)) {
136 1
            throw new InvalidArgumentException(
137
                "{$filename} doesn't have a valid extension. Allowed extensions are " . implode(', ', $allowedExtension)
138
            );
139 5
        }
140 5
    }
141
142
    /**
143 4
     * @param \Kerox\Messenger\Model\Common\Button\AbstractButton[] $buttons
144
     * @param array                                                 $allowedButtonsType
145
     *
146
     * @throws \InvalidArgumentException
147
     */
148
    protected function isValidButtons(array $buttons, array $allowedButtonsType): void
149
    {
150
        /** @var \Kerox\Messenger\Model\Common\Button\AbstractButton $button */
151
        foreach ($buttons as $button) {
152
            if (!$button instanceof AbstractButton) {
153
                throw new \InvalidArgumentException('Array can only contain instance of AbstractButton.');
154
            }
155
156
            if (!\in_array($button->getType(), $allowedButtonsType, true)) {
157
                throw new \InvalidArgumentException(
158
                    'Buttons can only be an instance of ' . implode(', ', $allowedButtonsType)
159
                );
160
            }
161
        }
162
    }
163
164
    /**
165
     * @param $message
166
     *
167
     * @throws \Exception
168
     *
169
     * @return \Kerox\Messenger\Model\Message
170
     */
171
    private function isValidMessage($message): Message
172
    {
173
        if ($message instanceof Message) {
174
            return $message;
175
        }
176
177
        if (\is_string($message) || $message instanceof Attachment) {
178
            return Message::create($message);
179
        }
180
181
        throw new \InvalidArgumentException('$message must be a string or an instance of Message or Attachment');
182
    }
183
184
    /**
185
     * @param string $action
186
     *
187
     * @throws \InvalidArgumentException
188
     */
189
    protected function isValidSenderAction(string $action): void
190
    {
191
        $allowedSenderAction = $this->getAllowedSenderAction();
192
        if (!\in_array($action, $allowedSenderAction, true)) {
193
            throw new \InvalidArgumentException('$action must be either ' . implode(', ', $allowedSenderAction));
194
        }
195
    }
196
197
    /**
198
     * @param string $notificationType
199
     *
200
     * @throws \InvalidArgumentException
201
     */
202
    protected function isValidNotificationType(string $notificationType): void
203
    {
204
        $allowedNotificationType = $this->getAllowedNotificationType();
205
        if (!\in_array($notificationType, $allowedNotificationType, true)) {
206
            throw new \InvalidArgumentException('$notificationType must be either ' . implode(', ', $allowedNotificationType));
207
        }
208
    }
209
210
    /**
211
     * @param string $tag
212
     *
213
     * @throws \InvalidArgumentException
214
     */
215
    protected function isValidTag(string $tag): void
216
    {
217
        $allowedTag = $this->getAllowedTag();
218
        if (!\in_array($tag, $allowedTag, true)) {
219
            throw new \InvalidArgumentException('$tag must be either ' . implode(', ', $allowedTag));
220
        }
221
    }
222
223
    /**
224
     * @return array
225
     */
226
    public function getAllowedSenderAction(): array
227
    {
228
        return [
229
            SendInterface::SENDER_ACTION_TYPING_ON,
230
            SendInterface::SENDER_ACTION_TYPING_OFF,
231
            SendInterface::SENDER_ACTION_MARK_SEEN,
232
        ];
233
    }
234
235
    /**
236
     * @return array
237
     */
238
    public function getAllowedNotificationType(): array
239
    {
240
        return [
241
            SendInterface::NOTIFICATION_TYPE_REGULAR,
242
            SendInterface::NOTIFICATION_TYPE_SILENT_PUSH,
243
            SendInterface::NOTIFICATION_TYPE_NO_PUSH,
244
        ];
245
    }
246
247
    /**
248
     * @return array
249
     */
250
    public function getAllowedTag(): array
251
    {
252
        return [
253
            SendInterface::TAG_COMMUNITY_ALERT,
254
            SendInterface::TAG_CONFIRMED_EVENT_REMINDER,
255
            SendInterface::TAG_NON_PROMOTIONAL_SUBSCRIPTION,
256
            SendInterface::TAG_PAIRING_UPDATE,
257
            SendInterface::TAG_APPLICATION_UPDATE,
258
            SendInterface::TAG_ACCOUNT_UPDATE,
259
            SendInterface::TAG_PAYMENT_UPDATE,
260
            SendInterface::TAG_PERSONAL_FINANCE_UPDATE,
261
            SendInterface::TAG_SHIPPING_UPDATE,
262
            SendInterface::TAG_RESERVATION_UPDATE,
263
            SendInterface::TAG_ISSUE_RESOLUTION,
264
            SendInterface::TAG_APPOINTMENT_UPDATE,
265
            SendInterface::TAG_GAME_EVENT,
266
            SendInterface::TAG_TRANSPORTATION_UPDATE,
267
            SendInterface::TAG_FEATURE_FUNCTIONALITY_UPDATE,
268
            SendInterface::TAG_TICKET_UPDATE,
269
        ];
270
    }
271
}
272