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

Extra::render()   C

Complexity

Conditions 10
Paths 22

Size

Total Lines 72

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 10.0015

Importance

Changes 0
Metric Value
dl 0
loc 72
ccs 39
cts 40
cp 0.975
rs 6.7442
c 0
b 0
f 0
cc 10
nc 22
nop 1
crap 10.0015

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\View;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Config;
6
use Redaxscript\Content;
7
use Redaxscript\Html;
8
use Redaxscript\Language;
9
use Redaxscript\Model;
10
use Redaxscript\Module;
11
use Redaxscript\Registry;
12
use Redaxscript\Request;
13
use Redaxscript\Validator;
14
use function array_replace_recursive;
15
16
/**
17
 * children class to create the extra
18
 *
19
 * @since 4.0.0
20
 *
21
 * @package Redaxscript
22
 * @category View
23
 * @author Henry Ruhs
24
 */
25
26
class Extra extends ViewAbstract
27
{
28
	/**
29
	 * options of the extra
30
	 *
31
	 * @var array
32
	 */
33
34
	protected array $_optionArray =
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...
35
	[
36
		'tag' =>
37
		[
38
			'title' => 'h3',
39
			'box' => 'div'
40
		],
41
		'className' =>
42
		[
43
			'title' => 'rs-title-extra',
44
			'box' => 'rs-box-extra'
45
		],
46
		'orderColumn' => 'rank'
47
	];
48
49
	/**
50
	 * constructor of the class
51
	 *
52
	 * @since 4.0.0
53
	 *
54
	 * @param Registry $_registry instance of the registry class
55
	 * @param Request $_request instance of the request class
56
	 * @param Language $_language instance of the language class
57
	 * @param Config $_config instance of the config class
58
	 */
59
60
	public function __construct(protected Registry $_registry, protected Request $_request, protected Language $_language, protected Config $_config)
61
	{
62
		parent::__construct($this->_registry, $this->_language);
63
	}
64
65
	/**
66
	 * init the class
67
	 *
68
	 * @since 4.0.0
69
	 *
70
	 * @param array $optionArray options of the extra
71
	 *
72
	 * @return self
73
	 */
74
75
	public function init(array $optionArray = []) : self
76 6
	{
77
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
78 6
		return $this;
79 6
	}
80 6
81 6
	/**
82
	 * render the view
83
	 *
84
	 * @since 4.0.0
85
	 *
86
	 * @param int $extraId identifier of the extra
87
	 *
88
	 * @return string
89
	 */
90
91
	public function render(int $extraId = null) : string
92
	{
93 6
		if ($this->_registry->get('extraReplace'))
94
		{
95 6
			return Module\Hook::trigger('extraReplace');
96 6
		}
97
		$output = Module\Hook::trigger('extraStart');
98
		$accessValidator = new Validator\Access();
99
		$extras = null;
100
		$contentParser = new Content\Parser($this->_registry, $this->_request, $this->_language, $this->_config);
101
		$adminDock = new Admin\View\Helper\Dock($this->_registry, $this->_language);
102
		$adminDock->init();
103
		$loggedIn = $this->_registry->get('loggedIn');
104
		$token = $this->_registry->get('token');
105
		$categoryId = $this->_registry->get('categoryId');
106
		$articleId = $this->_registry->get('articleId');
107
		$firstParameter = $this->_registry->get('firstParameter');
108
		$myGroups = $this->_registry->get('myGroups');
109 6
110
		/* html element */
111 6
112
		$element = new Html\Element();
113
		$titleElement = $element
114
			->copy()
115 6
			->init($this->_optionArray['tag']['title'],
116 6
			[
117 6
				'class' => $this->_optionArray['className']['title']
118 6
			]);
119 6
		$boxElement = $element
120 6
			->copy()
121 6
			->init($this->_optionArray['tag']['box'],
122 6
			[
123 6
				'class' => $this->_optionArray['className']['box']
124 6
			]);
125 6
		$extras = $this->queryExtras($extraId);
126 6
127
		/* process extras */
128
129
		foreach ($extras as $value)
130 6
		{
131
			$validateContent = true;
132 6
			if ($value->category)
133 6
			{
134
				$validateContent = (string)$value->category === $categoryId;
135 6
			}
136
			if ($value->article)
137
			{
138 6
				$validateContent = (string)$value->article === $articleId;
139 6
			}
140
			if ($accessValidator->validate($value->access, $myGroups) && $validateContent)
141 6
			{
142
				$output .= Module\Hook::trigger('extraFragmentStart', (array)$value);
143 6
				if ($value->headline)
144
				{
145
					$output .= $titleElement
146
						->attr('id', 'extra-' . $value->alias)
147 6
						->text($value->title);
148
				}
149 6
				$contentParser->process($value->text);
150 6
				$output .= $boxElement->html($contentParser->getOutput()) . Module\Hook::trigger('extraFragmentEnd', (array)$value);
151
152 4
				/* admin dock */
153
154 6
				if ($loggedIn === $token && $firstParameter !== 'logout')
155
				{
156 4
					$output .= $adminDock->render('extras', $value->id);
157
				}
158 6
			}
159
		}
160 6
		$output .= Module\Hook::trigger('extraEnd');
161 6
		return $output;
162
	}
163
164 6
	/**
165 6
	 * query the extras
166
	 *
167 6
	 * @since 4.0.0
168 6
	 *
169
	 * @param int $extraId identifier of the extra
170
	 *
171
	 * @return object|null
172 6
	 */
173
174 1
	public function queryExtras(int $extraId = null) : ?object
175
	{
176
		$extraModel = new Model\Extra();
177
		$language = $this->_registry->get('language');
178 6
179 6
		/* query extras */
180
181
		if ($extraId)
182
		{
183
			return $extraModel->getSiblingByIdAndLanguageAndOrder($extraId, $language, $this->_optionArray['orderColumn']);
184
		}
185
		return $extraModel->getByLanguageAndOrder($language, $this->_optionArray['orderColumn']);
186
	}
187
}
188