Passed
Push — master ( 3a195a...0456bd )
by Mihail
06:35
created

Boot::boot()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 60
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 37
nc 1
nop 0
dl 0
loc 60
rs 9.328
c 0
b 0
f 0

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 Apps\Controller\Admin\Content;
4
5
6
use Apps\ActiveRecord\Content;
7
use Apps\ActiveRecord\ContentCategory;
8
use Apps\Controller\Admin\Main;
9
use Apps\Model\Admin\Main\AbstractSearchItem;
10
use Apps\Model\Admin\Main\CollectionSearchResults;
11
use Ffcms\Core\App;
12
use Ffcms\Core\Helper\Text;
13
use Ffcms\Core\Helper\Type\Str;
14
use Illuminate\Support\Collection;
15
16
/**
17
 * Trait Boot
18
 * @package Apps\Controller\Admin\Content
19
 */
20
trait Boot
21
{
22
    /**
23
     * Implement search boot features
24
     * @return void
25
     */
26
    public static function boot(): void
27
    {
28
        App::$Event->on(Main::SEARCH_EVENT_NAME, function ($model) {
29
            /** @var CollectionSearchResults $model */
30
            $limit = $model->getLimit();
31
            $query = $model->getQuery();
32
33
            // relevant search by string query
34
            $records = Content::search($query)
35
                ->take($limit)
36
                ->get();
37
38
            /** @var Content[]|Collection $records */
39
            $records->each(function($item) use ($model) {
40
                /** @var Content $item */
41
                $title = $item->getLocaled('title');
42
                $text = App::$Security->strip_tags($item->getLocaled('text'));
43
                $snippet = Text::snippet($text);
0 ignored issues
show
Bug introduced by zenn
It seems like $text can also be of type array and array and null; however, parameter $text of Ffcms\Core\Helper\Text::snippet() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
                $snippet = Text::snippet(/** @scrutinizer ignore-type */ $text);
Loading history...
44
                // prevent empty items
45
                if (Str::likeEmpty($title)) {
0 ignored issues
show
Bug introduced by zenn
It seems like $title can also be of type array; however, parameter $string of Ffcms\Core\Helper\Type\Str::likeEmpty() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
                if (Str::likeEmpty(/** @scrutinizer ignore-type */ $title)) {
Loading history...
46
                    return;
47
                }
48
49
                // initialize abstract response pattern
50
                $res = new AbstractSearchItem();
51
                $res->setTitle($title);
0 ignored issues
show
Bug introduced by zenn
It seems like $title can also be of type array; however, parameter $value of Apps\Model\Front\Search\...earchResult::setTitle() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
                $res->setTitle(/** @scrutinizer ignore-type */ $title);
Loading history...
52
                $res->setSnippet($snippet);
53
                $res->setDate($item->created_at);
54
                $res->setRelevance((int)$item->relevance);
0 ignored issues
show
Bug introduced by zenn
The property relevance does not seem to exist on Apps\ActiveRecord\Content. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
55
                $res->setUrl('content/update', [$item->id]);
56
                $res->setMarker('Content');
57
58
                $model->add($res);
59
            });
60
            // search in categories
61
            $records = ContentCategory::search($query)
62
                ->take($limit)
63
                ->get();
64
65
            /** @var ContentCategory[]|Collection $records */
66
            $records->each(function($item) use ($model) {
67
                /** @var ContentCategory $item */
68
                $title = $item->getLocaled('title');
69
                $text = App::$Security->strip_tags($item->getLocaled('description'));
70
                $snippet = Text::snippet($text);
0 ignored issues
show
Bug introduced by zenn
It seems like $text can also be of type array and array and null; however, parameter $text of Ffcms\Core\Helper\Text::snippet() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
                $snippet = Text::snippet(/** @scrutinizer ignore-type */ $text);
Loading history...
71
                // prevent empty items
72
                if (Str::likeEmpty($title)) {
0 ignored issues
show
Bug introduced by zenn
It seems like $title can also be of type array; however, parameter $string of Ffcms\Core\Helper\Type\Str::likeEmpty() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
                if (Str::likeEmpty(/** @scrutinizer ignore-type */ $title)) {
Loading history...
73
                    return;
74
                }
75
76
                // initialize abstract response pattern
77
                $res = new AbstractSearchItem();
78
                $res->setTitle($title);
0 ignored issues
show
Bug introduced by zenn
It seems like $title can also be of type array; however, parameter $value of Apps\Model\Front\Search\...earchResult::setTitle() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
                $res->setTitle(/** @scrutinizer ignore-type */ $title);
Loading history...
79
                $res->setSnippet($snippet);
80
                $res->setDate($item->created_at);
81
                $res->setRelevance((int)$item->relevance);
0 ignored issues
show
Bug Best Practice introduced by zenn
The property relevance does not exist on Apps\ActiveRecord\ContentCategory. Since you implemented __get, consider adding a @property annotation.
Loading history...
82
                $res->setUrl('content/categoryupdate', [$item->id]);
83
                $res->setMarker('Content');
84
85
                $model->add($res);
86
            });
87
        });
88
    }
89
}