Passed
Push — master ( 34b191...8284bd )
by Charis
47:08
created

Article::__construct()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 62
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 5
eloc 40
c 3
b 0
f 1
nc 4
nop 12
dl 0
loc 62
rs 8.9688

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 TYPE_TEXT = 1;
55
56
    public const TYPE_PHOTO = 2;
57
58
    public const TYPE_VIDEO = 3;
59
60
    public const ATTACHMENT_FIELD_PHOTO = 'photo';
61
62
    public const ATTACHMENT_FIELD_PAGE = 'page';
63
64
    public const ATTACHMENT_FIELD_VIDEO = 'video';
65
66
    public const ATTACHMENT_FIELD_GALLERY = 'gallery';
67
68
    /**
69
     * identifier
70
     *
71
     * @var string
72
     */
73
    protected $identifier = null;
74
75
    /**
76
     * attachment property
77
     *
78
     * @var array<mixed>
79
     */
80
    private $attachment = [];
81
82
    /**
83
     * constructor
84
     *
85
     * @param \Psr\Http\Message\UriInterface|string $source
86
     * @param string $uniqueId
87
     * @param integer $typeId
88
     * @param integer $categoryId
89
     * @param string $reporter
90
     * @param string $lead
91
     * @param string $tags
92
     * @param \DateTimeInterface|string $publishedAt
93
     */
94
    public function __construct(
95
        string $title,
96
        string $body,
97
        $source,
98
        $uniqueId,
99
        $typeId = self::TYPE_TEXT,
100
        $categoryId = self::CATEGORY_NASIONAL,
101
        $reporter = '',
102
        $lead = '',
103
        $tags = '',
104
        $publishedAt = null,
105
        $identifier = null,
106
        $headline = false
107
    ) {
108
        if (! in_array($typeId, [self::TYPE_PHOTO, self::TYPE_TEXT, self::TYPE_VIDEO], true)) {
109
            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...
110
        }
111
112
        $allowedCategory = [
113
            self::CATEGORY_NASIONAL,
114
            self::CATEGORY_INTERNASIONAL,
115
            self::CATEGORY_BISNIS,
116
            self::CATEGORY_SEPAK_BOLA,
117
            self::CATEGORY_OLAHRAGA,
118
            self::CATEGORY_HIBURAN,
119
            self::CATEGORY_TEKNOLOGI,
120
            self::CATEGORY_TRAVEL,
121
            self::CATEGORY_LIFESTYLE,
122
            self::CATEGORY_WANITA,
123
            self::CATEGORY_HIJAB,
124
            self::CATEGORY_KULINER,
125
            self::CATEGORY_SEHAT,
126
            self::CATEGORY_OTOMOTIF,
127
            self::CATEGORY_INSPIRASI,
128
            self::CATEGORY_UNIK,
129
            self::CATEGORY_EVENT,
130
            self::CATEGORY_KOMUNITAS,
131
            self::CATEGORY_E_SPORTS,
132
            self::CATEGORY_DANGDUT,
133
            self::CATEGORY_RAMADAN,
134
        ];
135
136
        if (! in_array($categoryId, $allowedCategory, true)) {
137
            throw new \InvalidArgumentException("Invalid categoryId : ${categoryId}, allowed category are " . implode(', ', $allowedCategory));
138
        }
139
140
        $this->collection = new Collection([
141
            'title' => $this->filterStringInstance($title),
142
            'reporter' => $this->filterStringInstance($reporter),
143
            'lead' => empty($lead) ? $this->createLeadFromBody($body) : $this->filterStringInstance($lead),
144
            'body' => $this->filterStringInstance($body),
145
            'source' => $this->filterUriInstance($source),
146
            'uniqueId' => $uniqueId,
147
            'type_id' => $typeId,
148
            'category_id' => $categoryId,
149
            'tags' => $this->filterStringInstance($tags),
150
            'published_at' => $this->filterDateInstance($publishedAt),
151
            'headline' => $headline,
152
        ]);
153
154
        if ($identifier) {
155
            $this->setId((string) $identifier);
156
        }
157
    }
