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

Boot::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 29
nc 1
nop 0
dl 0
loc 47
rs 9.456
c 0
b 0
f 0
1
<?php
2
3
namespace Apps\Controller\Admin\Comments;
4
5
6
use Apps\ActiveRecord\CommentAnswer;
7
use Apps\ActiveRecord\CommentPost;
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 Illuminate\Support\Collection;
14
15
/**
16
 * Trait Boot
17
 * @package Apps\Controller\Admin\Comments
18
 */
19
trait Boot
20
{
21
    /**
22
     * Boot search hook
23
     * @return void
24
     */
25
    public static function boot(): void
26
    {
27
        App::$Event->on(Main::SEARCH_EVENT_NAME, function ($model) {
28
            /** @var CollectionSearchResults $model */
29
            $records = CommentPost::search($model->getQuery())
30
                ->take($model->getLimit())
31
                ->get();
32
33
            /** @var CommentPost[]|Collection $records */
34
            $records->each(function($item) use ($model) {
35
                /** @var CommentPost $item */
36
                $title = App::$Translate->get('Comments', 'Comment #%id%', ['id' => $item->id]);
37
                $text = Text::snippet(App::$Security->strip_tags($item->message));
38
39
                // initialize abstract response pattern
40
                $res = new AbstractSearchItem();
41
                $res->setTitle($title);
42
                $res->setSnippet($text);
43
                $res->setDate($item->created_at);
44
                $res->setRelevance((int)$item->relevance);
0 ignored issues
show
Bug introduced by zenn
The property relevance does not seem to exist on Apps\ActiveRecord\CommentPost. 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...
45
                $res->setUrl('comments/read', [$item->id]);
46
                $res->setMarker('Comment');
47
48
                $model->add($res);
49
            });
50
51
            // search comment answers
52
            $records = CommentAnswer::search($model->getQuery())
53
                ->take($model->getLimit())
54
                ->get();
55
56
            /** @var CommentAnswer[]|Collection $records */
57
            $records->each(function($item) use ($model) {
58
                /** @var CommentAnswer $item */
59
                $title = App::$Translate->get('Comments', 'Comment answer #%id%', ['id' => $item->id]);
60
                $text = Text::snippet(App::$Security->strip_tags($item->message));
61
62
                // initialize abstract response pattern
63
                $res = new AbstractSearchItem();
64
                $res->setTitle($title);
65
                $res->setSnippet($text);
66
                $res->setDate($item->created_at);
67
                $res->setRelevance((int)$item->relevance);
0 ignored issues
show
Bug introduced by zenn
The property relevance does not seem to exist on Apps\ActiveRecord\CommentAnswer. 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...
68
                $res->setUrl('comments/read', [$item->comment_id]);
69
                $res->setMarker('Comment');
70
71
                $model->add($res);
72
            });
73
74
        });
75
    }
76
}