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

Boot::boot()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 21
nc 1
nop 0
dl 0
loc 35
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
namespace Apps\Controller\Front\Content;
4
5
6
use Apps\ActiveRecord\Content;
7
use Apps\Controller\Front\Search;
8
use Apps\Model\Front\Search\AbstractSearchResult;
9
use Apps\Model\Front\Search\EntitySearchMain;
10
use Ffcms\Core\App;
11
use Apps\ActiveRecord\App as AppRecord;
12
use Ffcms\Core\Helper\Text;
13
use Ffcms\Core\Helper\Type\Str;
14
use Ffcms\Templex\Helper\Html\Dom;
15
use Illuminate\Support\Collection;
16
17
/**
18
 * Trait Boot
19
 * @package Apps\Controller\Front\Content
20
 */
21
trait Boot
22
{
23
    /**
24
     * Implement boot features
25
     */
26
    public static function boot(): void
27
    {
28
        App::$Event->on(Search::EVENT_SEARCH_RUN, function ($model) {
29
            /** @var EntitySearchMain $model */
30
            $limit = (int)$model->getConfigs()['itemPerApp'];
31
            if ($limit < 1) {
32
                $limit = 1;
33
            }
34
35
            // relevant search by string query
36
            $records = Content::search($model->query)
37
                ->where('display', true)
38
                ->take($limit)
39
                ->get();
40
41
            /** @var Content[]|Collection $records */
42
            $records->each(function($item) use ($model) {
43
                /** @var Content $item */
44
                $title = $item->getLocaled('title');
45
                $text = App::$Security->strip_tags($item->getLocaled('text'));
46
                $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

46
                $snippet = Text::snippet(/** @scrutinizer ignore-type */ $text);
Loading history...
47
                // prevent empty items
48
                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

48
                if (Str::likeEmpty(/** @scrutinizer ignore-type */ $title)) {
Loading history...
49
                    return;
50
                }
51
52
                // initialize abstract response pattern
53
                $res = new AbstractSearchResult();
54
                $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

54
                $res->setTitle(/** @scrutinizer ignore-type */ $title);
Loading history...
55
                $res->setSnippet($snippet);
56
                $res->setDate($item->created_at);
57
                $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...
58
                $res->setUri('/content/read/' . $item->getPath());
59
60
                $model->add($res);
61
            });
62
        });
63
    }
64
}