| Conditions | 46 |
| Paths | > 20000 |
| Total Lines | 189 |
| Code Lines | 122 |
| Lines | 57 |
| Ratio | 30.16 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 462 | public function search(\Base $f3, array $params) |
||
| 463 | { |
||
| 464 | $isAdmin = $f3->get('isAdmin'); |
||
| 465 | View Code Duplication | if (!$isAdmin && array_key_exists('id', $params)) { |
|
|
1 ignored issue
–
show
|
|||
| 466 | $this->failure('authentication_error', "User does not have permission.", 401); |
||
| 467 | return $this->setOAuthError('access_denied'); |
||
| 468 | } elseif ($isAdmin && array_key_exists('id', $params)) { |
||
| 469 | $users_uuid = $params['id']; |
||
| 470 | } elseif (!$isAdmin) { |
||
| 471 | $users_uuid = $f3->get('uuid'); |
||
| 472 | } |
||
| 473 | |||
| 474 | // return raw data for object? |
||
| 475 | $adminView = $f3->get('isAdmin') && 'admin' == $f3->get('REQUEST.view'); |
||
| 476 | |||
| 477 | // set up paging limits |
||
| 478 | $minPerPage = $f3->get('api.paging_min'); |
||
| 479 | $maxPerPage = $f3->get('api.paging_max'); |
||
| 480 | $perPage = (int) $f3->get('REQUEST.per_page'); |
||
| 481 | if ($perPage < $minPerPage) { |
||
| 482 | $perPage = $minPerPage; |
||
| 483 | } |
||
| 484 | if ($perPage > $maxPerPage) { |
||
| 485 | $perPage = $maxPerPage; |
||
| 486 | } |
||
| 487 | |||
| 488 | $page = $f3->get('REQUEST.page'); |
||
| 489 | if ($page < 1) { |
||
| 490 | $page = 1; |
||
| 491 | } |
||
| 492 | |||
| 493 | // fetch data (paging is 0 based) |
||
| 494 | $m = $this->getMapper(); |
||
| 495 | $allFields = $m->fields(); |
||
| 496 | |||
| 497 | // validate order field |
||
| 498 | $order = $f3->get('REQUEST.order'); |
||
| 499 | View Code Duplication | if (!empty($order)) { |
|
|
1 ignored issue
–
show
|
|||
| 500 | $orderClauses = empty($order) ? [] : preg_split("/[,]/", $order); |
||
| 501 | foreach ($orderClauses as $k => $field) { |
||
| 502 | // split into field, asc/desc |
||
| 503 | $field = preg_split("/[\s]+/", trim($field)); |
||
| 504 | if (!in_array($field[0], $allFields)) { |
||
| 505 | // invalid field |
||
| 506 | unset($orderClauses[$k]); |
||
| 507 | continue; |
||
| 508 | } elseif (count($field) == 1) { |
||
| 509 | $field[1] = 'asc'; |
||
| 510 | } elseif (count($field) == 2) { |
||
| 511 | if (!in_array($field[1], ['asc', 'desc'])) { |
||
| 512 | $field[1] = 'asc'; |
||
| 513 | } |
||
| 514 | } |
||
| 515 | $orderClauses[$k] = $field[0] . ' ' . $field[1]; |
||
| 516 | } |
||
| 517 | $order = join(',', $orderClauses); |
||
| 518 | } |
||
| 519 | |||
| 520 | // fields to return and fields to search - validate |
||
| 521 | $validFields = []; |
||
| 522 | View Code Duplication | foreach (['fields', 'search_fields'] as $fieldsList) { |
|
|
1 ignored issue
–
show
|
|||
| 523 | $fields = $f3->get('REQUEST.' . $fieldsList); |
||
| 524 | if (empty($fields)) { |
||
| 525 | continue; |
||
| 526 | } |
||
| 527 | $fields = empty($fields) ? [] : preg_split("/[,]/", $fields); |
||
| 528 | foreach ($fields as $k => $field) { |
||
| 529 | if (!in_array($field, $allFields)) { |
||
| 530 | unset($fields[$k]); |
||
| 531 | } |
||
| 532 | } |
||
| 533 | $validFields[$fieldsList] = join(',', $fields); |
||
| 534 | } |
||
| 535 | |||
| 536 | // validated fields to return |
||
| 537 | $fields = empty($validFields['fields']) ? join(',', $allFields) : $validFields['fields']; |
||
| 538 | |||
| 539 | // validated fields to search in, use all if empty |
||
| 540 | $searchFields = empty($searchFields['search_fields']) ? join(',', $allFields) : $validFields['search_fields']; |
||
| 541 | |||
| 542 | // get search type |
||
| 543 | $search = $f3->get('REQUEST.search'); |
||
| 544 | if (!empty($search)) { |
||
| 545 | $search = trim(strtolower($search)); |
||
| 546 | } |
||
| 547 | $search_type = $f3->get('REQUEST.search_type'); |
||
| 548 | if (empty($search_type)) { |
||
| 549 | $search_type = 'exact'; |
||
| 550 | } elseif ($search_type !== 'exact') { |
||
| 551 | $search_type = 'fuzzy'; |
||
| 552 | } |
||
| 553 | |||
| 554 | // construct search query |
||
| 555 | $db = \Registry::get('db'); |
||
| 556 | $sqlClauses = []; |
||
| 557 | $searchFieldsArray = preg_split("/[,]/", $searchFields); |
||
| 558 | View Code Duplication | foreach ($searchFieldsArray as $field) { |
|
|
1 ignored issue
–
show
|
|||
| 559 | $sqlClauses[] = 'LOWER(' . $db->quotekey($field) . ') = ' . $db->quote($search); |
||
| 560 | if ($search_type == 'fuzzy') { |
||
| 561 | $sqlClauses[] = 'LOWER(' . $db->quotekey($field) . ') LIKE ' . $db->quote('%' . $search . '%'); |
||
| 562 | } |
||
| 563 | } |
||
| 564 | |||
| 565 | // get total results |
||
| 566 | $query = 'SELECT COUNT(*) AS results FROM ' . $db->quotekey($m->table()) . ' WHERE '; |
||
| 567 | View Code Duplication | if (empty($users_uuid)) { |
|
|
1 ignored issue
–
show
|
|||
| 568 | $query .= join(' OR ', $sqlClauses); |
||
| 569 | } else { |
||
| 570 | $query .= ' users_uuid = ' . $db->quote($users_uuid) . ' AND ('. join(' OR ', $sqlClauses) . ')'; |
||
| 571 | } |
||
| 572 | $rows = $db->exec($query); |
||
| 573 | $rows = (int) $rows[0]['results']; |
||
| 574 | if ($rows < 1) { |
||
| 575 | $this->failure('sever_error', "No data available for request.", 404); |
||
| 576 | $this->setOAuthError('server_error'); |
||
| 577 | return; |
||
| 578 | } |
||
| 579 | |||
| 580 | // if fewer results than per page, set per_page |
||
| 581 | if ($page == 1 && $perPage > $rows) { |
||
| 582 | $perPage = $rows; |
||
| 583 | } |
||
| 584 | |||
| 585 | $pagination['count'] = ceil($rows / $perPage); |
||
| 586 | |||
| 587 | // too high page number? |
||
| 588 | if ($page > $pagination['count']) { |
||
| 589 | $page = $pagination['count']; |
||
| 590 | } |
||
| 591 | |||
| 592 | // set up page URLs |
||
| 593 | $url = $f3->get('PATH'); |
||
| 594 | $urlParams = [ |
||
| 595 | 'per_page' => $perPage, |
||
| 596 | 'search' => $search, |
||
| 597 | 'search_type' => $search_type, |
||
| 598 | ]; |
||
| 599 | if (!empty($order)) { |
||
| 600 | $urlParams['order'] = $order; |
||
| 601 | } |
||
| 602 | if (!empty($adminView)) { |
||
| 603 | $urlParams['view'] = 'admin'; |
||
| 604 | } |
||
| 605 | if (!empty($fields)) { |
||
| 606 | $urlParams['fields'] = $fields; |
||
| 607 | } |
||
| 608 | ksort($urlParams); |
||
| 609 | |||
| 610 | // previous page url |
||
| 611 | $prevPage = (1 > $page - 1 ) ? null : $page - 1; |
||
| 612 | $nextPage = (1 + $page> $pagination['count']) ? null : $page + 1; |
||
| 613 | |||
| 614 | $resultsFrom = 1 + ($page * $perPage) - $perPage; |
||
| 615 | $resultsTo = $resultsFrom + $perPage - 1; |
||
| 616 | if ($resultsTo > $rows) { |
||
| 617 | $resultsTo = $rows; |
||
| 618 | } |
||
| 619 | |||
| 620 | // return data |
||
| 621 | $this->data['pagination'] = [ |
||
| 622 | 'url_base' => $this->url($url, $urlParams), |
||
| 623 | 'url_current' => $this->url($url, $urlParams + ['page' => $page]), |
||
| 624 | 'url_first' => $this->url($url, $urlParams + ['page' => 1]), |
||
| 625 | 'url_last' => $this->url($url, $urlParams + ['page' => $pagination['count']]), |
||
| 626 | 'url_next' => (null == $nextPage) ? null : $this->url($url, $urlParams + ['page' => $nextPage]), |
||
| 627 | 'url_previous' => (null == $prevPage) ? null : $this->url($url, $urlParams + ['page' => $prevPage]), |
||
| 628 | 'results' => $rows, |
||
| 629 | 'results_from' => $resultsFrom, |
||
| 630 | 'results_to' => $resultsTo, |
||
| 631 | 'per_page' => $perPage, |
||
| 632 | 'pages' => $pagination['count'], |
||
| 633 | 'page' => $page, |
||
| 634 | 'object' => $m->table(), |
||
| 635 | 'fields' => preg_split("/[,]/", $fields) |
||
| 636 | ]; |
||
| 637 | |||
| 638 | // retrieve results |
||
| 639 | $query = 'SELECT * FROM ' . $db->quotekey($m->table()) . ' WHERE '; |
||
| 640 | View Code Duplication | if (empty($users_uuid)) { |
|
|
1 ignored issue
–
show
|
|||
| 641 | $query .= join(' OR ', $sqlClauses); |
||
| 642 | } else { |
||
| 643 | $query .= ' users_uuid = ' . $db->quote($users_uuid) . ' AND ('. join(' OR ', $sqlClauses) . ')'; |
||
| 644 | } |
||
| 645 | $query .= sprintf(' LIMIT %d,%d', (1 == $page) ? 0 : ($page - 1) * $perPage, $perPage); |
||
| 646 | $results = $db->exec($query); |
||
| 647 | foreach ($results as $row) { |
||
| 648 | $this->data['objects'][] = $adminView ? $m->castFields($fields, $row) : $m->exportArray($fields, $row); |
||
| 649 | } |
||
| 650 | } |
||
| 651 | |||
| 653 |