Completed
Push — master ( 2d0401...d6967e )
by Henry
15:58 queued 06:03
created

RankSorter::_sort()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 4
nc 3
nop 0
1
<?php
2
namespace Redaxscript\Modules\RankSorter;
3
4
use Redaxscript\Db;
5
use Redaxscript\Filter;
6
use Redaxscript\Head;
7
use Redaxscript\Header;
8
use Redaxscript\Module;
9
use function array_search;
10
use function json_encode;
11
12
/**
13
 * adjust the rank with draggable table rows
14
 *
15
 * @since 4.0.0
16
 *
17
 * @package Redaxscript
18
 * @category Modules
19
 * @author Henry Ruhs
20
 */
21
22
class RankSorter extends Module\Module
23
{
24
	/**
25
	 * array of the module
26
	 *
27
	 * @var array
28
	 */
29
30
	protected static $_moduleArray =
31
	[
32
		'name' => 'Rank Sorter',
33
		'alias' => 'RankSorter',
34
		'author' => 'Redaxmedia',
35
		'description' => 'Adjust the rank with draggable table rows',
36
		'version' => '4.3.0'
37
	];
38
39
	/**
40
	 * renderStart
41
	 *
42
	 * @since 4.0.0
43
	 */
44
45
	public function renderStart() : void
46
	{
47
		if ($this->_registry->get('loggedIn') === $this->_registry->get('token'))
48
		{
49
			/* link */
50
51
			$link = Head\Link::getInstance();
52
			$link
53
				->init()
54
				->appendFile('modules/RankSorter/dist/styles/rank-sorter.min.css');
55
56
			/* script */
57
58
			$script = Head\Script::getInstance();
59
			$script
60
				->init('foot')
61
				->appendFile(
62
				[
63
					'https://cdnjs.cloudflare.com/ajax/libs/dragula/3.7.2/dragula.min.js',
64
					'modules/RankSorter/assets/scripts/init.js',
65
					'modules/RankSorter/dist/scripts/rank-sorter.min.js'
66
				]);
67
68
			/* router */
69
70
			if ($this->_registry->get('firstParameter') === 'module' && $this->_registry->get('secondParameter') === 'rank-sorter' && $this->_registry->get('thirdParameter') === 'sort' && $this->_registry->get('tokenParameter'))
71
			{
72
				$this->_registry->set('renderBreak', true);
0 ignored issues
show
Documentation introduced by redaxmedia
true is of type boolean, but the function expects a string|array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
				echo $this->_sort();
74
			}
75
		}
76
	}
77
78
	/**
79
	 * sort
80
	 *
81
	 * @since 4.0.0
82
	 *
83
	 * @return string|null
84
	 */
85
86
	protected function _sort() : ?string
87
	{
88
		if ($this->_request->getServer('HTTP_X_REQUESTED_WITH') === 'XMLHttpRequest')
89
		{
90
			$postArray = $this->_sanitizePost();
91
			$contents = Db::forTablePrefix($postArray['table'])->whereIn('id', $postArray['rankArray'])->findMany();
92
93
			/* process contents */
94
95
			foreach ($contents as $value)
96
			{
97
				$value->set('rank', array_search($value->id, $postArray['rankArray']));
98
			}
99
100
			/* handle response */
101
102
			if ($contents->save())
103
			{
104
				Header::contentType('application/json');
105
				return json_encode($postArray['rankArray']);
106
			}
107
		}
108
		Header::responseCode(404);
109
		exit;
110
	}
111
112
	/**
113
	 * sanitize the post
114
	 *
115
	 * @since 4.0.0
116
	 *
117
	 * @return array
118
	 */
119
120
	protected function _sanitizePost() : array
121
	{
122
		$specialFilter = new Filter\Special();
123
124
		/* sanitize post */
125
126
		return
127
		[
128
			'table' => $specialFilter->sanitize($this->_request->getStream('table')),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_request->getStream('table') targeting Redaxscript\Request::getStream() can also be of type array; however, Redaxscript\Filter\Special::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
129
			'rankArray' => $this->_request->getStream('rankArray')
130
		];
131
	}
132
}
133