Completed
Push — master ( a52438...bf8826 )
by Henry
06:30
created

Reporter::_renderDashboard()   F

Complexity

Conditions 10
Paths 512

Size

Total Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 82
rs 3.7349
c 0
b 0
f 0
cc 10
nc 512
nop 1

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
namespace Redaxscript\Modules\Reporter;
3
4
use Redaxscript\Filesystem;
5
use Redaxscript\Html;
6
use Redaxscript\Model;
7
use Redaxscript\Module;
8
9
/**
10
 * report metrics on the dashboard
11
 *
12
 * @since 4.1.0
13
 *
14
 * @package Redaxscript
15
 * @category Modules
16
 * @author Henry Ruhs
17
 */
18
19
class Reporter extends Module\Metadata
20
{
21
	/**
22
	 * array of the module
23
	 *
24
	 * @var array
25
	 */
26
27
	protected static array $_moduleArray =
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
28
	[
29
		'name' => 'Reporter',
30
		'alias' => 'Reporter',
31
		'author' => 'Redaxmedia',
32
		'description' => 'Report metrics on the dashboard',
33
		'version' => '5.0.0',
34
		'license' => 'MIT'
35
	];
36
37
	/**
38
	 * array of the option
39
	 *
40
	 * @var array
41
	 */
42
43
	protected array $_optionArray =
44
	[
45
		'className' =>
46
		[
47
			'dashboard' =>
48
			[
49
				'title' => 'rs-admin-title-dashboard',
50
				'text' => 'rs-admin-text-dashboard'
51
			]
52
		]
53
	];
54
55
	/**
56
	 * adminDashboard
57
	 *
58
	 * @since 4.1.0
59
	 *
60
	 * @return array
61
	 */
62
63
	public function adminDashboard() : array
64
	{
65
		$this->setDashboard($this->_renderDashboard('categories'));
66
		$this->setDashboard($this->_renderDashboard('articles'));
67
		$this->setDashboard($this->_renderDashboard('extras'));
68
		$this->setDashboard($this->_renderDashboard('comments'));
69
		$this->setDashboard($this->_renderDashboard('users'));
70
		$this->setDashboard($this->_renderDashboard('groups'));
71
		$this->setDashboard($this->_renderDashboard('modules'));
72
		$this->setDashboard($this->_renderDashboard('languages'));
73
		$this->setDashboard($this->_renderDashboard('templates'));
74
		return $this->getDashboardArray();
75
	}
76
77
	/**
78
	 * renderDashboard
79
	 *
80
	 * @since 4.1.0
81
	 *
82
	 * @param string type
83
	 * @return string
84
	 */
85
86
	protected function _renderDashboard(string $type = null) : string
87
	{
88
		$categoryModel = new Model\Category();
89
		$articleModel = new Model\Article();
90
		$extraModel = new Model\Extra();
91
		$commentModel = new Model\Comment();
92
		$userModel = new Model\User();
93
		$groupModel = new Model\Group();
94
		$moduleModel = new Model\Module();
95
		$filesystem = new Filesystem\Filesystem();
96
		$divider = '/';
97
98
		/* html element */
99
100
		$element = new Html\Element();
101
		$titleElement = $element
102
			->copy()
103
			->init('h3',
104
			[
105
				'class' => $this->_optionArray['className']['dashboard']['title']
106
			]);
107
		$textElement = $element
108
			->copy()
109
			->init('span',
110
			[
111
				'class' => $this->_optionArray['className']['dashboard']['text']
112
			]);
113
114
		/* handle type */
115
116
		if ($type === 'categories')
117
		{
118
			$titleElement->text($categoryModel->query()->where('status', 1)->count() . $divider . $categoryModel->query()->count());
119
			$textElement->text($this->_language->get('_reporter')['categories_publish']);
120
		}
121
		if ($type === 'articles')
122
		{
123
			$titleElement->text($articleModel->query()->where('status', 1)->count() . $divider . $articleModel->query()->count());
124
			$textElement->text($this->_language->get('_reporter')['articles_publish']);
125
		}
126
		if ($type === 'extras')
127
		{
128
			$titleElement->text($extraModel->query()->where('status', 1)->count() . $divider . $extraModel->query()->count());
129
			$textElement->text($this->_language->get('_reporter')['extras_publish']);
130
		}
131
		if ($type === 'comments')
132
		{
133
			$titleElement->text($commentModel->query()->where('status', 1)->count() . $divider . $commentModel->query()->count());
134
			$textElement->text($this->_language->get('_reporter')['comments_publish']);
135
		}
136
		if ($type === 'users')
137
		{
138
			$titleElement->text($userModel->query()->where('status', 1)->count() . $divider . $userModel->query()->count());
139
			$textElement->text($this->_language->get('_reporter')['users_enable']);
140
		}
141
		if ($type === 'groups')
142
		{
143
			$titleElement->text($groupModel->query()->where('status', 1)->count() . $divider . $groupModel->query()->count());
144
			$textElement->text($this->_language->get('_reporter')['groups_enable']);
145
		}
146
		if ($type === 'modules')
147
		{
148
			$titleElement->text($moduleModel->query()->count() . $divider . $filesystem->init('modules')->countIterator());
149
			$textElement->text($this->_language->get('_reporter')['modules_install']);
150
		}
151
		if ($type === 'languages')
152
		{
153
			$titleElement->text($filesystem->init('languages')->countIterator());
154
			$textElement->text($this->_language->get('_reporter')['languages_install']);
155
		}
156
		if ($type === 'templates')
157
		{
158
			$titleElement->text($filesystem->init('templates', false,
159
			[
160
				'admin',
161
				'console',
162
				'install'
163
			])->countIterator());
164
			$textElement->text($this->_language->get('_reporter')['templates_install']);
165
		}
166
		return $titleElement . $textElement;
167
	}
168
}
169