Passed
Push — master ( ad6a40...1fa946 )
by Alexandr
02:05
created

ComponentSearchTrait::search()   C

Complexity

Conditions 16
Paths 2

Size

Total Lines 63
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 41
nc 2
nop 1
dl 0
loc 63
rs 6.0607
c 0
b 0
f 0

How to fix   Long Method    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
namespace Larrock\Core\ComponentTraits;
4
5
use Larrock\Core\Helpers\FormBuilder\FormTags;
6
7
trait ComponentSearchTrait
8
{
9
    /** @var null|bool Осуществляется ли поиск по компоненту */
10
    public $searchable;
11
12
    /** @var string Какое поле модели показывать для поиска */
13
    public $search_title;
14
15
    /**
16
     * Разрешить поиск по материалам компонента.
17
     *
18
     * @param string $search_title Какое поле модели показывать для поиска
19
     * @return $this
20
     */
21
    public function isSearchable($title = 'title')
22
    {
23
        $this->searchable = true;
24
        $this->search_title = $title;
25
26
        return $this;
27
    }
28
29
    /**
30
     * Данные для поиска по материалам компонента.
31
     * @param null|bool $admin Если TRUE - для поиска будут доступны вообще все элементы (не только опубликованные)
32
     * @return null
33
     */
34
    public function search($admin = null)
35
    {
36
        if ($this->searchable !== true) {
37
            return [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array() returns the type array which is incompatible with the documented return type null.
Loading history...
38
        }
39
40
        return \Cache::rememberForever('search'.$this->name.$admin, function () use ($admin) {
41
            $data = [];
42
43
            $model = new $this->model;
44
45
            $search_rows = ['id', $this->search_title];
46
47
            if (isset($this->rows['category']) && !$this->rows['category'] instanceof FormTags) {
48
                $search_rows[] = 'category';
49
            }
50
51
            if ($admin) {
52
                if (isset($this->rows['category'])) {
53
                    $model = $model::with(['getCategory']);
54
                }
55
            } else {
56
                if (isset($this->rows['category'])) {
57
                    $model = $model::with(['getCategoryActive']);
58
                }
59
            }
60
61
            $items = $model->get($search_rows);
62
63
            foreach ($items as $item) {
64
                $data[$item->id]['id'] = $item->id;
65
                $data[$item->id]['title'] = $item->{$this->search_title};
66
                $data[$item->id]['full_url'] = $item->full_url;
67
                $data[$item->id]['component'] = $this->name;
68
                $data[$item->id]['category'] = null;
69
                $data[$item->id]['admin_url'] = $item->admin_url;
70
                if ($admin) {
71
                    if ($item->getCategory) {
72
                        if (\count($item->getCategory) > 0) {
73
                            $data[$item->id]['category'] = $item->getCategory->first()->title;
74
                        } elseif (isset($item->getCategory->title)) {
75
                            $data[$item->id]['category'] = $item->getCategory->title;
76
                        } else {
77
                            unset($data[$item->id]);
78
                        }
79
                    }
80
                } else {
81
                    if ($item->getCategoryActive) {
82
                        if (\count($item->getCategoryActive) > 0) {
83
                            $data[$item->id]['category'] = $item->getCategoryActive->first()->title;
84
                        } elseif (isset($item->getCategoryActive->title)) {
85
                            $data[$item->id]['category'] = $item->getCategoryActive->title;
86
                        } else {
87
                            unset($data[$item->id]);
88
                        }
89
                    }
90
                }
91
            }
92
            if (\count($data) === 0) {
93
                return null;
94
            }
95
96
            return $data;
97
        });
98
    }
99
}