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

Boot::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

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