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

SessionsGrid   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B buildGrid() 0 63 3
1
<?php
2
3
namespace Coyote\Http\Grids\Adm;
4
5
use Boduch\Grid\Decorators\Ip;
6
use Boduch\Grid\Filters\FilterOperator;
7
use Boduch\Grid\Filters\Text;
8
use Boduch\Grid\Row;
9
use Carbon\Carbon;
10
use Coyote\Services\Grid\Grid;
11
use Boduch\Grid\Order;
12
use Coyote\Session;
13
use Jenssegers\Agent\Agent;
14
15
class SessionsGrid extends Grid
16
{
17
    public function buildGrid()
18
    {
19
        $this
20
            ->setDefaultOrder(new Order('updated_at', 'desc'))
21
            ->addColumn('name', [
22
                'title' => 'Nazwa użytkownika',
23
                'sortable' => true,
24
                'clickable' => function (Session $session) {
25
                    if ($session->userId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $session->userId of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
26
                        return link_to_route('adm.users.save', $session->name, [$session->userId]);
27
                    } else {
28
                        return $session->robot ?: '--';
29
                    }
30
                },
31
                'filter' => new Text(['operator' => FilterOperator::OPERATOR_ILIKE])
32
            ])
33
            ->addColumn('created_at', [
34
                'title' => 'Data logowania',
35
                'sortable' => true,
36
                'render' => function (Session $session) {
37
                    return Carbon::createFromTimestamp($session->createdAt);
38
                },
39
                'decorator' => [$this->getDateTimeDecorator()]
40
            ])
41
            ->addColumn('updated_at', [
42
                'title' => 'Ostatnia aktywność',
43
                'sortable' => true,
44
                'render' => function (Session $session) {
45
                    return Carbon::createFromTimestamp($session->updatedAt);
46
                },
47
                'decorator' => [$this->getDateTimeDecorator()]
48
            ])
49
            ->addColumn('ip', [
50
                'title' => 'IP',
51
                'decorators' => [new Ip()],
52
                'filter' => new Text(['operator' => FilterOperator::OPERATOR_ILIKE, 'name' => 'sessions.ip'])
53
            ])
54
            ->addColumn('path', [
55
                'title' => 'Strona',
56
                'render' => function (Session $session) {
57
                    return link_to($session->path);
58
                },
59
                'filter' => new Text(['operator' => FilterOperator::OPERATOR_ILIKE])
60
            ])
61
            ->addColumn('browser', [
62
                'title' => 'Przeglądarka'
63
            ])
64
            ->addColumn('platform', [
65
                'title' => 'System operacyjny'
66
            ])
67
            ->addColumn('user_agent', [
68
                'title' => 'User-agent',
69
                'filter' => new Text(['operator' => FilterOperator::OPERATOR_ILIKE, 'name' => 'sessions.browser'])
70
            ])
71
            ->after(function (Row $row) {
72
                $agent = new Agent();
73
                $agent->setUserAgent($row->raw('browser'));
74
75
                $row->get('platform')->setValue($agent->platform());
76
                $row->get('browser')->setValue($agent->browser());
77
                $row->get('user_agent')->setValue($row->raw('browser'));
78
            });
79
    }
80
}
81