Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
created

includes/View/Extra.php (1 issue)

Labels
Severity

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
104
	public function init(array $optionArray = []) : void
105
	{
106
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
107
	}
108
109
	/**
110
	 * render the view
111
	 *
112
	 * @since 4.0.0
113
	 *
114
	 * @param int $extraId identifier of the extra
115
	 *
116
	 * @return string
117
	 */
118
119
	public function render(int $extraId = null) : string
120
	{
121
		if ($this->_registry->get('extraReplace'))
122
		{
123
			return Module\Hook::trigger('extraReplace');
124
		}
125
		$output = Module\Hook::trigger('extraStart');
126
		$accessValidator = new Validator\Access();
127
		$extras = null;
128
		$contentParser = new Content\Parser($this->_registry, $this->_request, $this->_language, $this->_config);
129
		$adminDock = new Admin\View\Helper\Dock($this->_registry, $this->_language);
130
		$adminDock->init();
131
		$loggedIn = $this->_registry->get('loggedIn');
132
		$token = $this->_registry->get('token');
133
		$categoryId = $this->_registry->get('categoryId');
134
		$articleId = $this->_registry->get('articleId');
135
		$firstParameter = $this->_registry->get('firstParameter');
136
		$myGroups = $this->_registry->get('myGroups');
137
138
		/* html element */
139
140
		$element = new Html\Element();
141
		$titleElement = $element
142
			->copy()
143
			->init($this->_optionArray['tag']['title'],
144
			[
145
				'class' => $this->_optionArray['className']['title']
146
			]);
147
		$boxElement = $element
148
			->copy()
149
			->init($this->_optionArray['tag']['box'],
150
			[
151
				'class' => $this->_optionArray['className']['box']
152
			]);
153
		$extras = $this->queryExtras($extraId);
154
155
		/* process extras */
156
157
		foreach ($extras as $value)
158
		{
159
			$validateCategory = $categoryId === $value->category || !$value->category;
160
			$validateArticle = $articleId === $value->article || !$value->article;
161
			if ($accessValidator->validate($value->access, $myGroups) && ($validateCategory || $validateArticle))
162
			{
163
				$output .= Module\Hook::trigger('extraFragmentStart', (array)$value);
164
				if ($value->headline)
165
				{
166
					$output .= $titleElement
167
						->attr('id', 'extra-' . $value->alias)
168
						->text($value->title);
169
				}
170
				$contentParser->process($value->text);
171
				$output .= $boxElement->html($contentParser->getOutput()) . Module\Hook::trigger('extraFragmentEnd', (array)$value);
172
173
				/* admin dock */
174
175
				if ($loggedIn === $token && $firstParameter !== 'logout')
176
				{
177
					$output .= $adminDock->render('extras', $value->id);
178
				}
179
			}
180
		}
181
		$output .= Module\Hook::trigger('extraEnd');
182
		return $output;
183
	}
184
185
	/**
186
	 * query the extras
187
	 *
188
	 * @since 4.0.0
189
	 *
190
	 * @param int $extraId identifier of the extra
191
	 *
192
	 * @return object|null
193
	 */
194
195
	public function queryExtras(int $extraId = null) : ?object
196
	{
197
		$extraModel = new Model\Extra();
198
		$language = $this->_registry->get('language');
199
200
		/* query extras */
201
202
		if ($extraId)
203
		{
204
			return $extraModel->getByIdAndLanguageAndOrder($extraId, $language, $this->_optionArray['orderColumn']);
205
		}
206
		return $extraModel->getByLanguageAndOrder($language, $this->_optionArray['orderColumn']);
0 ignored issues
show
It seems like $language defined by $this->_registry->get('language') on line 198 can also be of type array; however, Redaxscript\Model\Conten...getByLanguageAndOrder() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
207
	}
208
}
209