Completed
Push — master ( dd1009...f3fdd4 )
by Henry
09:23
created

Article::render()   C

Complexity

Conditions 9
Paths 18

Size

Total Lines 80

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 80
ccs 0
cts 38
cp 0
rs 6.8808
c 0
b 0
f 0
cc 9
nc 18
nop 2
crap 90

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Model;
9
use Redaxscript\Module;
10
use Redaxscript\Request;
11
use Redaxscript\Registry;
12
use Redaxscript\Language;
13
use Redaxscript\Validator;
14
15
/**
16
 * children class to create the article
17
 *
18
 * @since 4.0.0
19
 *
20
 * @package Redaxscript
21
 * @category View
22
 * @author Henry Ruhs
23
 */
24
25
class Article extends ViewAbstract
26
{
27
	/**
28
	 * instance of the request class
29
	 *
30
	 * @var Request
31
	 */
32
33
	protected $_request;
34
35
	/**
36
	 * instance of the config class
37
	 *
38
	 * @var Config
39
	 */
40
41
	protected $_config;
42
43
	/**
44
	 * options of the article
45
	 *
46
	 * @var array
47
	 */
48
49
	protected $_optionArray =
50
	[
51
		'tag' =>
52
		[
53
			'title' => 'h2',
54
			'box' => 'div'
55
		],
56
		'className' =>
57
		[
58
			'title' => 'rs-title-content',
59
			'box' => 'rs-box-content'
60
		]
61
	];
62
63
	/**
64
	 * constructor of the class
65
	 *
66
	 * @since 4.0.0
67
	 *
68
	 * @param Registry $registry instance of the registry class
69
	 * @param Request $request instance of the request class
70
	 * @param Language $language instance of the language class
71
	 * @param Config $config instance of the config class
72
	 */
73
74
	public function __construct(Registry $registry, Request $request, Language $language, Config $config)
75
	{
76
		parent::__construct($registry, $language);
77
		$this->_request = $request;
78
		$this->_config = $config;
79
	}
80
81
	/**
82
	 * stringify the article
83
	 *
84
	 * @since 4.0.0
85
	 *
86
	 * @return string
87
	 */
88
89
	public function __toString() : string
90
	{
91
		return $this->render();
92
	}
93
94
	/**
95
	 * init the class
96
	 *
97
	 * @since 4.0.0
98
	 *
99
	 * @param array $optionArray options of the article
100
	 */
101
102
	public function init(array $optionArray = [])
103
	{
104
		if (is_array($optionArray))
105
		{
106
			$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
107
		}
108
	}
109
110
	/**
111
	 * render the view
112
	 *
113
	 * @since 4.0.0
114
	 *
115
	 * @param int $categoryId identifier of the category
116
	 * @param int $articleId identifier of the article
117
	 *
118
	 * @return string
119
	 */
120
121
	public function render(int $categoryId = null, int $articleId = null) : string
122
	{
123
		$output = Module\Hook::trigger('articleStart');
124
		$accessValidator = new Validator\Access();
125
		$articleModel = new Model\Article();
126
		$contentParser = new Content\Parser($this->_registry, $this->_request, $this->_language, $this->_config);
127
		$byline = new Helper\Byline($this->_registry, $this->_language);
128
		$byline->init();
129
		$adminDock = new Admin\View\Helper\Dock($this->_registry, $this->_language);
130
		$adminDock->init();
131
		$language = $this->_registry->get('language');
132
		$loggedIn = $this->_registry->get('loggedIn');
133
		$token = $this->_registry->get('token');
134
		$firstParameter = $this->_registry->get('firstParameter');
135
		$lastTable = $this->_registry->get('lastTable');
136
		$parameterRoute = $this->_registry->get('parameterRoute');
137
		$myGroups = $this->_registry->get('myGroups');
138
139
		/* html element */
140
141
		$element = new Html\Element();
142
		$titleElement = $element
143
			->copy()
144
			->init($this->_optionArray['tag']['title'],
145
			[
146
				'class' => $this->_optionArray['className']['title']
147
			]);
148
		$linkElement = $element->copy()->init('a');
149
		$boxElement = $element
150
			->copy()
151
			->init($this->_optionArray['tag']['box'],
152
			[
153
				'class' => $this->_optionArray['className']['box']
154
			]);
155
156
		/* query articles */
157
158
		if ($articleId)
0 ignored issues
show
Bug Best Practice introduced by
The expression $articleId of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
159
		{
160
			$articles = $articleModel->getByIdAndLanguage($articleId, $language);
161
		}
162
		else if ($categoryId)
0 ignored issues
show
Bug Best Practice introduced by
The expression $categoryId of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
163
		{
164
			$articles = $articleModel->getByCategoryAndLanguage($categoryId, $language);
165
		}
166
		else
167
		{
168
			$articles = $articleModel->getByLanguage();
169
		}
170
171
		/* process articles */
172
173
		foreach ($articles as $value)
174
		{
175
			if ($accessValidator->validate($value->access, $myGroups))
176
			{
177
				$output .= Module\Hook::trigger('articleFragmentStart', $value);
178
				if ((int)$value->headline === 1)
179
				{
180
					$output .= $titleElement
181
						->attr('id', 'article-' . $value->alias)
182
						->html($lastTable === 'categories' ? $linkElement
183
							->attr('href', $parameterRoute . $articleModel->getRouteById($value->id))
184
							->text($value->title) : $value->title
185
						);
186
				}
187
				$contentParser->process($value->text);
188
				$output .= $boxElement->html($contentParser->getOutput()) . $byline->render($value->date, $value->author) . Module\Hook::trigger('articleFragmentEnd', $value);
189
190
				/* admin dock */
191
192
				if ($loggedIn === $token && $firstParameter !== 'logout')
193
				{
194
					$output .= $adminDock->render('articles', $value->id);
195
				}
196
			}
197
		}
198
		$output .= Module\Hook::trigger('articleEnd');
199
		return $output;
200
	}
201
}
202