Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

Apps/Controller/Admin/Content/Boot.php (2 issues)

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);
44
                // prevent empty items
45
                if (Str::likeEmpty($title)) {
46
                    return;
47
                }
48
49
                // initialize abstract response pattern
50
                $res = new AbstractSearchItem();
51
                $res->setTitle($title);
52
                $res->setSnippet($snippet);
53
                $res->setDate($item->created_at);
54
                $res->setRelevance((int)$item->relevance);
0 ignored issues
show
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);
71
                // prevent empty items
72
                if (Str::likeEmpty($title)) {
73
                    return;
74
                }
75
76
                // initialize abstract response pattern
77
                $res = new AbstractSearchItem();
78
                $res->setTitle($title);
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
}