SearchRelationHandlerTrait::searchRelation()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 51
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 33
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 51
ccs 0
cts 30
cp 0
crap 30
rs 9.0808

How to fix   Long Method   

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
namespace Yaro\Jarboe\Http\Controllers\Traits\Handlers;
4
5
use Illuminate\Http\Request;
6
use Yaro\Jarboe\Table\CRUD;
7
use Yaro\Jarboe\Table\Fields\Select;
8
use Yaro\Jarboe\Table\Fields\Tags;
9
10
trait SearchRelationHandlerTrait
11
{
12
    /**
13
     * Handle relation search action.
14
     * Currently used for Select field with type `select2` and `ajax = true`, and Tags field with `ajax = true`.
15
     *
16
     * @param Request $request
17
     * @return \Illuminate\Http\JsonResponse
18
     */
19
    public function searchRelation(Request $request)
20
    {
21
        $this->beforeInit();
22
        $this->init();
23
        $this->bound();
24
25
        // TODO:
26
        $perPage = 10;
27
28
        $query = $request->get('term');
29
        $fieldName = $request->get('field');
30
        $page = (int) $request->get('page');
31
32
        /** @var Select|Tags $field */
33
        $field = $this->crud()->getFieldByName($fieldName);
34
        if (!$field) {
35
            throw new \RuntimeException(sprintf('Field [%s] not setted to crud', $fieldName));
36
        }
37
38
        $total = 0;
39
        $results = [];
40
        if ($field->isGroupedRelation()) {
41
            foreach ($field->getRelations() as $index => $group) {
42
                $options = $field->getOptions($page, $perPage, $query, $total, $index);
43
                array_walk($options, function (&$item, $key) use ($group) {
44
                    $item = [
45
                        'id'   => crc32($group['group']) .'~~~'. $key,
46
                        'text' => $item,
47
                    ];
48
                });
49
                if ($options) {
50
                    $results[] = [
51
                        'text'     => $group['group'],
52
                        'children' => array_values($options),
53
                    ];
54
                }
55
            }
56
        } else {
57
            $results = $field->getOptions($page, $perPage, $query, $total);
58
            array_walk($results, function (&$item, $key) {
59
                $item = [
60
                    'id'   => $key,
61
                    'text' => $item,
62
                ];
63
            });
64
        }
65
66
        return response()->json([
67
            'results' => array_values($results),
68
            'pagination' => [
69
                'more' => $total > $page * $perPage,
70
            ]
71
        ]);
72
    }
73
74
    abstract protected function beforeInit();
75
    abstract protected function init();
76
    abstract protected function bound();
77
    abstract protected function crud(): CRUD;
78
    abstract protected function can($action): bool;
79
}
80