| Total Complexity | 93 |
| Total Lines | 526 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Boms 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 Boms, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class Boms extends DolibarrApi |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var BOM $bom {@type BOM} |
||
| 42 | */ |
||
| 43 | public $bom; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Constructor |
||
| 47 | */ |
||
| 48 | public function __construct() |
||
| 49 | { |
||
| 50 | global $db, $conf; |
||
| 51 | $this->db = $db; |
||
| 52 | $this->bom = new BOM($this->db); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get properties of a bom object |
||
| 57 | * |
||
| 58 | * Return an array with bom informations |
||
| 59 | * |
||
| 60 | * @param int $id ID of bom |
||
| 61 | * @return array|mixed data without useless information |
||
| 62 | * |
||
| 63 | * @url GET {id} |
||
| 64 | * @throws RestException |
||
| 65 | */ |
||
| 66 | public function get($id) |
||
| 82 | } |
||
| 83 | |||
| 84 | |||
| 85 | /** |
||
| 86 | * List boms |
||
| 87 | * |
||
| 88 | * Get a list of boms |
||
| 89 | * |
||
| 90 | * @param string $sortfield Sort field |
||
| 91 | * @param string $sortorder Sort order |
||
| 92 | * @param int $limit Limit for list |
||
| 93 | * @param int $page Page number |
||
| 94 | * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" |
||
| 95 | * @return array Array of order objects |
||
| 96 | * |
||
| 97 | * @throws RestException |
||
| 98 | */ |
||
| 99 | public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '') |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Create bom object |
||
| 193 | * |
||
| 194 | * @param array $request_data Request datas |
||
| 195 | * @return int ID of bom |
||
| 196 | */ |
||
| 197 | public function post($request_data = null) |
||
| 198 | { |
||
| 199 | if (!DolibarrApiAccess::$user->rights->bom->write) { |
||
| 200 | throw new RestException(401); |
||
| 201 | } |
||
| 202 | // Check mandatory fields |
||
| 203 | $result = $this->_validate($request_data); |
||
| 204 | |||
| 205 | foreach ($request_data as $field => $value) { |
||
| 206 | $this->bom->$field = $value; |
||
| 207 | } |
||
| 208 | |||
| 209 | $this->checkRefNumbering(); |
||
| 210 | |||
| 211 | if (!$this->bom->create(DolibarrApiAccess::$user)) { |
||
| 212 | throw new RestException(500, "Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors)); |
||
| 213 | } |
||
| 214 | return $this->bom->id; |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Update bom |
||
| 219 | * |
||
| 220 | * @param int $id Id of bom to update |
||
| 221 | * @param array $request_data Datas |
||
| 222 | * |
||
| 223 | * @return int |
||
| 224 | */ |
||
| 225 | public function put($id, $request_data = null) |
||
| 226 | { |
||
| 227 | if (!DolibarrApiAccess::$user->rights->bom->write) { |
||
| 228 | throw new RestException(401); |
||
| 229 | } |
||
| 230 | |||
| 231 | $result = $this->bom->fetch($id); |
||
| 232 | if (!$result) { |
||
| 233 | throw new RestException(404, 'BOM not found'); |
||
| 234 | } |
||
| 235 | |||
| 236 | if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) { |
||
| 237 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 238 | } |
||
| 239 | |||
| 240 | foreach ($request_data as $field => $value) { |
||
| 241 | if ($field == 'id') { |
||
| 242 | continue; |
||
| 243 | } |
||
| 244 | $this->bom->$field = $value; |
||
| 245 | } |
||
| 246 | |||
| 247 | $this->checkRefNumbering(); |
||
| 248 | |||
| 249 | if ($this->bom->update(DolibarrApiAccess::$user) > 0) { |
||
| 250 | return $this->get($id); |
||
| 251 | } else { |
||
| 252 | throw new RestException(500, $this->bom->error); |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Delete bom |
||
| 258 | * |
||
| 259 | * @param int $id BOM ID |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | public function delete($id) |
||
| 263 | { |
||
| 264 | if (!DolibarrApiAccess::$user->rights->bom->delete) { |
||
| 265 | throw new RestException(401); |
||
| 266 | } |
||
| 267 | $result = $this->bom->fetch($id); |
||
| 268 | if (!$result) { |
||
| 269 | throw new RestException(404, 'BOM not found'); |
||
| 270 | } |
||
| 271 | |||
| 272 | if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) { |
||
| 273 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 274 | } |
||
| 275 | |||
| 276 | if (!$this->bom->delete(DolibarrApiAccess::$user)) { |
||
| 277 | throw new RestException(500, 'Error when deleting BOM : '.$this->bom->error); |
||
| 278 | } |
||
| 279 | |||
| 280 | return array( |
||
| 281 | 'success' => array( |
||
| 282 | 'code' => 200, |
||
| 283 | 'message' => 'BOM deleted' |
||
| 284 | ) |
||
| 285 | ); |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get lines of an BOM |
||
| 290 | * |
||
| 291 | * @param int $id Id of BOM |
||
| 292 | * |
||
| 293 | * @url GET {id}/lines |
||
| 294 | * |
||
| 295 | * @return array |
||
| 296 | */ |
||
| 297 | public function getLines($id) |
||
| 298 | { |
||
| 299 | if (!DolibarrApiAccess::$user->rights->bom->read) { |
||
| 300 | throw new RestException(401); |
||
| 301 | } |
||
| 302 | |||
| 303 | $result = $this->bom->fetch($id); |
||
| 304 | if (!$result) { |
||
| 305 | throw new RestException(404, 'BOM not found'); |
||
| 306 | } |
||
| 307 | |||
| 308 | if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) { |
||
| 309 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 310 | } |
||
| 311 | $this->bom->getLinesArray(); |
||
| 312 | $result = array(); |
||
| 313 | foreach ($this->bom->lines as $line) { |
||
| 314 | array_push($result, $this->_cleanObjectDatas($line)); |
||
| 315 | } |
||
| 316 | return $result; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Add a line to given BOM |
||
| 321 | * |
||
| 322 | * @param int $id Id of BOM to update |
||
| 323 | * @param array $request_data BOMLine data |
||
| 324 | * |
||
| 325 | * @url POST {id}/lines |
||
| 326 | * |
||
| 327 | * @return int |
||
| 328 | */ |
||
| 329 | public function postLine($id, $request_data = null) |
||
| 330 | { |
||
| 331 | if (!DolibarrApiAccess::$user->rights->bom->write) { |
||
| 332 | throw new RestException(401); |
||
| 333 | } |
||
| 334 | |||
| 335 | $result = $this->bom->fetch($id); |
||
| 336 | if (!$result) { |
||
| 337 | throw new RestException(404, 'BOM not found'); |
||
| 338 | } |
||
| 339 | |||
| 340 | if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) { |
||
| 341 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 342 | } |
||
| 343 | |||
| 344 | $request_data = (object) $request_data; |
||
| 345 | |||
| 346 | $updateRes = $this->bom->addLine( |
||
| 347 | $request_data->fk_product, |
||
| 348 | $request_data->qty, |
||
| 349 | $request_data->qty_frozen, |
||
| 350 | $request_data->disable_stock_change, |
||
| 351 | $request_data->efficiency, |
||
| 352 | $request_data->position, |
||
| 353 | $request_data->fk_bom_child, |
||
| 354 | $request_data->import_key |
||
| 355 | ); |
||
| 356 | |||
| 357 | if ($updateRes > 0) { |
||
| 358 | return $updateRes; |
||
| 359 | } else { |
||
| 360 | throw new RestException(400, $this->bom->error); |
||
| 361 | } |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Update a line to given BOM |
||
| 366 | * |
||
| 367 | * @param int $id Id of BOM to update |
||
| 368 | * @param int $lineid Id of line to update |
||
| 369 | * @param array $request_data BOMLine data |
||
| 370 | * |
||
| 371 | * @url PUT {id}/lines/{lineid} |
||
| 372 | * |
||
| 373 | * @return array|bool |
||
| 374 | */ |
||
| 375 | public function putLine($id, $lineid, $request_data = null) |
||
| 376 | { |
||
| 377 | if (!DolibarrApiAccess::$user->rights->bom->write) { |
||
| 378 | throw new RestException(401); |
||
| 379 | } |
||
| 380 | |||
| 381 | $result = $this->bom->fetch($id); |
||
| 382 | if (!$result) { |
||
| 383 | throw new RestException(404, 'BOM not found'); |
||
| 384 | } |
||
| 385 | |||
| 386 | if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) { |
||
| 387 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 388 | } |
||
| 389 | |||
| 390 | $request_data = (object) $request_data; |
||
| 391 | |||
| 392 | $updateRes = $this->bom->updateLine( |
||
| 393 | $lineid, |
||
| 394 | $request_data->qty, |
||
| 395 | $request_data->qty_frozen, |
||
| 396 | $request_data->disable_stock_change, |
||
| 397 | $request_data->efficiency, |
||
| 398 | $request_data->position, |
||
| 399 | $request_data->import_key |
||
| 400 | ); |
||
| 401 | |||
| 402 | if ($updateRes > 0) { |
||
| 403 | $result = $this->get($id); |
||
| 404 | unset($result->line); |
||
| 405 | return $this->_cleanObjectDatas($result); |
||
| 406 | } |
||
| 407 | return false; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Delete a line to given BOM |
||
| 412 | * |
||
| 413 | * |
||
| 414 | * @param int $id Id of BOM to update |
||
| 415 | * @param int $lineid Id of line to delete |
||
| 416 | * |
||
| 417 | * @url DELETE {id}/lines/{lineid} |
||
| 418 | * |
||
| 419 | * @return int |
||
| 420 | * |
||
| 421 | * @throws RestException 401 |
||
| 422 | * @throws RestException 404 |
||
| 423 | * @throws RestException 500 |
||
| 424 | */ |
||
| 425 | public function deleteLine($id, $lineid) |
||
| 426 | { |
||
| 427 | if (!DolibarrApiAccess::$user->rights->bom->write) { |
||
| 428 | throw new RestException(401); |
||
| 429 | } |
||
| 430 | |||
| 431 | $result = $this->bom->fetch($id); |
||
| 432 | if (!$result) { |
||
| 433 | throw new RestException(404, 'BOM not found'); |
||
| 434 | } |
||
| 435 | |||
| 436 | if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) { |
||
| 437 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 438 | } |
||
| 439 | |||
| 440 | //Check the rowid is a line of current bom object |
||
| 441 | $lineIdIsFromObject = false; |
||
| 442 | foreach ($this->bom->lines as $bl) { |
||
| 443 | if ($bl->id == $lineid) { |
||
| 444 | $lineIdIsFromObject = true; |
||
| 445 | break; |
||
| 446 | } |
||
| 447 | } |
||
| 448 | if (!$lineIdIsFromObject) { |
||
| 449 | throw new RestException(500, 'Line to delete (rowid: '.$lineid.') is not a line of BOM (id: '.$this->bom->id.')'); |
||
| 450 | } |
||
| 451 | |||
| 452 | $updateRes = $this->bom->deleteline(DolibarrApiAccess::$user, $lineid); |
||
| 453 | if ($updateRes > 0) { |
||
| 454 | return $this->get($id); |
||
| 455 | } else { |
||
| 456 | throw new RestException(405, $this->bom->error); |
||
| 457 | } |
||
| 458 | } |
||
| 459 | |||
| 460 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore |
||
| 461 | /** |
||
| 462 | * Clean sensible object datas |
||
| 463 | * |
||
| 464 | * @param Object $object Object to clean |
||
| 465 | * @return Object Object with cleaned properties |
||
| 466 | */ |
||
| 467 | protected function _cleanObjectDatas($object) |
||
| 520 | } |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Validate fields before create or update object |
||
| 524 | * |
||
| 525 | * @param array $data Array of data to validate |
||
| 526 | * @return array |
||
| 527 | * |
||
| 528 | * @throws RestException |
||
| 529 | */ |
||
| 530 | private function _validate($data) |
||
| 531 | { |
||
| 532 | $myobject = array(); |
||
| 533 | foreach ($this->bom->fields as $field => $propfield) { |
||
| 534 | if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) { |
||
| 535 | continue; // Not a mandatory field |
||
| 536 | } |
||
| 537 | if (!isset($data[$field])) { |
||
| 538 | throw new RestException(400, "$field field missing"); |
||
| 539 | } |
||
| 540 | $myobject[$field] = $data[$field]; |
||
| 541 | } |
||
| 542 | return $myobject; |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Validate the ref field and get the next Number if it's necessary. |
||
| 547 | * |
||
| 548 | * @return void |
||
| 549 | */ |
||
| 550 | private function checkRefNumbering(): void |
||
| 564 | } |
||
| 565 | } |
||
| 566 | } |
||
| 567 | } |
||
| 568 |