Completed
Push — master ( c3c208...858dca )
by Henry
08:18
created

Content   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 259
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 96.4%

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 8
dl 0
loc 259
c 0
b 0
f 0
ccs 107
cts 111
cp 0.964
rs 9.76

7 Methods

Rating   Name   Duplication   Size   Complexity  
A autorun() 0 7 2
B _setContent() 0 26 6
A _setTableByRoot() 0 11 2
B _setTableByParameter() 0 40 8
A _setIdByRoot() 0 33 4
B _setIdByParameter() 0 57 6
A _setId() 0 30 5
1
<?php
2
namespace Redaxscript\Bootstrap;
3
4
use Redaxscript\Db;
5
use Redaxscript\Model;
6
use Redaxscript\Validator;
7
8
/**
9
 * children class to boot the content
10
 *
11
 * @since 3.1.0
12
 *
13
 * @package Redaxscript
14
 * @category Bootstrap
15
 * @author Henry Ruhs
16
 */
17
18
class Content extends BootstrapAbstract
19
{
20
	/**
21
	 * automate run
22
	 *
23
	 * @since 3.1.0
24
	 */
25
26 9
	public function autorun() : void
27
	{
28 9
		if ($this->_registry->get('dbStatus') === 2)
29
		{
30 9
			$this->_setContent();
31
		}
32 9
	}
33
34
	/**
35
	 * set the content
36
	 *
37
	 * @since 3.1.0
38
	 */
39
40 9
	protected function _setContent() : void
41
	{
42 9
		$aliasValidator = new Validator\Alias();
43 9
		$firstParameter = $this->_registry->get('firstParameter');
44 9
		$secondParameter = $this->_registry->get('secondParameter');
45 9
		$lastParameter = $this->_registry->get('lastParameter');
46 9
		$lastSubParameter = $this->_registry->get('lastSubParameter');
47 9
		$isRoot = !$lastParameter && !$lastSubParameter;
48 9
		$isAdmin = $firstParameter === 'admin' && !$secondParameter;
49
50
		/* set by the root */
51
52 9
		if ($isRoot || $isAdmin)
53
		{
54 3
			$this->_setTableByRoot();
55 3
			$this->_setIdByRoot();
56
		}
57
58
		/* else set by the parameter */
59
60 6
		else if ($aliasValidator->validate($firstParameter))
0 ignored issues
show
Bug introduced by
It seems like $firstParameter defined by $this->_registry->get('firstParameter') on line 43 can also be of type array; however, Redaxscript\Validator\Alias::validate() 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...
61
		{
62 6
			$this->_setTableByParameter();
63 6
			$this->_setIdByParameter();
64
		}
65 9
	}
66
67
	/**
68
	 * set the table by root
69
	 *
70
	 * @since 3.1.0
71
	 */
72
73 3
	protected function _setTableByRoot() : void
74
	{
75 3
		$settingModel = new Model\Setting();
76 3
		$homepageId = $settingModel->get('homepage');
77 3
		$table = $homepageId > 0 ? 'articles' : 'categories';
78
79
		/* set the registry */
80
81 3
		$this->_registry->set('firstTable', $table);
82 3
		$this->_registry->set('lastTable', $table);
83 3
	}
84
85
	/**
86
	 * set the table by parameter
87
	 *
88
	 * @since 3.1.0
89
	 */
90
91 6
	protected function _setTableByParameter() : void
92
	{
93 6
		$contentModel = new Model\Content();
94 6
		$firstParameter = $this->_registry->get('firstParameter');
95 6
		$secondParameter = $this->_registry->get('secondParameter');
96 6
		$thirdParameter = $this->_registry->get('thirdParameter');
97
98
		/* set the registry */
99
100 6
		if ($thirdParameter)
101
		{
102 1
			$this->_registry->set('thirdTable', $contentModel->getTableByAlias($thirdParameter));
0 ignored issues
show
Bug introduced by
It seems like $thirdParameter defined by $this->_registry->get('thirdParameter') on line 96 can also be of type array; however, Redaxscript\Model\Content::getTableByAlias() 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...
103 1
			if ($this->_registry->get('thirdTable'))
104
			{
105 1
				$this->_registry->set('lastTable', $this->_registry->get('thirdTable'));
106 1
				$this->_registry->set('secondTable', $contentModel->getTableByAlias($secondParameter));
0 ignored issues
show
Bug introduced by
It seems like $secondParameter defined by $this->_registry->get('secondParameter') on line 95 can also be of type array; however, Redaxscript\Model\Content::getTableByAlias() 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...
107 1
				if ($this->_registry->get('secondTable'))
108
				{
109 1
					$this->_registry->set('firstTable', $contentModel->getTableByAlias($firstParameter));
0 ignored issues
show
Bug introduced by
It seems like $firstParameter defined by $this->_registry->get('firstParameter') on line 94 can also be of type array; however, Redaxscript\Model\Content::getTableByAlias() 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...
110
				}
111
			}
112
		}
113 5
		else if ($secondParameter)
114
		{
115 2
			$this->_registry->set('secondTable', $contentModel->getTableByAlias($secondParameter));
0 ignored issues
show
Bug introduced by
It seems like $secondParameter defined by $this->_registry->get('secondParameter') on line 95 can also be of type array; however, Redaxscript\Model\Content::getTableByAlias() 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...
116 2
			if ($this->_registry->get('secondTable'))
117
			{
118 2
				$this->_registry->set('lastTable', $this->_registry->get('secondTable'));
119 2
				$this->_registry->set('firstTable', $contentModel->getTableByAlias($firstParameter));
0 ignored issues
show
Bug introduced by
It seems like $firstParameter defined by $this->_registry->get('firstParameter') on line 94 can also be of type array; however, Redaxscript\Model\Content::getTableByAlias() 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...
120
			}
121
		}
122 3
		else if ($firstParameter)
123
		{
124 3
			$this->_registry->set('firstTable', $contentModel->getTableByAlias($firstParameter));
0 ignored issues
show
Bug introduced by
It seems like $firstParameter defined by $this->_registry->get('firstParameter') on line 94 can also be of type array; however, Redaxscript\Model\Content::getTableByAlias() 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...
125 3
			if ($this->_registry->get('firstTable'))
126
			{
127 1
				$this->_registry->set('lastTable', $this->_registry->get('firstTable'));
128
			}
129
		}
130 6
	}
131
132
	/**
133
	 * set the id by root
134
	 *
135
	 * @since 3.3.0
136
	 */
137
138 3
	protected function _setIdByRoot() : void
139
	{
140 3
		$settingModel = new Model\Setting();
141 3
		$homepageId = $settingModel->get('homepage');
142 3
		$order = $settingModel->get('order');
143 3
		$lastTable = $this->_registry->get('lastTable');
144 3
		$content = Db::forTablePrefix($lastTable);
0 ignored issues
show
Bug introduced by
It seems like $lastTable defined by $this->_registry->get('lastTable') on line 143 can also be of type array; however, Redaxscript\Db::forTablePrefix() 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...
145
146
		/* set by homepage */
147
148 3
		if ($homepageId > 0)
149
		{
150 1
			$this->_registry->set('articleId', $homepageId);
151 1
			$this->_registry->set('lastId', $homepageId);
152
		}
153
154
		/* set by order */
155
156 2
		else if ($order === 'asc')
157
		{
158 1
			$this->_setId($lastTable,
0 ignored issues
show
Bug introduced by
It seems like $lastTable defined by $this->_registry->get('lastTable') on line 143 can also be of type array; however, Redaxscript\Bootstrap\Content::_setId() 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...
159
			[
160 1
				'rank' => $content->min('rank')
161
			]);
162
		}
163 1
		else if ($order === 'desc')
164
		{
165 1
			$this->_setId($lastTable,
0 ignored issues
show
Bug introduced by
It seems like $lastTable defined by $this->_registry->get('lastTable') on line 143 can also be of type array; however, Redaxscript\Bootstrap\Content::_setId() 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...
166
			[
167 1
				'rank' => $content->max('rank')
168
			]);
169
		}
170 3
	}
171
172
	/**
173
	 * set the id by parameter
174
	 *
175
	 * @since 3.1.0
176
	 */
177
178 6
	protected function _setIdByParameter() : void
179
	{
180 6
		$categoryModel = new Model\Category();
181 6
		$firstTable = $this->_registry->get('firstTable');
182 6
		$secondTable = $this->_registry->get('secondTable');
183 6
		$thirdTable = $this->_registry->get('thirdTable');
184 6
		$firstParameter = $this->_registry->get('firstParameter');
185 6
		$secondParameter = $this->_registry->get('secondParameter');
186 6
		$thirdParameter = $this->_registry->get('thirdParameter');
187
188
		/* set by third table */
189
190 6
		if ($thirdTable === 'articles')
191
		{
192 1
			$this->_setId('articles',
193
			[
194 1
				'alias' => $thirdParameter,
195 1
				'category' => $categoryModel->query()->where('alias', $secondParameter)->findOne()->id
196
			]);
197
		}
198
199
		/* set by second table */
200
201 5
		else if ($secondTable === 'categories')
202
		{
203 1
			$this->_setId('categories',
204
			[
205 1
				'alias' => $secondParameter,
206 1
				'parent' => $categoryModel->query()->where('alias', $firstParameter)->findOne()->id
207
			]);
208
		}
209 4
		else if ($secondTable === 'articles')
210
		{
211 1
			$this->_setId('articles',
212
			[
213 1
				'alias' => $secondParameter,
214 1
				'category' => $categoryModel->query()->where('alias', $firstParameter)->findOne()->id
215
			]);
216
		}
217
218
		/* set by first table */
219
220 3
		else if ($firstTable === 'categories')
221
		{
222 1
			$this->_setId('categories',
223
			[
224 1
				'alias' => $firstParameter
225 1
			], 'parent');
226
		}
227 2
		else if ($firstTable === 'articles')
228
		{
229
			$this->_setId('articles',
230
			[
231
				'alias' => $firstParameter
232
			], 'category');
233
		}
234 6
	}
235
236
	/**
237
	 * set the id
238
	 *
239
	 * @since 3.1.0
240
	 *
241
	 * @param string $table
242
	 * @param array $whereArray
243
	 * @param string $whereNull
244
	 */
245
246 6
	protected function _setId(string $table = null, array $whereArray = [], string $whereNull = null) : void
247
	{
248 6
		$categoryModel = new Model\Category();
249 6
		$articleModel = new Model\Article();
250
251
		/* set the registry */
252
253 6
		if ($table === 'categories')
254
		{
255 4
			$category = $categoryModel->query()->where($whereArray)->where('status', 1);
256 4
			if ($whereNull)
0 ignored issues
show
Bug Best Practice introduced by
The expression $whereNull of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
257
			{
258 1
				$category->whereNull($whereNull);
259
			}
260 4
			$category = $category->findOne();
261 4
			$this->_registry->set('categoryId', $category->id);
262 4
			$this->_registry->set('lastId', $category->id);
263
		}
264 6
		if ($table === 'articles')
265
		{
266 2
			$article = $articleModel->query()->where($whereArray)->where('status', 1);
267 2
			if ($whereNull)
0 ignored issues
show
Bug Best Practice introduced by
The expression $whereNull of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
268
			{
269
				$article->whereNull($whereNull);
270
			}
271 2
			$article = $article->findOne();
272 2
			$this->_registry->set('articleId', $article->id);
273 2
			$this->_registry->set('lastId', $article->id);
274
		}
275 6
	}
276
}
277