Completed
Push — master ( 4eb4a8...a52438 )
by Henry
07:48
created

includes/View/Extra.php (1 issue)

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 6
	public function __construct(Registry $registry, Request $request, Language $language, Config $config)
77
	{
78 6
		parent::__construct($registry, $language);
79 6
		$this->_request = $request;
80 6
		$this->_config = $config;
81 6
	}
82
83
	/**
84
	 * init the class
85
	 *
86
	 * @since 4.0.0
87
	 *
88
	 * @param array $optionArray options of the extra
89
	 *
90
	 * @return self
91
	 */
92
93 6
	public function init(array $optionArray = []) : self
94
	{
95 6
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
96 6
		return $this;
97
	}
98
99
	/**
100
	 * render the view
101
	 *
102
	 * @since 4.0.0
103
	 *
104
	 * @param int $extraId identifier of the extra
105
	 *
106
	 * @return string
107
	 */
108
109 6
	public function render(int $extraId = null) : string
110
	{
111 6
		if ($this->_registry->get('extraReplace'))
112
		{
113
			return Module\Hook::trigger('extraReplace');
114
		}
115 6
		$output = Module\Hook::trigger('extraStart');
116 6
		$accessValidator = new Validator\Access();
117 6
		$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...
118 6
		$contentParser = new Content\Parser($this->_registry, $this->_request, $this->_language, $this->_config);
119 6
		$adminDock = new Admin\View\Helper\Dock($this->_registry, $this->_language);
120 6
		$adminDock->init();
121 6
		$loggedIn = $this->_registry->get('loggedIn');
122 6
		$token = $this->_registry->get('token');
123 6
		$categoryId = $this->_registry->get('categoryId');
124 6
		$articleId = $this->_registry->get('articleId');
125 6
		$firstParameter = $this->_registry->get('firstParameter');
126 6
		$myGroups = $this->_registry->get('myGroups');
127
128
		/* html element */
129
130 6
		$element = new Html\Element();
131
		$titleElement = $element
132 6
			->copy()
133 6
			->init($this->_optionArray['tag']['title'],
134
			[
135 6
				'class' => $this->_optionArray['className']['title']
136
			]);
137
		$boxElement = $element
138 6
			->copy()
139 6
			->init($this->_optionArray['tag']['box'],
140
			[
141 6
				'class' => $this->_optionArray['className']['box']
142
			]);
143 6
		$extras = $this->queryExtras($extraId);
144
145
		/* process extras */
146
147 6
		foreach ($extras as $value)
148
		{
149 6
			$validateContent = true;
150 6
			if ($value->category)
151
			{
152 4
				$validateContent = (string)$value->category === $categoryId;
153
			}
154 6
			if ($value->article)
155
			{
156 4
				$validateContent = (string)$value->article === $articleId;
157
			}
158 6
			if ($accessValidator->validate($value->access, $myGroups) && $validateContent)
159
			{
160 6
				$output .= Module\Hook::trigger('extraFragmentStart', (array)$value);
161 6
				if ($value->headline)
162
				{
163
					$output .= $titleElement
164 6
						->attr('id', 'extra-' . $value->alias)
165 6
						->text($value->title);
166
				}
167 6
				$contentParser->process($value->text);
168 6
				$output .= $boxElement->html($contentParser->getOutput()) . Module\Hook::trigger('extraFragmentEnd', (array)$value);
169
170
				/* admin dock */
171
172 6
				if ($loggedIn === $token && $firstParameter !== 'logout')
173
				{
174 1
					$output .= $adminDock->render('extras', $value->id);
175
				}
176
			}
177
		}
178 6
		$output .= Module\Hook::trigger('extraEnd');
179 6
		return $output;
180
	}
181
182
	/**
183
	 * query the extras
184
	 *
185
	 * @since 4.0.0
186
	 *
187
	 * @param int $extraId identifier of the extra
188
	 *
189
	 * @return object|null
190
	 */
191
192 6
	public function queryExtras(int $extraId = null) : ?object
193
	{
194 6
		$extraModel = new Model\Extra();
195 6
		$language = $this->_registry->get('language');
196
197
		/* query extras */
198
199 6
		if ($extraId)
200
		{
201 2
			return $extraModel->getSiblingByIdAndLanguageAndOrder($extraId, $language, $this->_optionArray['orderColumn']);
202
		}
203 4
		return $extraModel->getByLanguageAndOrder($language, $this->_optionArray['orderColumn']);
204
	}
205
}
206