Completed
Push — master ( cbd317...231b94 )
by Henry
09:43
created

includes/Bootstrap/Content.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\Bootstrap;
3
4
use Redaxscript\Db;
5
use Redaxscript\Model;
6
use Redaxscript\Validator;
7
use function is_numeric;
8
9
/**
10
 * children class to boot the content
11
 *
12
 * @since 3.1.0
13
 *
14
 * @package Redaxscript
15
 * @category Bootstrap
16
 * @author Henry Ruhs
17
 */
18
19
class Content extends BootstrapAbstract
20
{
21
	/**
22
	 * automate run
23
	 *
24
	 * @since 3.1.0
25
	 */
26 9
27
	public function autorun()
28 9
	{
29
		if ($this->_registry->get('dbStatus') === 2)
30 9
		{
31
			$this->_setContent();
32 9
		}
33
	}
34
35
	/**
36
	 * set the content
37
	 *
38
	 * @since 3.1.0
39
	 */
40 9
41
	protected function _setContent()
42 9
	{
43 9
		$firstParameter = $this->_registry->get('firstParameter');
44 9
		$secondParameter = $this->_registry->get('secondParameter');
45 9
		$lastParameter = $this->_registry->get('lastParameter');
46
		$lastSubParameter = $this->_registry->get('lastSubParameter');
47
48
		/* set by the root */
49 9
50
		if ((!$lastParameter && !is_numeric($lastSubParameter)) || ($firstParameter === 'admin' && !$secondParameter))
51 3
		{
52 3
			$this->_setTableByRoot();
53
			$this->_setIdByRoot();
54
		}
55
56
		/* else set by the parameter */
57
58
		else
59 6
		{
60 6
			$this->_setTableByParameter();
61
			$this->_setIdByParameter();
62 9
		}
63
	}
64
65
	/**
66
	 * set the table by root
67
	 *
68
	 * @since 3.1.0
69
	 */
70 3
71
	protected function _setTableByRoot()
72 3
	{
73 3
		$settingModel = new Model\Setting();
74 3
		$homepage = $settingModel->get('homepage');
75
		$table = $homepage > 0 ? 'articles' : 'categories';
76
77
		/* set the registry */
78 3
79 3
		$this->_registry->set('firstTable', $table);
80 3
		$this->_registry->set('lastTable', $table);
81
	}
82
83
	/**
84
	 * set the table by parameter
85
	 *
86
	 * @since 3.1.0
87
	 */
88 6
89
	protected function _setTableByParameter()
90 6
	{
91 6
		$firstParameter = $this->_registry->get('firstParameter');
92 6
		$secondParameter = $this->_registry->get('secondParameter');
93 6
		$thirdParameter = $this->_registry->get('thirdParameter');
94 6
		$lastParameter = $this->_registry->get('lastParameter');
95
		$contentModel = new Model\Content();
96
97
		/* set the registry */
98 6
99
		if ($firstParameter)
100 6
		{
101 6
			$this->_registry->set('firstTable', $contentModel->getTableByAlias($firstParameter));
102
			if ($this->_registry->get('firstTable'))
103 4
			{
104
				$this->_registry->set('secondTable', $contentModel->getTableByAlias($secondParameter));
105 6
			}
106
			if ($this->_registry->get('secondTable'))
107 3
			{
108
				$this->_registry->set('thirdTable', $contentModel->getTableByAlias($thirdParameter));
109 6
			}
110
			if ($this->_registry->get('lastParameter'))
111 6
			{
112
				$this->_registry->set('lastTable', $contentModel->getTableByAlias($lastParameter));
113
			}
114 6
		}
115
	}
116
117
	/**
118
	 * set the id
119
	 *
120
	 * @since 3.1.0
121
	 *
122
	 * @param array $whereArray
123
	 */
124 9
125
	protected function _setId(array $whereArray = [])
126 9
	{
127 9
		$aliasValidator = new Validator\Alias();
128 9
		$firstParameter = $this->_registry->get('firstParameter');
129
		$lastTable = $this->_registry->get('lastTable');
130
131
		/* set the registry */
132 9
133
		if ($firstParameter === 'admin' || !$aliasValidator->validate($firstParameter, 'system'))
134 8
		{
135
			if ($lastTable === 'categories')
136 4
			{
137 4
				$category = Db::forTablePrefix('categories')->where($whereArray)->findOne();
138 4
				$this->_registry->set('categoryId', $category->id);
139
				$this->_registry->set('lastId', $category->id);
140 8
			}
141
			if ($lastTable === 'articles')
142 3
			{
143 3
				$article = Db::forTablePrefix('articles')->where($whereArray)->findOne();
144 3
				$this->_registry->set('articleId', $article->id);
145
				$this->_registry->set('lastId', $article->id);
146
			}
147 9
		}
148
	}
149
150
	/**
151
	 * set the id by root
152
	 *
153
	 * @since 3.3.0
154
	 */
155 3
156
	protected function _setIdByRoot()
157 3
	{
158 3
		$settingModel = new Model\Setting();
159 3
		$order = $settingModel->get('order');
160 3
		$lastTable = $this->_registry->get('lastTable');
161
		$content = Db::forTablePrefix($lastTable);
0 ignored issues
show
It seems like $lastTable defined by $this->_registry->get('lastTable') on line 160 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...
162
163
		/* handle order */
164 3
165
		if ($order === 'asc')
166 2
		{
167
			$this->_setId(
168 2
			[
169 2
				'rank' => $content->min('rank'),
170
				'status' => 1
171
			]);
172 3
		}
173
		if ($order === 'desc')
174 1
		{
175
			$this->_setId(
176 1
			[
177 1
				'rank' => $content->max('rank'),
178
				'status' => 1
179
			]);
180 3
		}
181
	}
182
183
	/**
184
	 * set the id by parameter
185
	 *
186
	 * @since 3.1.0
187
	 */
188 6
189
	protected function _setIdByParameter()
190 6
	{
191 6
		$lastParameter = $this->_registry->get('lastParameter');
192
		if ($lastParameter)
193 6
		{
194
			$this->_setId(
195 6
			[
196 6
				'alias' => $lastParameter,
197
				'status' => 1
198
			]);
199 6
		}
200
	}
201
}
202