RApiSoapOperationOperation::readList()   F
last analyzed

Complexity

Conditions 12
Paths 384

Size

Total Lines 49
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 49
rs 3.8333
cc 12
nc 384
nop 1

How to fix   Complexity   

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
/**
3
 * @package     Redcore
4
 * @subpackage  Base
5
 *
6
 * @copyright   Copyright (C) 2008 - 2021 redWEB.dk. All rights reserved.
7
 * @license     GNU General Public License version 2 or later, see LICENSE.
8
 */
9
10
defined('JPATH_REDCORE') or die;
11
12
use Joomla\Utilities\ArrayHelper;
13
14
/**
15
 * redCORE Soap Webservice Dynamic Class
16
 *
17
 * @package     Redcore
18
 * @subpackage  Soap
19
 * @since       1.4
20
 */
21
class RApiSoapOperationOperation
22
{
23
	/**
24
	 * Webservice object
25
	 *
26
	 * @var  RApiHalHal
27
	 */
28
	protected $webservice = null;
29
30
	/**
31
	 * Constructor.
32
	 *
33
	 * @param   RApiHalHal  $webservice  Webservice object
34
	 * @param   array       $config      An optional associative array of configuration settings.
35
	 */
36
	public function __construct($webservice, $config = array())
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

36
	public function __construct($webservice, /** @scrutinizer ignore-unused */ $config = array())

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
	{
38
		$this->webservice = $webservice;
39
	}
40
41
	/**
42
	 * Read list
43
	 *
44
	 * @param   object  $data  $limitStart, $limit, $filterSearch,
45
	 *                         $filters, $ordering, $orderingDirection, $language
46
	 *
47
	 * @return  array
48
	 */
49
	public function readList($data)
50
	{
51
		// We are setting the operation of the webservice to Read
52
		$this->setOperation('read');
53
		$dataGet = $this->webservice->options->get('dataGet', array());
54
55
		if (is_object($dataGet))
56
		{
57
			$dataGet = ArrayHelper::fromObject($dataGet);
58
		}
59
60
		$dataGet['list']['limitstart'] = (isset($data->limitStart) ? (int) $data->limitStart : 0);
61
		$dataGet['list']['limit']      = (isset($data->limit) ? (int) $data->limit : 20);
62
		$dataGet['filter']['search']   = (isset($data->filterSearch) ? (string) $data->filterSearch : '');
63
64
		$filters = RApiHalHelper::getFilterFields($this->webservice->configuration->operations->read->list, true);
65
66
		foreach ($filters as $filter)
67
		{
68
			$dataGet['filter'][$filter] = isset($data->filters->$filter) ? $data->filters->$filter : '';
69
		}
70
71
		$dataGet['list']['ordering']  = (isset($data->ordering) ? (string) $data->ordering : '');
72
		$dataGet['list']['direction'] = (isset($data->orderingDirection) ? (string) $data->orderingDirection : '');
73
74
		// Handle different language switch
75
		$this->setLanguage((isset($data->language) ? (string) $data->language : ''));
76
77
		$this->webservice->options->set('dataGet', $dataGet);
78
		$this->webservice->options->set('task', '');
79
		$this->webservice->options->set('filterOutResourcesGroups', array('_links', '_messages'));
80
		$this->webservice->options->set('filterResourcesSpecific', 'listItem');
81
		$this->webservice->execute();
82
83
		$arr = $this->webservice->hal->toArray();
84
85
		$outputResources = RApiSoapHelper::getOutputResources($this->webservice->configuration->operations->read->list, 'listItem', true);
86
87
		$response = array();
88
89
		if ($arr['_embedded'] && $arr['_embedded']['item'])
90
		{
91
			$response = RApiSoapHelper::selectListResources($outputResources, $arr['_embedded']['item']);
92
		}
93
94
		$final       = new stdClass;
95
		$final->list = $response;
96
97
		return $final;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $final returns the type stdClass which is incompatible with the documented return type array.
Loading history...
98
	}
99
100
	/**
101
	 * Read item
102
	 *
103
	 * @param   object  $data  Primary keys and $language
104
	 *
105
	 * @return  array
106
	 */
107
	public function readItem($data)
108
	{
109
		// We are setting the operation of the webservice to Read
110
		$this->setOperation('read');
111
		$dataGet               = $this->webservice->options->get('dataGet', array());
112
		$primaryKeysFromFields = RApiHalHelper::getFieldsArray($this->webservice->configuration->operations->read->item, true);
113
114
		// If there are no primary keys defined we will use id field as default
115
		if (empty($primaryKeysFromFields))
116
		{
117
			$primaryKeysFromFields['id'] = array('transform' => 'int');
118
		}
119
120
		foreach ($primaryKeysFromFields as $primaryKey => $primaryKeyField)
121
		{
122
			$keyData = '';
123
124
			if (isset($data->$primaryKey) && $data->$primaryKey != '')
125
			{
126
				$keyData = $data->$primaryKey;
127
			}
128
129
			$dataGet->$primaryKey = $this->webservice->transformField($primaryKeyField['transform'], $keyData, false);
130
		}
131
132
		// Handle different language switch
133
		$this->setLanguage((string) (isset($data->language) ? $data->language : ''));
134
135
		$this->webservice->options->set('dataGet', $dataGet);
136
		$this->webservice->options->set('task', '');
137
		$this->webservice->options->set('filterOutResourcesGroups', array('_links', '_messages'));
138
		$this->webservice->execute();
139
140
		$arr             = $this->webservice->hal->toArray();
141
		$outputResources = RApiSoapHelper::getOutputResources($this->webservice->configuration->operations->read->item, '', true);
142
		$response        = RApiSoapHelper::selectListResources($outputResources, array($arr));
143
144
		$final       = new stdClass;
145
		$final->item = (empty($response) ? array() : $response[0]);
146
147
		$match = true;
148
149
		if (RApiHalHelper::isAttributeTrue($this->webservice->configuration->operations->read->item, 'enforcePKs', true))
150
		{
151
			foreach ($primaryKeysFromFields as $primaryKey => $primaryKeyField)
152
			{
153
				if ($dataGet->$primaryKey != $final->item->$primaryKey)
154
				{
155
					$match = false;
156
				}
157
			}
158
		}
159
160
		if (!$match)
161
		{
162
			$final = array();
163
		}
164
165
		if (!count((array) $final->item))
166
		{
167
			$final = array();
168
		}
169
170
		return $final;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $final also could return the type stdClass which is incompatible with the documented return type array.
Loading history...
171
	}
172
173
	/**
174
	 * Create operation
175
	 *
176
	 * @param   object  $data  Data array passed for the item
177
	 *
178
	 * @return  mixed
179
	 */
180
	public function create($data)
181
	{
182
		// We are setting the operation of the webservice to create
183
		$this->webservice->options->set('task', '');
184
		$this->setOperation('create');
185
		$this->webservice->options->set('data', (array) $data);
186
		$this->webservice->options->set('filterOutResourcesGroups', array('_links', '_messages'));
187
		$this->webservice->execute();
188
189
		$arr = $this->webservice->hal->toArray();
190
191
		if (!isset($arr['result']))
192
		{
193
			$arr['result'] = false;
194
		}
195
196
		return $arr;
197
	}
198
199
	/**
200
	 * Update operation
201
	 *
202
	 * @param   array  $data  Data array passed for the item
203
	 *
204
	 * @return  array
205
	 */
206
	public function update($data = array())
207
	{
208
		// We are setting the operation of the webservice to update
209
		$this->webservice->options->set('task', '');
210
		$this->setOperation('update');
211
		$this->webservice->options->set('data', $data);
212
		$this->webservice->options->set('filterOutResourcesGroups', array('_links', '_messages'));
213
		$this->webservice->execute();
214
215
		$arr = $this->webservice->hal->toArray();
216
217
		if (!isset($arr['result']))
218
		{
219
			$arr['result'] = false;
220
		}
221
222
		return $arr;
223
	}
224
225
	/**
226
	 * Delete operation
227
	 *
228
	 * @param   array  $data  Data array passed for the item
229
	 *
230
	 * @return  array
231
	 */
232
	public function delete($data = array())
233
	{
234
		// We are setting the operation of the webservice to delete
235
		$this->webservice->options->set('task', '');
236
		$this->setOperation('delete');
237
		$this->webservice->options->set('data', $data);
238
		$this->webservice->options->set('filterOutResourcesGroups', array('_links', '_messages'));
239
		$this->webservice->execute();
240
241
		$arr = $this->webservice->hal->toArray();
242
243
		if (!isset($arr['result']))
244
		{
245
			$arr['result'] = false;
246
		}
247
248
		return $arr;
249
	}
250
251
	/**
252
	 * We use this method to counter all task related methods and point them to the same method
253
	 *
254
	 * @param   string  $method  Method name
255
	 * @param   array   $args    Arrays passed to the method
256
	 *
257
	 * @return  mixed
258
	 */
259
	public function __call($method, $args)
260
	{
261
		if (strpos($method, 'task_') === 0)
262
		{
263
			$taskName = substr($method, 5);
264
265
			return $this->task($taskName, $args);
0 ignored issues
show
Bug introduced by
$args of type array is incompatible with the type object expected by parameter $data of RApiSoapOperationOperation::task(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

265
			return $this->task($taskName, /** @scrutinizer ignore-type */ $args);
Loading history...
266
		}
267
	}
268
269
	/**
270
	 * Triggers specific task operation on the webservice
271
	 *
272
	 * @param   string  $taskName  Task name
273
	 * @param   object  $data      Data Array passed to the task method
274
	 *
275
	 * @return  array
276
	 */
277
	private function task($taskName, $data)
278
	{
279
		// Correctly load of data coming from SOAP request
280
		if (is_array($data) && !empty($data))
0 ignored issues
show
introduced by
The condition is_array($data) is always false.
Loading history...
281
		{
282
			$data = $data[0];
283
		}
284
285
		// We are setting the operation of the webservice to task
286
		$this->webservice->options->set('task', $taskName);
287
		JFactory::getApplication()->input->set('task', $taskName);
288
		$this->setOperation('task');
289
		$this->webservice->options->set('data', (array) $data);
290
		$this->webservice->options->set('filterOutResourcesGroups', array('_links', '_messages'));
291
		$this->webservice->execute();
292
293
		$arr = $this->webservice->hal->toArray();
294
295
		if (!isset($arr['result']))
296
		{
297
			$arr['result'] = false;
298
		}
299
300
		return $arr;
301
	}
302
303
	/**
304
	 * Set operation of the webservice
305
	 *
306
	 * @param   string  $operationName  Operation name
307
	 *
308
	 * @return  void
309
	 */
310
	protected function setOperation($operationName)
311
	{
312
		if ($operationName == 'task')
313
		{
314
			$task = $this->webservice->options->get('task', '');
315
316
			// If task is pointing to some other operation like apply, update or delete
317
			if (!empty($task) && !empty($this->webservice->configuration->operations->task->{$task}['useOperation']))
318
			{
319
				$operation = strtoupper((string) $this->webservice->configuration->operations->task->{$task}['useOperation']);
320
321
				if (in_array($operation, array('CREATE', 'READ', 'UPDATE', 'DELETE', 'DOCUMENTATION')))
322
				{
323
					$operationName = $operation;
324
				}
325
			}
326
		}
327
328
		$this->webservice->operation = strtolower($operationName);
329
	}
330
331
	/**
332
	 * Set language of the site
333
	 *
334
	 * @param   string  $language  Language name
335
	 *
336
	 * @return  void
337
	 */
338
	protected function setLanguage($language)
339
	{
340
		RTranslationHelper::setLanguage($language);
341
	}
342
}
343