Passed
Push — master ( ba4419...5e7510 )
by
unknown
03:13
created

Article::isHeadlineLip6()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
3
namespace One\Model;
4
5
use One\Collection;
6
7
/**
8
 * Article Class
9
 */
10
class Article extends Model
11
{
12
    public const CATEGORY_NASIONAL = 1;
13
14
    public const CATEGORY_INTERNASIONAL = 2;
15
16
    public const CATEGORY_BISNIS = 3;
17
18
    public const CATEGORY_SEPAK_BOLA = 4;
19
20
    public const CATEGORY_OLAHRAGA = 5;
21
22
    public const CATEGORY_HIBURAN = 6;
23
24
    public const CATEGORY_TEKNOLOGI = 7;
25
26
    public const CATEGORY_TRAVEL = 8;
27
28
    public const CATEGORY_LIFESTYLE = 9;
29
30
    public const CATEGORY_WANITA = 10;
31
32
    public const CATEGORY_HIJAB = 11;
33
34
    public const CATEGORY_KULINER = 12;
35
36
    public const CATEGORY_SEHAT = 13;
37
38
    public const CATEGORY_OTOMOTIF = 14;
39
40
    public const CATEGORY_INSPIRASI = 15;
41
42
    public const CATEGORY_UNIK = 16;
43
44
    public const CATEGORY_EVENT = 17;
45
46
    public const CATEGORY_KOMUNITAS = 18;
47
48
    public const CATEGORY_E_SPORTS = 19;
49
50
    public const CATEGORY_DANGDUT = 20;
51
52
    public const CATEGORY_RAMADAN = 21;
53
54
    public const CATEGORY_CEK_FAKTA = 25;
55
56
    public const CATEGORY_HAJI = 26;
57
58
    public const TYPE_TEXT = 1;
59
60
    public const TYPE_PHOTO = 2;
61
62
    public const TYPE_VIDEO = 3;
63
64
    public const ATTACHMENT_FIELD_PHOTO = 'photo';
65
66
    public const ATTACHMENT_FIELD_PAGE = 'page';
67
68
    public const ATTACHMENT_FIELD_VIDEO = 'video';
69
70
    public const ATTACHMENT_FIELD_GALLERY = 'gallery';
71
72
    /**
73
     * identifier
74
     *
75
     * @var string
76
     */
77
    protected $identifier = null;
78
79
    /**
80
     * attachment property
81
     *
82
     * @var array<mixed>
83
     */
84
    private $attachment = [];
85
86
    /**
87
     * constructor
88
     *
89
     * @param \Psr\Http\Message\UriInterface|string $source
90
     * @param string $uniqueId
91
     * @param integer $typeId
92
     * @param integer $categoryId
93
     * @param string $reporter
94
     * @param string $lead
95
     * @param string $tags
96
     * @param \DateTimeInterface|string $publishedAt
97
     * @param string $identifier
98
     * @param boolean $headline
99
     * @param boolean $headline_lip6
100
     */
101
    public function __construct(
102
        string $title,
103
        string $body,
104
        $source,
105
        $uniqueId,
106
        $typeId = self::TYPE_TEXT,
107
        $categoryId = self::CATEGORY_NASIONAL,
108
        $reporter = '',
109
        $lead = '',
110
        $tags = '',
111
        $publishedAt = null,
112
        $identifier = null,
113
        $headline = false,
114
        $headlineLip6 = false
115
    ) {
116
        if (! in_array($typeId, [self::TYPE_PHOTO, self::TYPE_TEXT, self::TYPE_VIDEO], true)) {
117
            throw new \InvalidArgumentException("Invalid typeId : ${typeId}, allowed typeId are " . implode(', ', $allowedType));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $allowedType seems to be never defined.
Loading history...
118
        }
119
120
        $allowedCategory = [
121
            self::CATEGORY_NASIONAL,
122
            self::CATEGORY_INTERNASIONAL,
123
            self::CATEGORY_BISNIS,
124
            self::CATEGORY_SEPAK_BOLA,
125
            self::CATEGORY_OLAHRAGA,
126
            self::CATEGORY_HIBURAN,
127
            self::CATEGORY_TEKNOLOGI,
128
            self::CATEGORY_TRAVEL,
129
            self::CATEGORY_LIFESTYLE,
130
            self::CATEGORY_WANITA,
131
            self::CATEGORY_HIJAB,
132
            self::CATEGORY_KULINER,
133
            self::CATEGORY_SEHAT,
134
            self::CATEGORY_OTOMOTIF,
135
            self::CATEGORY_INSPIRASI,
136
            self::CATEGORY_UNIK,
137
            self::CATEGORY_EVENT,
138
            self::CATEGORY_KOMUNITAS,
139
            self::CATEGORY_E_SPORTS,
140
            self::CATEGORY_DANGDUT,
141
            self::CATEGORY_RAMADAN,
142
            self::CATEGORY_CEK_FAKTA,
143
            self::CATEGORY_HAJI
144
        ];
145
146
        if (! in_array($categoryId, $allowedCategory, true)) {
147
            throw new \InvalidArgumentException("Invalid categoryId : ${categoryId}, allowed category are " . implode(', ', $allowedCategory));
148
        }
149
150
        $this->collection = new Collection([
151
            'title' => $this->filterStringInstance($title),
152
            'reporter' => $this->filterStringInstance($reporter),
153
            'lead' => empty($lead) ? $this->createLeadFromBody($body) : $this->filterStringInstance($lead),
154
            'body' => $this->filterStringInstance($body),
155
            'source' => $this->filterUriInstance($source),
156
            'uniqueId' => $uniqueId,
157
            'type_id' => $typeId,
158
            'category_id' => $categoryId,
159
            'tags' => $this->filterStringInstance($tags),
160
            'published_at' => $this->filterDateInstance($publishedAt),
161
            'headline' => $headline,
162
            'headline_lip6' => $headlineLip6 ? 1 : 0
163
        ]);
164
165
        if ($identifier) {
166
            $this->setId((string) $identifier);
167
        }
168
    }
169
170
    /**
171
     * get ALL Possible attachment for an article, return arrays of field name. Used for consistency accross sdk
172
     * leveraging php version 5.3 cannot use array constant
173
     *
174
     * @return string[]
175
     */
176
    public static function getPossibleAttachment(): array
177
    {
178
        return array_merge(
179
            self::getDeleteableAttachment(),
180
            [
181
                self::ATTACHMENT_FIELD_PHOTO,
182
            ]
183
        );
184
    }
185
186
    /**
187
     * get deleteable attachment for constant usage across sdk
188
     *
189
     * @return string[]
190
     */
191
    public static function getDeleteableAttachment(): array
192
    {
193
        return [
194
            self::ATTACHMENT_FIELD_GALLERY,
195
            self::ATTACHMENT_FIELD_PAGE,
196
            self::ATTACHMENT_FIELD_VIDEO,
197
        ];
198
    }
199
200
    /**
201
     * setIdentifier from rest api response
202
     */
203
    public function setId(string $identifier): void
204
    {
205
        $this->identifier = $identifier;
206
    }
207
208
    /**
209
     * getIdentifier set before
210
     */
211
    public function getId(): string
212
    {
213
        return $this->identifier;
214
    }
215
216
    /**
217
     * set as headline
218
     */
219
    public function setHeadline(bool $headline = true): void
220
    {
221
        $this->collection['headline'] = $headline;
222
    }
223
224
    /**
225
     * check is headline
226
     */
227
    public function isHeadline(): bool
228
    {
229
        return $this->collection['headline'];
230
    }
231
232
    public function setHeadlineLip6(bool $headline = true): void
233
    {
234
        $this->collection['headline_lip6'] = $headline;
235
    }
236
237
    public function isHeadlineLip6(): bool
238
    {
239
        return $this->collection['headline_lip6'];
240
    }
241
242
    /**
243
     * check if this object has attachment assigned to it
244
     */
245
    public function hasAttachment(string $field): bool
246
    {
247
        return isset($this->attachment[$field]);
248
    }
249
250
    /**
251
     * getAttachment based on fields
252
     */
253
    public function getAttachmentByField(string $field): array
254
    {
255
        if (isset($this->attachment[$field])) {
256
            return $this->attachment[$field];
257
        }
258
259
        return [];
260
    }
261
262
    /**
263
     * get ALL attachment assigned to this object
264
     */
265
    public function getAttachments(): ?array
266
    {
267
        return $this->attachment;
268
    }
269
270
    /**
271
     * add attach an attachment to this model
272
     */
273
    public function attach(string $field, Model $item): self
274
    {
275
        $this->attachment[$field][] = $item;
276
277
        return $this;
278
    }
279
280
    /**
281
     * Attach Photo Attachment to article
282
     */
283
    public function attachPhoto(Photo $photo): self
284
    {
285
        return $this->attach(
286
            self::ATTACHMENT_FIELD_PHOTO,
287
            $photo
288
        );
289
    }
290
291
    /**
292
     * Attach Paging
293
     */
294
    public function attachPage(Page $page): self
295
    {
296
        return $this->attach(
297
            self::ATTACHMENT_FIELD_PAGE,
298
            $this->ensureOrder(
299
                $page,
300
                self::ATTACHMENT_FIELD_PAGE
301
            )
302
        );
303
    }
304
305
    /**
306
     * Attach gallery here
307
     */
308
    public function attachGallery(Gallery $gallery): self
309
    {
310
        return $this->attach(
311
            self::ATTACHMENT_FIELD_GALLERY,
312
            $this->ensureOrder(
313
                $gallery,
314
                self::ATTACHMENT_FIELD_GALLERY
315
            )
316
        );
317
    }
318
319
    /**
320
     * attach Video
321
     */
322
    public function attachVideo(Video $video): self
323
    {
324
        return $this->attach(
325
            self::ATTACHMENT_FIELD_VIDEO,
326
            $this->ensureOrder(
327
                $video,
328
                self::ATTACHMENT_FIELD_VIDEO
329
            )
330
        );
331
    }
332
333
    /**
334
     * ensuring order
335
     */
336
    private function ensureOrder(Model $attachment, string $type): Model
337
    {
338
        $attachmentOrder = $attachment->get('order');
339
340
        if (empty($attachmentOrder)) {
341
            $attachment->set(
342
                'order',
343
                count($this->getAttachmentByField($type)) + 1
344
            );
345
        }
346
347
        return $attachment;
348
    }
349
}
350