Boot::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 28
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Apps\Controller\Admin\User;
4
5
6
use Apps\ActiveRecord\User;
7
use Apps\Controller\Admin\Main;
8
use Apps\Model\Admin\Main\AbstractSearchItem;
9
use Apps\Model\Admin\Main\CollectionSearchResults;
10
use Ffcms\Core\App;
11
use Illuminate\Support\Collection;
12
13
/**
14
 * Trait Boot
15
 * @package Apps\Controller\Admin\User
16
 */
17
trait Boot
18
{
19
    /**
20
     * Boot features for search
21
     * @return void
22
     */
23
    public static function boot(): void
24
    {
25
        App::$Event->on(Main::SEARCH_EVENT_NAME, function ($model) {
26
            /** @var CollectionSearchResults $model */
27
            $records = User::with('profile')
28
                ->search($model->getQuery())
29
                ->take($model->getLimit())
30
                ->get();
31
32
            /** @var User[]|Collection $records */
33
            $records->each(function($item) use ($model) {
34
                /** @var User $item */
35
                $title = $item->email . '(id=' . $item->id . ')';
36
                $text = App::$Translate->get('User', 'Email: %email%, nick: %nick%', [
37
                    'email' => $item->email,
38
                    'nick' => $item->profile->nick ?? 'id' . $item->id
39
                ]);
40
41
                // initialize abstract response pattern
42
                $res = new AbstractSearchItem();
43
                $res->setTitle($title);
44
                $res->setSnippet($text);
45
                $res->setDate($item->created_at);
46
                $res->setRelevance((int)$item->relevance);
0 ignored issues
show
Bug introduced by
The property relevance does not seem to exist on Apps\ActiveRecord\User. 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...
47
                $res->setUrl('user/update', [$item->id]);
48
                $res->setMarker('User');
49
50
                $model->add($res);
51
            });
52
53
        });
54
    }
55
}