Completed
Push — master ( 1da492...320203 )
by Henry
07:00
created

includes/View/Extra.php (1 issue)

Check for unnecessary variable assignments.

Unused Code Major

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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