158
159
    /**
160
     * get ALL Possible attachment for an article, return arrays of field name. Used for consistency accross sdk
161
     * leveraging php version 5.3 cannot use array constant
162
     *
163
     * @return string[]
164
     */
165
    public static function getPossibleAttachment(): array
166
    {
167
        return array_merge(
168
            self::getDeleteableAttachment(),
169
            [
170
                self::ATTACHMENT_FIELD_PHOTO,
171
            ]
172
        );
173
    }
174
175
    /**
176
     * get deleteable attachment for constant usage across sdk
177
     *
178
     * @return string[]
179
     */
180
    public static function getDeleteableAttachment(): array
181
    {
182
        return [
183
            self::ATTACHMENT_FIELD_GALLERY,
184
            self::ATTACHMENT_FIELD_PAGE,
185
            self::ATTACHMENT_FIELD_VIDEO,
186
        ];
187
    }
188
189
    /**
190
     * setIdentifier from rest api response
191
     */
192
    public function setId(string $identifier): void
193
    {
194
        $this->identifier = $identifier;
195
    }
196
197
    /**
198
     * getIdentifier set before
199
     */
200
    public function getId(): string
201
    {
202
        return $this->identifier;
203
    }
204
205
    /**
206
     * set as headline
207
     */
208
    public function setHeadline(bool $headline = true): void
209
    {
210
        $this->collection['headline'] = $headline;
211
    }
212
213
    /**
214
     * check is headline
215
     */
216
    public function isHeadline(): bool
217
    {
218
        return $this->collection['headline'];
219
    }
220
221
    /**
222
     * check if this object has attachment assigned to it
223
     */
224
    public function hasAttachment(string $field): bool
225
    {
226
        return isset($this->attachment[$field]);
227
    }
228
229
    /**
230
     * getAttachment based on fields
231
     */
232
    public function getAttachmentByField(string $field): array
233
    {
234
        if (isset($this->attachment[$field])) {
235
            return $this->attachment[$field];
236
        }
237
238
        return [];
239
    }
240
241
    /**
242
     * get ALL attachment assigned to this object
243
     */
244
    public function getAttachments(): ?array
245
    {
246
        return $this->attachment;
247
    }
248
249
    /**
250
     * add attach an attachment to this model
251
     */
252
    public function attach(string $field, Model $item): self
253
    {
254
        $this->attachment[$field][] = $item;
255
256
        return $this;
257
    }
258
259
    /**
260
     * Attach Photo Attachment to article
261
     */
262
    public function attachPhoto(Photo $photo): self
263
    {
264
        return $this->attach(
265
            self::ATTACHMENT_FIELD_PHOTO,
266
            $photo
267
        );
268
    }
269
270
    /**
271
     * Attach Paging
272
     */
273
    public function attachPage(Page $page): self
274
    {
275
        return $this->attach(
276
            self::ATTACHMENT_FIELD_PAGE,
277
            $this->ensureOrder(
278
                $page,
279
                self::ATTACHMENT_FIELD_PAGE
280
            )
281
        );
282
    }
283
284
    /**
285
     * Attach gallery here
286
     */
287
    public function attachGallery(Gallery $gallery): self
288
    {
289
        return $this->attach(
290
            self::ATTACHMENT_FIELD_GALLERY,
291
            $this->ensureOrder(
292
                $gallery,
293
                self::ATTACHMENT_FIELD_GALLERY
294
            )
295
        );
296
    }
297
298
    /**
299
     * attach Video
300
     */
301
    public function attachVideo(Video $video): self
302
    {
303
        return $this->attach(
304
            self::ATTACHMENT_FIELD_VIDEO,
305
            $this->ensureOrder(
306
                $video,
307
                self::ATTACHMENT_FIELD_VIDEO
308
            )
309
        );
310
    }
311
312
    /**
313
     * ensuring order
314
     */
315
    private function ensureOrder(Model $attachment, string $type): Model
316
    {
317
        $attachmentOrder = $attachment->get('order');
318
319
        if (empty($attachmentOrder)) {
320
            $attachment->set(
321
                'order',
322
                count($this->getAttachmentByField($type)) + 1
323
            );
324
        }
325
326
        return $attachment;
327
    }
328
}
329