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

FormatMapping   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 143
rs 10
c 0
b 0
f 0
wmc 15
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
C mapMainArticle() 0 118 13
A jsonToArray() 0 10 2
1
<?php
2
3
namespace one;
4
5
use One\Model\Article;
6
7
class FormatMapping {
8
9
	/**
10
	 * map a single article to main attributes in Article Class
11
	 * @param  string $singleJsonArticle JSON response
12
	 * @return \One\Model\Article                   An Article Object
13
	 */
14
	public function mapMainArticle($singleJsonArticle) {
15
16
		$decoded_article = $singleJsonArticle;
17
18
		if (!is_null($this->jsonToArray($singleJsonArticle))) {
19
			$decoded_article = $this->jsonToArray($singleJsonArticle);
20
		}
21
22
		$data_article = $decoded_article['data'];
23
24
		$title = function () use ($data_article) {
25
			if (isset($data_article['title'])) {
26
				return $data_article['title'];
27
			}
28
			return null;
29
		};
30
31
		$body = function () use ($data_article) {
32
			if (isset($data_article['body'])) {
33
				return $data_article['body'];
34
			}
35
			return null;
36
		};
37
38
		$source = function () use ($data_article) {
39
			if (isset($data_article['source'])) {
40
				return $data_article['source'];
41
			}
42
			return null;
43
		};
44
45
		$uniqueId = function () use ($data_article) {
46
			if (isset($data_article['uniqueId'])) {
47
				return $data_article['uniqueId'];
48
			}
49
			return null;
50
		};
51
52
		$typeId = function () use ($data_article) {
53
			if (isset($data_article['type']['type_id'])) {
54
				return $data_article['type']['type_id'];
55
			}
56
			return null;
57
		};
58
59
		$categoryId = function () use ($data_article) {
60
			if (isset($data_article['category']['category_id'])) {
61
				return $data_article['category']['category_id'];
62
			}
63
			return null;
64
		};
65
66
		$reporter = function () use ($data_article) {
67
			if (isset($data_article['reporter'])) {
68
				return $data_article['reporter'];
69
			}
70
			return null;
71
		};
72
73
		$lead = function () use ($data_article) {
74
			if (isset($data_article['lead'])) {
75
				return $data_article['lead'];
76
			}
77
			return null;
78
		};
79
80
		$tags = function () use ($data_article) {
81
			if (isset($data_article['tags']['tag_name'])) {
82
				return $data_article['tags']['tag_name'];
83
			}
84
			return null;
85
		};
86
87
		$publishedAt = function () use ($data_article) {
88
			if (isset($data_article['published_at'])) {
89
				return $data_article['published_at'];
90
			}
91
			return null;
92
		};
93
94
		$identifier = function () use ($data_article) {
95
			if (isset($data_article['id'])) {
96
				return $data_article['id'];
97
			}
98
			return null;
99
		};
100
101
		$article = array();
0 ignored issues
show
Unused Code introduced by
$article is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
102
103
		$article = new Article(
104
105
			$title(),
106
107
			$body(),
108
109
			$source(),
110
111
			$uniqueId(),
112
113
			$typeId(),
114
115
			$categoryId(),
116
117
			$reporter(),
118
119
			$lead(),
120
121
			$tags(),
122
123
			$publishedAt(),
124
125
			$identifier()
126
127
		);
128
129
		return $article;
130
131
	}
132
133
	/**
134
	 * Convert JSON string to associative array
135
	 * @param  string $jsonResponse
136
	 * @return array if it is valid json, null otherwise
137
	 */
138
	public function jsonToArray($jsonResponse) {
139
140
		try {
141
			return json_decode($jsonResponse, TRUE);
142
143
		} catch (\Exception $e) {
144
			return null;
145
		}
146
147
	}
148
149
}
150