Passed
Push — php-5x ( ce0fc0...5a5d63 )
by Charis
02:04
created

Article::attachPhoto()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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