Completed
Push — master ( f0a9c0...16ddfb )
by Henry
08:56
created

includes/Bootstrap/Content.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\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, 'system'))
61
		{
62 5
			$this->_setTableByParameter();
63 5
			$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
		$homepage = $settingModel->get('homepage');
77 3
		$table = $homepage > 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 5
	protected function _setTableByParameter() : void
92
	{
93 5
		$contentModel = new Model\Content();
94 5
		$firstParameter = $this->_registry->get('firstParameter');
95 5
		$secondParameter = $this->_registry->get('secondParameter');
96 5
		$thirdParameter = $this->_registry->get('thirdParameter');
97 5
		$lastParameter = $this->_registry->get('lastParameter');
98
99
		/* set the registry */
100
101 5
		$this->_registry->set('firstTable', $contentModel->getTableByAlias($firstParameter));
102 5
		if ($this->_registry->get('firstTable'))
103
		{
104 4
			$this->_registry->set('secondTable', $contentModel->getTableByAlias($secondParameter));
105
		}
106 5
		if ($this->_registry->get('secondTable'))
107
		{
108 3
			$this->_registry->set('thirdTable', $contentModel->getTableByAlias($thirdParameter));
109
		}
110 5
		if ($this->_registry->get('lastParameter'))
111
		{
112 5
			$this->_registry->set('lastTable', $contentModel->getTableByAlias($lastParameter));
113
		}
114 5
	}
115
116
	/**
117
	 * set the id by root
118
	 *
119
	 * @since 3.3.0
120
	 */
121
122 3
	protected function _setIdByRoot() : void
123
	{
124 3
		$settingModel = new Model\Setting();
125 3
		$order = $settingModel->get('order');
126 3
		$lastTable = $this->_registry->get('lastTable');
127 3
		$content = Db::forTablePrefix($lastTable);
0 ignored issues
show
It seems like $lastTable defined by $this->_registry->get('lastTable') on line 126 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...
128
129
		/* handle order */
130
131 3
		if ($order === 'asc')
132
		{
133 2
			$this->_setId(
134
			[
135 2
				'rank' => $content->min('rank'),
136 2
				'status' => 1
137
			]);
138
		}
139 3
		if ($order === 'desc')
140
		{
141 1
			$this->_setId(
142
			[
143 1
				'rank' => $content->max('rank'),
144 1
				'status' => 1
145
			]);
146
		}
147 3
	}
148
149
	/**
150
	 * set the id by parameter
151
	 *
152
	 * @since 3.1.0
153
	 */
154
155 5
	protected function _setIdByParameter() : void
156
	{
157 5
		$lastParameter = $this->_registry->get('lastParameter');
158 5
		if ($lastParameter)
159
		{
160 5
			$this->_setId(
161
			[
162 5
				'alias' => $lastParameter,
163 5
				'status' => 1
164
			]);
165
		}
166 5
	}
167
168
	/**
169
	 * set the id
170
	 *
171
	 * @since 3.1.0
172
	 *
173
	 * @param array $whereArray
174
	 */
175
176 8
	protected function _setId(array $whereArray = []) : void
177
	{
178 8
		$categoryModel = new Model\Category();
179 8
		$articleModel = new Model\Article();
180 8
		$lastTable = $this->_registry->get('lastTable');
181
182
		/* set the registry */
183
184 8
		if ($lastTable === 'categories')
185
		{
186 4
			$category = $categoryModel->query()->where($whereArray)->findOne();
187 4
			$this->_registry->set('categoryId', $category->id);
188 4
			$this->_registry->set('lastId', $category->id);
189
		}
190 8
		if ($lastTable === 'articles')
191
		{
192 3
			$article = $articleModel->query()->where($whereArray)->findOne();
193 3
			$this->_registry->set('articleId', $article->id);
194 3
			$this->_registry->set('lastId', $article->id);
195
		}
196 8
	}
197
}
198