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

Comments::boot()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 1
nop 0
dl 0
loc 30
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Apps\Controller\Front;
4
5
6
use Apps\ActiveRecord\CommentPost;
7
use Apps\Model\Front\Search\AbstractSearchResult;
8
use Apps\Model\Front\Search\EntitySearchMain;
9
use Extend\Core\Arch\FrontAppController;
10
use Ffcms\Core\App;
11
use Ffcms\Core\Helper\Text;
12
use Ffcms\Templex\Url\Url;
13
use Illuminate\Support\Collection;
14
15
/**
16
 * Class Comments
17
 * @package Apps\Controller\Front
18
 */
19
class Comments extends FrontAppController
20
{
21
    /**
22
     * Boot search features
23
     * @return void
24
     */
25
    public static function boot(): void
26
    {
27
        App::$Event->on(Search::EVENT_SEARCH_RUN, function ($model) {
28
            /** @var EntitySearchMain $model */
29
            $limit = (int)$model->getConfigs()['itemPerApp'];
30
            if ($limit < 1) {
31
                $limit = 1;
32
            }
33
34
            $query = CommentPost::search($model->query)
35
                ->where('moderate', '=', 0)
36
                ->take($limit)
37
                ->get();
38
39
            /** @var CommentPost[]|Collection $query */
40
            $query->each(function($item) use ($model){
41
                /** @var CommentPost $item */
42
                $snippet = App::$Security->strip_tags($item->message);
43
                $snippet = Text::snippet($snippet);
44
45
                // make unique instance object
46
                $instance = new AbstractSearchResult();
47
                $instance->setTitle(App::$Translate->get('Search', 'Comment on the page'));
48
                $instance->setSnippet($snippet);
49
                $instance->setUri('/' . $item->app_name . '/comments/' . $item->app_relation_id);
50
                $instance->setDate($item->created_at);
51
                $instance->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...
52
53
                // add instance to result set
54
                $model->add($instance);
55
            });
56
        });
57
    }
58
}