Completed
Push — master ( 115248...91d2c9 )
by Henry
78:30 queued 58:40
created

includes/View/Article.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\View;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Config;
6
use Redaxscript\Content;
7
use Redaxscript\Html;
8
use Redaxscript\Language;
9
use Redaxscript\Model;
10
use Redaxscript\Module;
11
use Redaxscript\Registry;
12
use Redaxscript\Request;
13
use Redaxscript\Template;
14
use Redaxscript\Validator;
15
16
/**
17
 * children class to create the article
18
 *
19
 * @since 4.0.0
20
 *
21
 * @package Redaxscript
22
 * @category View
23
 * @author Henry Ruhs
24
 */
25
26
class Article extends ViewAbstract
27
{
28
	/**
29
	 * instance of the request class
30
	 *
31
	 * @var Request
32
	 */
33
34
	protected $_request;
35
36
	/**
37
	 * instance of the config class
38
	 *
39
	 * @var Config
40
	 */
41
42
	protected $_config;
43
44
	/**
45
	 * options of the article
46
	 *
47
	 * @var array
48
	 */
49
50
	protected $_optionArray =
51
	[
52
		'tag' =>
53
		[
54
			'title' => 'h2',
55
			'box' => 'div'
56
		],
57
		'className' =>
58
		[
59
			'title' => 'rs-title-content',
60
			'box' => 'rs-box-content'
61
		],
62
		'orderColumn' => 'rank',
63
		'partial' =>
64
		[
65
			'error' => 'error.phtml'
66
		]
67
	];
68
69
	/**
70
	 * constructor of the class
71
	 *
72
	 * @since 4.0.0
73
	 *
74
	 * @param Registry $registry instance of the registry class
75
	 * @param Request $request instance of the request class
76
	 * @param Language $language instance of the language class
77
	 * @param Config $config instance of the config class
78
	 */
79
80
	public function __construct(Registry $registry, Request $request, Language $language, Config $config)
81
	{
82
		parent::__construct($registry, $language);
83
		$this->_request = $request;
84
		$this->_config = $config;
85
	}
86
87
	/**
88
	 * stringify the article
89
	 *
90
	 * @since 4.0.0
91
	 *
92
	 * @return string
93
	 */
94
95
	public function __toString() : string
96
	{
97
		return $this->render();
98
	}
99
100
	/**
101
	 * init the class
102
	 *
103
	 * @since 4.0.0
104
	 *
105
	 * @param array $optionArray options of the article
106
	 */
107
108
	public function init(array $optionArray = [])
109
	{
110
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
111
	}
112
113
	/**
114
	 * render the view
115
	 *
116
	 * @since 4.0.0
117
	 *
118
	 * @param int $categoryId identifier of the category
119
	 * @param int $articleId identifier of the article
120
	 *
121
	 * @return string
122
	 */
123
124
	public function render(int $categoryId = null, int $articleId = null) : string
125
	{
126
		if ($this->_registry->get('articleReplace'))
127
		{
128
			return Module\Hook::trigger('articleReplace');
129
		}
130
		$output = Module\Hook::trigger('articleStart');
131
		$accessValidator = new Validator\Access();
132
		$settingModel = new Model\Setting();
133
		$articleModel = new Model\Article();
134
		$articles = null;
135
		$contentParser = new Content\Parser($this->_registry, $this->_request, $this->_language, $this->_config);
136
		$byline = new Helper\Byline($this->_registry, $this->_language);
137
		$byline->init();
138
		$adminDock = new Admin\View\Helper\Dock($this->_registry, $this->_language);
139
		$adminDock->init();
140
		$language = $this->_registry->get('language');
141
		$loggedIn = $this->_registry->get('loggedIn');
142
		$token = $this->_registry->get('token');
143
		$firstParameter = $this->_registry->get('firstParameter');
144
		$lastSubParameter = $this->_registry->get('lastSubParameter');
145
		$lastTable = $this->_registry->get('lastTable');
146
		$parameterRoute = $this->_registry->get('parameterRoute');
147
		$myGroups = $this->_registry->get('myGroups');
148
149
		/* html element */
150
151
		$element = new Html\Element();
152
		$titleElement = $element
153
			->copy()
154
			->init($this->_optionArray['tag']['title'],
155
			[
156
				'class' => $this->_optionArray['className']['title']
157
			]);
158
		$linkElement = $element->copy()->init('a');
159
		$boxElement = $element
160
			->copy()
161
			->init($this->_optionArray['tag']['box'],
162
			[
163
				'class' => $this->_optionArray['className']['box']
164
			]);
165
166
		/* query articles */
167
168
		if ($categoryId)
169
		{
170
			if ($settingModel->get('pagination'))
171
			{
172
				$articles = $articleModel->getByCategoryAndLanguageAndOrderAndStep($categoryId, $language, $this->_optionArray['orderColumn'], $lastSubParameter - 1);
173
			}
174
			else
175
			{
176
				$articles = $articleModel->getByCategoryAndLanguageAndOrder($categoryId, $language, $this->_optionArray['orderColumn']);
177
			}
178
		}
179
		else if ($articleId)
180
		{
181
			$articles = $articleModel->getByIdAndLanguageAndOrder($articleId, $language, $this->_optionArray['orderColumn']);
182
		}
183
184
		/* process articles */
185
186
		if ($articles->count())
187
		{
188
			foreach ($articles as $value)
0 ignored issues
show
The expression $articles of type object|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
189
			{
190
				if ($accessValidator->validate($value->access, $myGroups))
191
				{
192
					$output .= Module\Hook::trigger('articleFragmentStart', (array)$value);
193
					if ($value->headline)
194
					{
195
						$output .= $titleElement
196
							->attr('id', 'article-' . $value->alias)
197
							->html($lastTable === 'categories' ? $linkElement
198
								->attr('href', $parameterRoute . $articleModel->getRouteById($value->id))
199
								->text($value->title) : $value->title
200
							);
201
					}
202
					$contentParser->process($value->text);
203
					$output .= $boxElement->html($contentParser->getOutput()) . $byline->render($value->date, $value->author) . Module\Hook::trigger('articleFragmentEnd', (array)$value);
204
205
					/* admin dock */
206
207
					if ($loggedIn === $token && $firstParameter !== 'logout')
208
					{
209
						$output .= $adminDock->render('articles', $value->id);
210
					}
211
				}
212
			}
213
		}
214
		else
215
		{
216
			$output .= Template\Tag::partial($this->_registry->get('template') . '/' . $this->_optionArray['partial']['error']);
217
		}
218
		$output .= Module\Hook::trigger('articleEnd');
219
		return $output;
220
	}
221
}
222