Passed
Pull Request — master (#21)
by kenny
02:17
created

FormatMapping   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 323
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 34
dl 0
loc 323
rs 9.68
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A attachment() 0 28 3
A __construct() 0 36 1
B makeAttachmentObject() 0 51 6
B article() 0 62 6
A filterAttachmentObject() 0 6 2
A filterString() 0 6 4
A photoRatio() 0 14 6
A filterInteger() 0 6 2
A getValue() 0 3 2
A filterArray() 0 6 2
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
    public function __construct()
42
    {
43
        $this->photoAttributes = array(
44
            "photo_id",
45
            "photo_url",
46
            "photo_ratio",
47
            "photo_description",
48
            "photo_information",
49
        );
50
51
        $this->pageAttributes = array(
52
            "page_id",
53
            "page_title",
54
            "page_lead",
55
            "page_body",
56
            "page_source",
57
            "page_order",
58
            "page_cover",
59
        );
60
61
        $this->galleryAttributes = array(
62
            "gallery_id",
63
            "gallery_lead",
64
            "gallery_body",
65
            "gallery_source",
66
            "gallery_order",
67
            "gallery_photo",
68
        );
69
70
        $this->videoAttributes = array(
71
            "video_id",
72
            "video_lead",
73
            "video_body",
74
            "video_source",
75
            "video_order",
76
            "video_cover",
77
        );
78
    }
79
    /**
80
     * map a single article to main attributes in Article Class
81
     * @param  string $singleJsonArticle JSON response
82
     * @return Article
83
     * @throws Exception
84
     */
85
    public function article($singleJsonArticle)
86
    {
87
        if (json_decode($singleJsonArticle, true)) {
88
            $dataArticle = json_decode($singleJsonArticle, true)['data'];
89
90
            $article = new Article(
91
92
                $title = $this->filterString($this->getValue('title', $dataArticle)),
93
94
                $body = $this->filterString($this->getValue('body', $dataArticle)),
95
96
                $source = $this->filterString($this->getValue('source', $dataArticle)),
97
98
                $uniqueId = $this->getValue('unique_id', $dataArticle),
99
100
                $typeId = $this->filterInteger($this->getValue('type_id', $dataArticle['type'])),
101
102
                $categoryId = $this->filterInteger($this->getValue(
103
                    'category_id',
104
                    $dataArticle['category']
105
                )),
106
107
                $reporter = $this->getValue('reporter', $dataArticle),
108
109
                $lead = $this->filterString($this->getValue('lead', $dataArticle)),
110
111
                $tags = $this->getValue('tag_name', $dataArticle['tags']),
112
113
                $publishedAt = $this->filterString($this->getValue('published_at', $dataArticle)),
114
115
                $identifier = $this->filterInteger($this->getValue('id', $dataArticle))
116
117
            );
118
119
            $photo_attachments = $this->attachment('photos', $this->photoAttributes, $dataArticle);
120
121
            $page_attachments = $this->attachment('pages', $this->pageAttributes, $dataArticle);
122
123
            $gallery_attachments = $this->attachment('galleries', $this->galleryAttributes, $dataArticle);
124
125
            $video_attachments = $this->attachment('videos', $this->videoAttributes, $dataArticle);
126
127
            for ($i = 0; $i < $photo_attachments['numberOfItems']; $i++) {
128
                $article->attachPhoto($photo_attachments['attachments'][$i]);
129
            }
130
131
            for ($i = 0; $i < $page_attachments['numberOfItems']; $i++) {
132
                $article->attachPage($page_attachments['attachments'][$i]);
133
            }
134
135
            for ($i = 0; $i < $gallery_attachments['numberOfItems']; $i++) {
136
                $article->attachGallery($gallery_attachments['attachments'][$i]);
137
            }
138
139
            for ($i = 0; $i < $video_attachments['numberOfItems']; $i++) {
140
                $article->attachVideo($video_attachments['attachments'][$i]);
141
            }
142
143
            return $article;
144
        }
145
146
        throw new \Exception("Empty or invalid JSON Response", 1);
147
    }
148
149
    /**
150
     * Attachment(s) of a single article
151
     * @param  string $attachmentType
152
     * @param  array $attributes
153
     * @param  assoc array $dataArticle
154
     * @return array attachments
155
     */
156
    private function attachment($attachmentType, $attributes, $dataArticle)
157
    {
158
        $data = $dataArticle[$attachmentType];
159
160
        $encoded = json_encode($data);
161
162
        $attachments = array();
163
164
        if ($this->filterArray($data)) {
165
            $decodedData = json_decode($encoded, true);
166
167
            $numberOfItems = count($decodedData);
168
169
            for ($i = 0; $i < $numberOfItems; $i++) {
170
                $item = $decodedData[$i];
171
172
                $attachment = $this->makeAttachmentObject($attachmentType, $attributes, $item);
173
174
                array_push($attachments, $attachment);
175
            }
176
        }
177
178
        $structure = array(
179
            'numberOfItems' => count($attachments),
180
            'attachments' => $attachments,
181
        );
182
183
        return $structure;
184
    }
185
186
    /**
187
     * Make attachment object
188
     * @param  string $attachmentType json field of attachment
189
     * @param  array  $attributes     attributes of attachment
190
     * @param  assoc array $item
191
     * @return object
192
     */
193
    private function makeAttachmentObject($attachmentType, $attributes, $item)
194
    {
195
        $numOfAttributes = count($attributes);
196
197
        $values = array();
198
199
        $object = null;
200
201
        for ($i = 0; $i < $numOfAttributes; $i++) {
202
            $val = $this->getValue($attributes[$i], $item);
203
            $values[$attributes[$i]] = $val;
204
        }
205
206
        extract($values);
207
208
        if ($attachmentType == 'photos') {
209
            $object = new Photo(
210
                $photo_url,
211
                $this->photoRatio($photo_ratio),
212
                '',
213
                ''
214
            );
215
        } elseif ($attachmentType == 'pages') {
216
            $object = new Page(
217
                $page_title,
218
                $page_body,
219
                $page_source,
220
                $page_order,
221
                $page_cover,
222
                $page_lead
223
            );
224
        } elseif ($attachmentType == 'galleries') {
225
            $object = new Gallery(
226
                $gallery_body,
227
                $gallery_order,
228
                $gallery_photo,
229
                $gallery_source,
230
                $gallery_lead
231
            );
232
        } elseif ($attachmentType == 'videos') {
233
            $object = new Video(
234
235
                $video_body,
236
                $video_source,
237
                $video_order,
238
                $video_cover,
239
                $video_lead
240
            );
241
        }
242
243
        return $this->filterAttachmentObject($object);
244
    }
245
246
    /**
247
     * Map ratio to photo ratio constants
248
     * @param   string $ratio
249
     * @return   string
250
     * @throws Exception
251
     */
252
    private function photoRatio($ratio)
253
    {
254
        if ($ratio == "1:1") {
255
            return Photo::RATIO_SQUARE;
256
        } elseif ($ratio == "2:1") {
257
            return Photo::RATIO_RECTANGLE;
258
        } elseif ($ratio == "3:2") {
259
            return Photo::RATIO_HEADLINE;
260
        } elseif ($ratio == "9:16") {
261
            return Photo::RATIO_VERTICAL;
262
        } elseif ($ratio == 'cover') {
263
            return Photo::RATIO_COVER;
264
        } else {
265
            throw new \Exception("Unknown ratio", 1);
266
        }
267
    }
268
269
    /**
270
     * Make sure value is integer
271
     * @param  mixed $int
272
     * @return int
273
     * @throws Exception
274
     */
275
    private function filterInteger($int)
276
    {
277
        if (is_int($int)) {
278
            return $int;
279
        }
280
        throw new \Exception("Invalid Integer", 1);
281
    }
282
283
    /**
284
     * Make sure string is not null or empty
285
     * @param   mixed $str
286
     * @return string if it is valid or exception
287
     * @throws Exception
288
     */
289
    private function filterString($str)
290
    {
291
        if (is_string($str) && strlen($str) > 0 && !is_null($str)) {
292
            return $str;
293
        }
294
        throw new \Exception("String required", 1);
295
    }
296
297
    /**
298
     * Make sure variable is type of array
299
     * @param  mixed $array
300
     * @return array
301
     * @throws Exception
302
     */
303
    private function filterArray($array)
304
    {
305
        if (is_array($array)) {
306
            return $array;
307
        }
308
        throw new \Exception("Array required", 1);
309
    }
310
311
    /**
312
     * Make sure attachment object not null
313
     * @param  mixed $object
314
     * @return object
315
     * @throws Exception
316
     */
317
    private function filterAttachmentObject($object)
318
    {
319
        if (!is_null($object)) {
320
            return $object;
321
        }
322
        throw new \Exception("Attachment object required", 1);
323
    }
324
325
    /**
326
     * Get value of array based on attributes(keys)
327
     * @param  mixed $attribute
328
     * @param  array $data
329
     * @return mixed
330
     */
331
    private function getValue($attribute, $data)
332
    {
333
        return isset($data[$attribute]) ? $data[$attribute] : null;
334
    }
335
}
336