Completed
Push — master ( 17a370...d70e31 )
by Michael
02:18
created

StatsJsonView::render()   D

Complexity

Conditions 27
Paths 56

Size

Total Lines 148
Code Lines 68

Duplication

Lines 8
Ratio 5.41 %

Code Coverage

Tests 91
CRAP Score 27.0009

Importance

Changes 7
Bugs 0 Features 0
Metric Value
c 7
b 0
f 0
dl 8
loc 148
ccs 91
cts 92
cp 0.9891
rs 4.509
cc 27
eloc 68
nc 56
nop 0
crap 27.0009

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Stats\Views\Stats;
4
5
use Joomla\View\BaseJsonView;
6
7
/**
8
 * JSON response for requesting the stats data.
9
 *
10
 * @property-read  \Stats\Models\StatsModel  $model  The model object.
11
 *
12
 * @since          1.0
13
 */
14
class StatsJsonView extends BaseJsonView
15
{
16
	/**
17
	 * Flag if the response should return the raw data.
18
	 *
19
	 * @var    boolean
20
	 * @since  1.0
21
	 */
22
	private $authorizedRaw = false;
23
24
	/**
25
	 * The data source to return.
26
	 *
27
	 * @var    string
28
	 * @since  1.0
29
	 */
30
	private $source;
31
32
	/**
33
	 * Set whether the raw data should be returned.
34
	 *
35
	 * @param   boolean  $authorizedRaw  Flag if the response should return the raw data.
36
	 *
37
	 * @return  void
38
	 *
39
	 * @since   1.0
40
	 */
41 1
	public function isAuthorizedRaw($authorizedRaw)
42
	{
43 1
		$this->authorizedRaw = $authorizedRaw;
44 1
	}
45
46
	/**
47
	 * Method to render the view.
48
	 *
49
	 * @return  string  The rendered view.
50
	 *
51
	 * @since   1.0
52
	 * @throws  \InvalidArgumentException
53
	 */
54 3
	public function render()
55
	{
56 3
		$items = $this->model->getItems($this->source);
57
58
		$data = [
59 3
			'php_version' => [],
60 3
			'db_type'     => [],
61 3
			'db_version'  => [],
62 3
			'cms_version' => [],
63 3
			'server_os'   => []
64 3
		];
65
66 3
		foreach ($items as $item)
67
		{
68 3
			foreach ($data as $key => $value)
69
			{
70 3
				if (isset($item->$key) && !is_null($item->$key))
71 3
				{
72
					// Special case, if the server is empty then change the key to "unknown"
73 3
					if ($key === 'server_os' && empty($item->$key))
74 3
					{
75 2
						if (!isset($data[$key]['unknown']))
76 2
						{
77 2
							$data[$key]['unknown'] = 0;
78 2
						}
79
80 2
						$data[$key]['unknown']++;
81 2
					}
82
					else
83
					{
84 3
						if (!isset($data[$key][$item->$key]))
85 3
						{
86 3
							$data[$key][$item->$key] = 0;
87 3
						}
88
89 3
						$data[$key][$item->$key]++;
90
					}
91 3
				}
92 3
			}
93 3
		}
94
95 3
		$responseData = [];
96
97 3
		foreach ($data as $key => $value)
98
		{
99 3
			foreach ($value as $name => $count)
100
			{
101
				if ($name)
102 3
				{
103 3
					$responseData[$key][] = [
104 3
						'name'  => $name,
105
						'count' => $count
106 3
					];
107 3
				}
108 3
			}
109 3
		}
110
111 3
		$total = count($items);
112
113 3
		if (!$this->authorizedRaw)
114 3
		{
115 2
			foreach ($responseData as $key => $dataGroup)
116
			{
117
				switch ($key)
118
				{
119 2
					case 'php_version':
120 2
					case 'db_version':
121
						// We're going to group by minor version branch here and convert to a percentage
122 2
						$counts = [];
123
124 2
						foreach ($dataGroup as $row)
125
						{
126 2
							$exploded = explode('.', $row['name']);
127 2
							$version  = $exploded[0] . '.' . (isset($exploded[1]) ? $exploded[1] : '0');
128
129
							// If the container does not exist, add it
130 2
							if (!isset($counts[$version]))
131 2
							{
132 2
								$counts[$version] = 0;
133 2
							}
134
135 2
							$counts[$version] += $row['count'];
136 2
						}
137
138 2
						$sanitizedData = [];
139
140 2 View Code Duplication
						foreach ($counts as $version => $count)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
						{
142 2
							$sanitizedData[$version] = round(($count / $total) * 100, 2);
143 2
						}
144
145 2
						$responseData[$key] = $sanitizedData;
146
147 2
						break;
148
149 1
					case 'server_os':
150
						// We're going to group by operating system here
151 1
						$counts = [];
152
153 1
						foreach ($dataGroup as $row)
154
						{
155 1
							$fullOs = explode(' ', $row['name']);
156 1
							$os     = $fullOs[0];
157
158
							// If the container does not exist, add it
159 1
							if (!isset($counts[$os]))
160 1
							{
161 1
								$counts[$os] = 0;
162 1
							}
163
164 1
							$counts[$os] += $row['count'];
165 1
						}
166
167 1
						$sanitizedData = [];
168
169 1 View Code Duplication
						foreach ($counts as $os => $count)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
						{
171 1
							$sanitizedData[$os] = round(($count / $total) * 100, 2);
172 1
						}
173
174 1
						$responseData[$key] = $sanitizedData;
175
176 1
						break;
177
178 1
					case 'db_type':
179 1
					case 'cms_version':
180 1
					default:
181
						// For now, group by the object name and figure out the percentages
182 1
						$sanitizedData = [];
183
184 1
						foreach ($dataGroup as $row)
185
						{
186 1
							$sanitizedData[$row['name']] = round(($row['count'] / $total) * 100, 2);
187 1
						}
188
189 1
						$responseData[$key] = $sanitizedData;
190
191 1
						break;
192
				}
193 2
			}
194 2
		}
195
196 3
		$responseData['total'] = $total;
197
198 3
		$this->addData('data', $responseData);
199
200 3
		return parent::render();
201
	}
202
203
	/**
204
	 * Set the data source.
205
	 *
206
	 * @param   string  $source  Data source to return.
207
	 *
208
	 * @return  void
209
	 *
210
	 * @since   1.0
211
	 */
212 1
	public function setSource($source)
213
	{
214 1
		$this->source = $source;
215 1
	}
216
}
217