| Total Complexity | 43 |
| Total Lines | 320 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like RApiSoapOperationOperation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RApiSoapOperationOperation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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()) |
||
|
|
|||
| 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) |
||
| 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; |
||
| 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); |
||
| 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)) |
||
| 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) |
||
| 341 | } |
||
| 342 | } |
||
| 343 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.