Passed
Pull Request — master (#21)
by kenny
03:37
created

FormatMapping::photoRatio()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 1
dl 0
loc 14
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace one;
4
5
use One\Model\Article;
6
use one\Model\Gallery;
7
use One\Model\Page;
8
use one\Model\Photo;
9
use One\Model\Video;
10
11
class FormatMapping
12
{
13
14
    /**
15
     * Json Attributes of Photo
16
     * @var array
17
     */
18
    private $photoAttributes;
19
20
    /**
21
     * Json Attributes of Page
22
     * @var array
23
     */
24
    private $pageAttributes;
25
26
    /**
27
     * Json Attributes of gallery
28
     * @var array
29
     */
30
    private $galleryAttributes;
31
32
    /**
33
     * Json Attributes of video
34
     * @var array
35
     */
36
    private $videoAttributes;
37
38
    /**
39
     * Construct Json attribute variables
40
     */
41
42
    const JSON_PHOTO_FIELD = "photos";
43
44
    const JSON_PAGE_FIELD = "pages";
45
46
    const JSON_GALLERY_FIELD = "galleries";
47
48
    const JSON_VIDEO_FIELD = "videos";
49
50
    const JSON_ATTRIBUTES = array(
51
        'ID' => "_id",
52
        "URL" => '_url',
53
        "RATIO" => '_ratio',
54
        "DESCRIPTION" => '_description',
55
        "INFORMATION" => '_information',
56
        "TITLE" => '_title',
57
        "LEAD" => '_lead',
58
        "BODY" => '_body',
59
        "SOURCE" => '_source',
60
        "ORDER" => '_order',
61
        "COVER" => '_cover',
62
        "PHOTO" => '_photo',
63
64
    );
65
66
    /**
67
     * Construct JSON attributes
68
     */
69
    public function __construct()
70
    {
71
        $this->photoAttributes = array(
72
            Article::ATTACHMENT_FIELD_PHOTO . self::JSON_ATTRIBUTES['ID'],
73
            Article::ATTACHMENT_FIELD_PHOTO . self::JSON_ATTRIBUTES['URL'],
74
            Article::ATTACHMENT_FIELD_PHOTO . self::JSON_ATTRIBUTES['RATIO'],
75
            Article::ATTACHMENT_FIELD_PHOTO . self::JSON_ATTRIBUTES['DESCRIPTION'],
76
            Article::ATTACHMENT_FIELD_PHOTO . self::JSON_ATTRIBUTES['INFORMATION'],
77
78
        );
79
80
        $this->pageAttributes = array(
81
            Article::ATTACHMENT_FIELD_PAGE . self::JSON_ATTRIBUTES['ID'],
82
            Article::ATTACHMENT_FIELD_PAGE . self::JSON_ATTRIBUTES['TITLE'],
83
            Article::ATTACHMENT_FIELD_PAGE . self::JSON_ATTRIBUTES['LEAD'],
84
            Article::ATTACHMENT_FIELD_PAGE . self::JSON_ATTRIBUTES['BODY'],
85
            Article::ATTACHMENT_FIELD_PAGE . self::JSON_ATTRIBUTES['SOURCE'],
86
            Article::ATTACHMENT_FIELD_PAGE . self::JSON_ATTRIBUTES['ORDER'],
87
            Article::ATTACHMENT_FIELD_PAGE . self::JSON_ATTRIBUTES['COVER'],
88
        );
89
90
        $this->galleryAttributes = array(
91
            Article::ATTACHMENT_FIELD_GALLERY . self::JSON_ATTRIBUTES['ID'],
92
            Article::ATTACHMENT_FIELD_GALLERY . self::JSON_ATTRIBUTES['LEAD'],
93
            Article::ATTACHMENT_FIELD_GALLERY . self::JSON_ATTRIBUTES['BODY'],
94
            Article::ATTACHMENT_FIELD_GALLERY . self::JSON_ATTRIBUTES['SOURCE'],
95
            Article::ATTACHMENT_FIELD_GALLERY . self::JSON_ATTRIBUTES['ORDER'],
96
            Article::ATTACHMENT_FIELD_GALLERY . self::JSON_ATTRIBUTES['PHOTO'],
97
        );
98
99
        $this->videoAttributes = array(
100
            Article::ATTACHMENT_FIELD_VIDEO . self::JSON_ATTRIBUTES['ID'],
101
            Article::ATTACHMENT_FIELD_VIDEO . self::JSON_ATTRIBUTES['LEAD'],
102
            Article::ATTACHMENT_FIELD_VIDEO . self::JSON_ATTRIBUTES['BODY'],
103
            Article::ATTACHMENT_FIELD_VIDEO . self::JSON_ATTRIBUTES['SOURCE'],
104
            Article::ATTACHMENT_FIELD_VIDEO . self::JSON_ATTRIBUTES['ORDER'],
105
            Article::ATTACHMENT_FIELD_VIDEO . self::JSON_ATTRIBUTES['COVER'],
106
        );
107
    }
108
    /**
109
     * map a single article to main attributes in Article Class
110
     * @param  string $singleJsonArticle JSON response
111
     * @return Article
112
     * @throws Exception
113
     */
114
    public function article($singleJsonArticle)
115
    {
116
        if (json_decode($singleJsonArticle, true)) {
117
            $dataArticle = json_decode($singleJsonArticle, true)['data'];
118
119
            $article = new Article(
120
121
                $title = $this->filterString($this->getValue('title', $dataArticle)),
122
123
                $body = $this->filterString($this->getValue('body', $dataArticle)),
124
125
                $source = $this->filterString($this->getValue('source', $dataArticle)),
126
127
                $uniqueId = $this->getValue('unique_id', $dataArticle),
128
129
                $typeId = $this->filterInteger($this->getValue('type_id', $dataArticle['type'])),
130
131
                $categoryId = $this->filterInteger($this->getValue(
132
                    'category_id',
133
                    $dataArticle['category']
134
                )),
135
136
                $reporter = $this->getValue('reporter', $dataArticle),
137
138
                $lead = $this->filterString($this->getValue('lead', $dataArticle)),
139
140
                $tags = $this->getValue('tag_name', $dataArticle['tags']),
141
142
                $publishedAt = $this->filterString($this->getValue('published_at', $dataArticle)),
143
144
                $identifier = $this->filterInteger($this->getValue('id', $dataArticle))
145
146
            );
147
148
            $this->generalAttachment(
149
                $article,
150
                Article::ATTACHMENT_FIELD_PHOTO,
151
                self::JSON_PHOTO_FIELD,
152
                $this->photoAttributes,
153
                $dataArticle
154
            );
155
156
            $this->generalAttachment(
157
                $article,
158
                Article::ATTACHMENT_FIELD_PAGE,
159
                self::JSON_PAGE_FIELD,
160
                $this->pageAttributes,
161
                $dataArticle
162
            );
163
164
            $this->generalAttachment($article, Article::ATTACHMENT_FIELD_GALLERY, self::JSON_GALLERY_FIELD, $this->galleryAttributes, $dataArticle);
165
166
            $this->generalAttachment(
167
                $article,
168
                Article::ATTACHMENT_FIELD_VIDEO,
169
                self::JSON_VIDEO_FIELD,
170
                $this->videoAttributes,
171
                $dataArticle
172
            );
173
174
            return $article;
175
        }
176
177
        throw new \Exception("Empty or invalid JSON Response", 1);
178
    }
179
180
    /**
181
     * Attach attachments to article
182
     * @param  Article &$article
183
     * @param  string $attachmentConst
184
     * @param  string $attachmentype
185
     * @param  array $attributes
186
     * @param  array $dataArticle
187
     * @return void
188
     */
189
    private function generalAttachment(
190
        &$article,
191
        $attachmentConst,
192
        $attachmentype,
193
        $attributes,
194
        $dataArticle
195
    ) {
196
        $attachments = $this->attachment($attachmentype, $attributes, $dataArticle);
197
198
        for ($i = 0; $i < $attachments['numberOfItems']; $i++) {
199
            $attachment = $attachments['attachments'][$i];
200
201
            $article->attach($attachmentConst, $attachment);
202
        }
203
    }
204
205
    /**
206
     * Attachment(s) of a single article
207
     * @param  string $attachmentType
208
     * @param  array $attributes
209
     * @param  assoc array $dataArticle
210
     * @return array attachments
211
     */
212
    private function attachment($attachmentType, $attributes, $dataArticle)
213
    {
214
        $data = $dataArticle[$attachmentType];
215
216
        $encoded = json_encode($data);
217
218
        $attachments = array();
219
220
        if ($this->filterArray($data)) {
221
            $decodedData = json_decode($encoded, true);
222
223
            $numberOfItems = count($decodedData);
224
225
            for ($i = 0; $i < $numberOfItems; $i++) {
226
                $item = $decodedData[$i];
227
228
                $attachment = $this->makeAttachmentObject($attachmentType, $attributes, $item);
229
230
                array_push($attachments, $attachment);
231
            }
232
        }
233
234
        $structure = array(
235
            'numberOfItems' => count($attachments),
236
            'attachments' => $attachments,
237
        );
238
239
        return $structure;
240
    }
241
242
    /**
243
     * Make attachment object
244
     * @param  string $attachmentType json field of attachment
245
     * @param  array  $attributes     attributes of attachment
246
     * @param  assoc array $item
247
     * @return object
248
     */
249
    private function makeAttachmentObject($attachmentType, $attributes, $item)
250
    {
251
        $numOfAttributes = count($attributes);
252
253
        $values = array();
254
255
        $object = null;
256
257
        for ($i = 0; $i < $numOfAttributes; $i++) {
258
            $val = $this->getValue($attributes[$i], $item);
259
            $values[$attributes[$i]] = $val;
260
        }
261
262
        extract($values);
263
264
        if ($attachmentType == 'photos') {
265
            $object = new Photo(
266
                $photo_url,
267
                $this->photoRatio($photo_ratio),
268
                '',
269
                ''
270
            );
271
        } elseif ($attachmentType == 'pages') {
272
            $object = new Page(
273
                $page_title,
274
                $page_body,
275
                $page_source,
276
                $page_order,
277
                $page_cover,
278
                $page_lead
279
            );
280
        } elseif ($attachmentType == 'galleries') {
281
            $object = new Gallery(
282
                $gallery_body,
283
                $gallery_order,
284
                $gallery_photo,
285
                $gallery_source,
286
                $gallery_lead
287
            );
288
        } elseif ($attachmentType == 'videos') {
289
            $object = new Video(
290
291
                $video_body,
292
                $video_source,
293
                $video_order,
294
                $video_cover,
295
                $video_lead
296
            );
297
        }
298
299
        return $this->filterAttachmentObject($object);
300
    }
301
302
    /**
303
     * Map ratio to photo ratio constants
304
     * @param   string $ratio
305
     * @return   string
306
     * @throws Exception
307
     */
308
    private function photoRatio($ratio)
309
    {
310
        if ($ratio == "1:1") {
311
            return Photo::RATIO_SQUARE;
312
        } elseif ($ratio == "2:1") {
313
            return Photo::RATIO_RECTANGLE;
314
        } elseif ($ratio == "3:2") {
315
            return Photo::RATIO_HEADLINE;
316
        } elseif ($ratio == "9:16") {
317
            return Photo::RATIO_VERTICAL;
318
        } elseif ($ratio == 'cover') {
319
            return Photo::RATIO_COVER;
320
        } else {
321
            throw new \Exception("Unknown ratio", 1);
322
        }
323
    }
324
325
    /**
326
     * Make sure value is integer
327
     * @param  mixed $int
328
     * @return int
329
     * @throws Exception
330
     */
331
    private function filterInteger($int)
332
    {
333
        if (is_int($int)) {
334
            return $int;
335
        }
336
        throw new \Exception("Invalid Integer", 1);
337
    }
338
339
    /**
340
     * Make sure string is not null or empty
341
     * @param   mixed $str
342
     * @return string if it is valid or exception
343
     * @throws Exception
344
     */
345
    private function filterString($str)
346
    {
347
        if (is_string($str) && strlen($str) > 0 && !is_null($str)) {
348
            return $str;
349
        }
350
        throw new \Exception("String required", 1);
351
    }
352
353
    /**
354
     * Make sure variable is type of array
355
     * @param  mixed $array
356
     * @return array
357
     * @throws Exception
358
     */
359
    private function filterArray($array)
360
    {
361
        if (is_array($array)) {
362
            return $array;
363
        }
364
        throw new \Exception("Array required", 1);
365
    }
366
367
    /**
368
     * Make sure attachment object not null
369
     * @param  mixed $object
370
     * @return object
371
     * @throws Exception
372
     */
373
    private function filterAttachmentObject($object)
374
    {
375
        if (!is_null($object)) {
376
            return $object;
377
        }
378
        throw new \Exception("Attachment object required", 1);
379
    }
380
381
    /**
382
     * Get value of array based on attributes(keys)
383
     * @param  mixed $attribute
384
     * @param  array $data
385
     * @return mixed
386
     */
387
    private function getValue($attribute, $data)
388
    {
389
        return isset($data[$attribute]) ? $data[$attribute] : null;
390
    }
391
}
392