Passed
Pull Request — master (#21)
by kenny
48:32 queued 25:20
created

FormatMapping::article()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 34
rs 9.376
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 Article\Exception
14
     */
15
    public function article($singleJsonArticle)
16
    {
17
        if ($this->jsonToArray($singleJsonArticle)) {
18
            $dataArticle = $this->jsonToArray($singleJsonArticle)['data'];
19
20
            $article = new Article(
21
                $title = $this->filterString($this->getValue('title', $dataArticle)),
22
23
                $body = $this->filterString($this->getValue('body', $dataArticle)),
24
25
                $source = $this->filterString($this->getValue('source', $dataArticle)),
26
27
                $uniqueId = $this->getValue('unique_id', $dataArticle),
28
29
                $typeId = $this->filterInteger($this->getValue('type_id', $dataArticle['type'])),
30
31
                $categoryId = $this->filterInteger($this->getValue('category_id', $dataArticle['category'])),
32
33
                $reporter = $this->getValue('reporter', $dataArticle),
34
35
                $lead = $this->filterString($this->getValue('lead', $dataArticle)),
36
37
                $tags = $this->getValue('tag_name', $dataArticle['tags']),
38
39
                $publishedAt = $this->filterString($this->getValue('published_at', $dataArticle)),
40
41
                $identifier = $this->filterInteger($this->getValue('id', $dataArticle))
42
            );
43
44
            return $article;
45
        }
46
47
        throw new \Exception("Empty or invalid JSON Response", 1);
48
    }
49
50
    /**
51
     * Make sure value is integer
52
     * @param  int $int
53
     * @return boolean
54
     */
55
    private function filterInteger($int)
56
    {
57
        if (is_int((int) $int)) {
58
            return $int;
59
        }
60
        throw new \Exception("Invalid Integer", 1);
61
    }
62
63
    /**
64
     * Make sure string is not null or empty
65
     * @param   mixed $str
66
     * @return string if it is valid or exception
67
     */
68
    private function filterString($str)
69
    {
70
        if (is_string($str) && strlen($str) > 0 && !is_null($str)) {
71
            return $str;
72
        }
73
        throw new \Exception("String required", 1);
74
    }
75
76
    /**
77
     * Get value of array based on attributes(keys)
78
     * @param  supported php variables $attribute
79
     * @param  array $data
80
     * @return supported php variables
81
     */
82
    private function getValue($attribute, $data)
83
    {
84
        return isset($data[$attribute]) ? $data[$attribute] : null;
85
    }
86
87
    /**
88
     * Convert JSON string to associative array
89
     * @param  string $jsonResponse
90
     * @return array if it is valid json, null otherwise
91
     */
92
    public function jsonToArray($jsonResponse)
93
    {
94
        try {
95
            return json_decode($jsonResponse, true);
96
        } catch (\Exception $e) {
97
            return null;
98
        }
99
    }
100
}
101