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

Boot::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 0
dl 0
loc 29
rs 9.6333
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->login . '(' . $item->email . ')';
36
                $text = App::$Translate->get('User', 'Login: %login%, email: %email%, nick: %nick%', [
37
                    'login' => $item->login,
38
                    'email' => $item->email,
39
                    'nick' => $item->profile->nick ?? 'id' . $item->id
40
                ]);
41
42
                // initialize abstract response pattern
43
                $res = new AbstractSearchItem();
44
                $res->setTitle($title);
45
                $res->setSnippet($text);
46
                $res->setDate($item->created_at);
47
                $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...
48
                $res->setUrl('user/update', [$item->id]);
49
                $res->setMarker('User');
50
51
                $model->add($res);
52
            });
53
54
        });
55
    }
56
}