Completed
Branch master (8e0976)
by Adam
04:13
created

UsersGrid::buildGrid()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 55
rs 8.9818
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Coyote\Http\Grids\Adm;
4
5
use Boduch\Grid\Filters\FilterOperator;
6
use Boduch\Grid\Filters\Select;
7
use Boduch\Grid\Filters\Text;
8
use Coyote\Services\Grid\Components\FirewallButton;
9
use Coyote\Services\Grid\Grid;
10
use Boduch\Grid\Decorators\Boolean;
11
use Boduch\Grid\Decorators\Ip;
12
use Boduch\Grid\Order;
13
use Boduch\Grid\Components\EditButton;
14
use Coyote\User;
15
16
class UsersGrid extends Grid
17
{
18
    const YES = 1;
19
    const NO = 0;
20
21
    public function buildGrid()
22
    {
23
        $booleanOptions = [self::YES => 'Tak', self::NO => 'Nie'];
24
25
        $this
26
            ->setDefaultOrder(new Order('id', 'desc'))
27
            ->addColumn('id', [
28
                'title' => 'ID',
29
                'sortable' => true
30
            ])
31
            ->addColumn('name', [
32
                'title' => 'Nazwa użytkownika',
33
                'sortable' => true,
34
                'clickable' => function (User $user) {
35
                    return link_to_route('adm.users.save', $user->name, [$user->id]);
36
                },
37
                'filter' => new Text(['operator' => FilterOperator::OPERATOR_ILIKE])
38
            ])
39
            ->addColumn('email', [
40
                'title' => 'E-mail',
41
                'filter' => new Text(['operator' => FilterOperator::OPERATOR_ILIKE])
42
            ])
43
            ->addColumn('created_at', [
44
                'title' => 'Data rejestracji'
45
            ])
46
            ->addColumn('visited_at', [
47
                'title' => 'Data ost. wizyty',
48
                'sortable' => true
49
            ])
50
            ->addColumn('is_active', [
51
                'title' => 'Aktywny',
52
                'decorators' => [new Boolean()],
53
                'filter' => new Select(['options' => $booleanOptions])
54
            ])
55
            ->addColumn('is_blocked', [
56
                'title' => 'Zablokowany',
57
                'decorators' => [new Boolean()],
58
                'filter' => new Select(['options' => $booleanOptions])
59
            ])
60
            ->addColumn('ip', [
61
                'title' => 'IP',
62
                'decorators' => [new Ip()],
63
                'filter' => new Text(['operator' => FilterOperator::OPERATOR_ILIKE])
64
            ])
65
            ->addColumn('reputation', [
66
                'title' => 'Reputacja',
67
                'sortable' => true
68
            ])
69
            ->addRowAction(new FirewallButton(function (User $user) {
70
                return route('adm.firewall.save') . '?' . http_build_query(['user' => $user->id, 'ip' => $user->ip]);
71
            }))
72
            ->addRowAction(new EditButton(function (User $user) {
73
                return route('adm.users.save', [$user->id]);
74
            }));
75
    }
76
}
77