Completed
Push — master ( 29f0c5...0d72ed )
by Henry
09:47
created

Preview::_renderDashboard()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Redaxscript\Modules\Preview;
3
4
use Redaxscript\Filesystem;
5
use Redaxscript\Head;
6
use Redaxscript\Html;
7
use Redaxscript\Module;
8
use function str_replace;
9
10
/**
11
 * preview template elements
12
 *
13
 * @since 3.0.0
14
 *
15
 * @package Redaxscript
16
 * @category Modules
17
 * @author Henry Ruhs
18
 */
19
20
class Preview extends Module\Metadata
21
{
22
	/**
23
	 * array of the module
24
	 *
25
	 * @var array
26
	 */
27
28
	protected static $_moduleArray =
29
	[
30
		'name' => 'Preview',
31
		'alias' => 'Preview',
32
		'author' => 'Redaxmedia',
33
		'description' => 'Preview template elements',
34
		'version' => '4.0.0'
35
	];
36
37
	/**
38
	 * array of the option
39
	 *
40
	 * @var array
41
	 */
42
43
	protected $_optionArray =
44
	[
45
		'className' =>
46
		[
47
			'title' => 'rs-title-preview',
48
			'box' => 'rs-is-preview'
49
		]
50
	];
51
52
	/**
53
	 * renderStart
54
	 *
55
	 * @since 4.0.0
56
	 */
57
58
	public function renderStart() : void
59
	{
60
		if ($this->_registry->get('firstParameter') === 'module' && $this->_registry->get('secondParameter') === 'preview')
61
		{
62
			$link = Head\Link::getInstance();
63
			$link
64
				->init()
65
				->appendFile(
66
				[
67
					'modules/Dialog/dist/styles/dialog.min.css',
68
					'modules/Preview/dist/styles/preview.min.css'
69
				]);
70
		}
71
	}
72
73
	/**
74
	 * adminDashboard
75
	 *
76
	 * @since 4.1.0
77
	 *
78
	 * @return array
79
	 */
80
81
	public function adminDashboard() : array
82
	{
83
		$this->setDashboard($this->_renderDashboard());
84
		return $this->getDashboardArray();
85
	}
86
87
	/**
88
	 * routeHeader
89
	 *
90
	 * @since 3.3.0
91
	 */
92
93
	public function routeHeader() : void
94
	{
95
		if ($this->_registry->get('firstParameter') === 'module' && $this->_registry->get('secondParameter') === 'preview')
96
		{
97
			$this->_registry->set('useTitle', $this->_language->get('_preview')['preview']);
98
			$this->_registry->set('useDescription', $this->_language->get('_preview')['description']);
99
			$this->_registry->set('routerBreak', true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string|array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
100
		}
101
	}
102
103
	/**
104
	 * routeContent
105
	 *
106
	 * @since 3.3.0
107
	 */
108
109
	public function routeContent() : void
110
	{
111
		if ($this->_registry->get('firstParameter') === 'module' && $this->_registry->get('secondParameter') === 'preview')
112
		{
113
			echo $this->render();
114
		}
115
	}
116
117
	/**
118
	 * render
119
	 *
120
	 * @since 3.2.0
121
	 *
122
	 * @return string
123
	 */
124
125
	public function render() : string
126
	{
127
		$output = null;
128
		$templatesFilesystem = new Filesystem\File();
129
		$templatesFilesystem->init('modules/Preview/templates');
130
		$thirdParameter = $this->_registry->get('thirdParameter');
131
		$extension = '.phtml';
132
133
		/* collect output */
134
135
		if ($thirdParameter)
136
		{
137
			$output = $this->_renderPartial($thirdParameter, $templatesFilesystem->renderFile($thirdParameter . $extension));
0 ignored issues
show
Bug introduced by
It seems like $thirdParameter defined by $this->_registry->get('thirdParameter') on line 130 can also be of type array; however, Redaxscript\Modules\Prev...eview::_renderPartial() 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...
138
		}
139
		else
140
		{
141
			foreach ($templatesFilesystem->getSortArray() as $value)
142
			{
143
				$alias = str_replace($extension, '', $value);
144
				$output .= $this->_renderPartial($alias, $templatesFilesystem->renderFile($value));
145
			}
146
		}
147
		return $output;
148
	}
149
150
	/**
151
	 * renderPartial
152
	 *
153
	 * @since 3.2.0
154
	 *
155
	 * @param string $alias
156
	 * @param string $html
157
	 *
158
	 * @return string
159
	 */
160
161
	protected function _renderPartial(string $alias = null, string $html = null) : string
162
	{
163
		$thirdParameter = $this->_registry->get('thirdParameter');
164
		$parameterRoute = $this->_registry->get('parameterRoute');
165
166
		/* html element */
167
168
		$element = new Html\Element();
169
		$linkElement = $element
170
			->copy()
171
			->init('a',
172
			[
173
				'href' => $thirdParameter === $alias ? $parameterRoute . 'module/preview#' . $alias : $parameterRoute . 'module/preview/' . $alias
174
			])
175
			->text($thirdParameter === $alias ? $this->_language->get('back') : $alias);
0 ignored issues
show
Bug introduced by
It seems like $thirdParameter === $ali...e->get('back') : $alias can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, 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...
176
		$titleElement = $element
177
			->copy()
178
			->init('h2',
179
			[
180
				'class' => $this->_optionArray['className']['title'],
181
				'id' => $alias
182
			])
183
			->html($linkElement);
184
		$boxElement = $element
185
			->copy()
186
			->init('div',
187
			[
188
				'class' => $this->_optionArray['className']['box'],
189
			])
190
			->html($html);
191
192
		/* collect output */
193
194
		$output = $titleElement . $boxElement;
195
		return $output;
196
	}
197
198
	/**
199
	 * renderDashboard
200
	 *
201
	 * @since 4.1.0
202
	 *
203
	 * @return string
204
	 */
205
206
	protected function _renderDashboard() : string
207
	{
208
		$parameterRoute = $this->_registry->get('parameterRoute');
209
210
		/* html element */
211
212
		$element = new Html\Element();
213
		$linkElement = $element
214
			->copy()
215
			->init('a',
216
			[
217
				'href' => $parameterRoute . 'module/preview/'
218
			])
219
			->text($this->_language->get('_preview')['preview']);
220
221
		return $linkElement;
222
	}
223
}
224