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