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

Boot   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A boot() 0 29 1
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 zenn
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
}