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

FormatMapping::mapMainArticle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
c 1
b 1
f 0
nc 4
nop 1
dl 0
loc 48
rs 9.1344
1
<?php
2
3
namespace one;
4
5
use One\Model\Article;
6
7
class FormatMapping
8
{
9
10
    /**
11
     * map a single article to main attributes in Article Class
12
     * @param  string $singleJsonArticle JSON response
13
     * @return \One\Model\Article                   An Article Object
14
     */
15
    public function mapMainArticle($singleJsonArticle)
16
    {
17
        $decodedArticle = $this->jsonToArray($singleJsonArticle) ? $this->jsonToArray($singleJsonArticle) : $singleJsonArticle;
18
19
        if (!is_null($this->jsonToArray($singleJsonArticle))) {
20
            $decodedArticle = $this->jsonToArray($singleJsonArticle);
21
        }
22
23
        $dataArticle = $decodedArticle['data'];
24
25
        $title = $this->getValue('title', $dataArticle);
26
27
        $body = $this->getValue('body', $dataArticle);
28
29
        $source = $this->getValue('source', $dataArticle);
30
31
        $uniqueId = $this->getValue('unique_id', $dataArticle);
32
33
        $typeId = $this->getValue('type_id', $dataArticle['type']);
34
35
        $categoryId = $this->getValue('category_id', $dataArticle['category']);
36
37
        $reporter = $this->getValue('reporter', $dataArticle);
38
39
        $lead = $this->getValue('lead', $dataArticle);
40
41
        $tags = $this->getValue('tag_name', $dataArticle['tags']);
42
43
        $publishedAt = $this->getValue('published_at', $dataArticle);
44
45
        $identifier = $this->getValue('id', $dataArticle);
46
47
        $article = new Article(
48
            $title,
49
            $body,
50
            $source,
51
            $uniqueId,
52
            $typeId,
53
            $categoryId,
54
            $reporter,
55
            $lead,
56
            $tags,
57
            $publishedAt,
58
            $identifier
59
        );
60
61
        return $article;
62
    }
63
64
    /**
65
     * Get value of array based on attributes(keys)
66
     * @param  string,int $attribute
0 ignored issues
show
Documentation introduced by
The doc-type string,int could not be parsed: Expected "|" or "end of type", but got "," at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
67
     * @param  array $data
68
     * @return supported php variables
69
     */
70
    private function getValue($attribute, $data)
71
    {
72
        if (isset($data[$attribute])) {
73
            return $data[$attribute];
74
        }
75
        return null;
76
    }
77
78
    /**
79
     * Convert JSON string to associative array
80
     * @param  string $jsonResponse
81
     * @return array if it is valid json, null otherwise
82
     */
83
    public function jsonToArray($jsonResponse)
84
    {
85
        try {
86
            return json_decode($jsonResponse, true);
87
        } catch (\Exception $e) {
88
            return null;
89
        }
90
    }
91
}
92