UserTable::columns()   D
last analyzed

Complexity

Conditions 17
Paths 1

Size

Total Lines 111
Code Lines 88

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 17
eloc 88
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 111
rs 4.4351

How to fix   Long Method    Complexity   

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 App\Http\Livewire;
4
5
use App\Http\Livewire;
6
use Rappasoft\LaravelLivewireTables\DataTableComponent;
7
use Rappasoft\LaravelLivewireTables\Views\Column;
8
use App\Models\User;
9
use App\Models\Link;
10
11
class UserTable extends DataTableComponent
12
{
13
    protected $model = User::class;
14
15
    public function configure(): void
16
    {
17
        $this->setPrimaryKey('id');
18
        $this->setDefaultSort('created_at', 'asc');
19
        $this->setPerPageAccepted([50, 100, 250, 500, 1000, -1]);
20
        $this->setColumnSelectEnabled();
21
    }
22
23
    public function columns(): array
24
    {
25
        return [
26
            // Column::make("", "id")
27
            // ->format(function ($value, $row, Column $column) {
28
            //     return view('components.table-components.select', ['user' => $row]);
29
            // }),
30
            Column::make(__('messages.ID'), "id")
31
                ->sortable()
32
                ->searchable(),
33
            Column::make(__('messages.Name'), "name")
34
                ->sortable()
35
                ->searchable(),
36
            Column::make(__('messages.E-Mail'), "email")
37
                ->sortable()
38
                ->searchable(),
39
            Column::make(__('messages.Page'), "littlelink_name")
40
                ->sortable()
41
                ->searchable()
42
                ->format(function ($value, $row, Column $column) {
43
                    if (!$row->littlelink_name == NULL) {
44
                        return "<a href='" . url('') . "/@" . htmlspecialchars($row->littlelink_name) . "' target='_blank' class='text-info'><i class='bi bi-box-arrow-up-right'></i>&nbsp; " . htmlspecialchars($row->littlelink_name) . " </a>";
45
                    } else {
46
                        return 'N/A';
47
                    }
48
                })
49
                ->html(),
50
            Column::make(__('messages.Role'), "role")
51
                ->sortable()
52
                ->searchable(),
53
            Column::make(__('messages.Links'), "id")
54
                ->format(function ($value, $row) {
55
                    $linkCount = Link::where('user_id', $row->id)->count();
56
                    return $linkCount;
57
                }),
58
            Column::make(__('messages.Clicks'), "id")
59
                ->format(function ($value, $row) {
60
                    $clicksSum = Link::where('user_id', $row->id)->sum('click_number');
61
                    return $clicksSum;
62
                }),
63
            Column::make(__('messages.E-Mail'), "email_verified_at")
64
                ->sortable()
65
                ->format(function ($value, $row, Column $column) {
66
                    if (env('REGISTER_AUTH') !== 'auth') {
67
                        if ($row->role == 'admin' && $row->email_verified_at != '') {
68
                            return '<div class="text-center">-</div>';
69
                        } else {
70
                            if($row->email_verified_at == ''){
71
                                $verifyLinkBool = 'true';
72
                            } else {
73
                                $verifyLinkBool = 'false';
74
                            }
75
                            $verifyLink = route('verifyUser', [
76
                                'verify' => $verifyLinkBool,
77
                                'id' => $row->id
78
                            ]);
79
                            if ($row->email_verified_at == '') {
80
                                return '<div class="text-center"><a style="cursor:pointer" data-id="'.$verifyLink.'" class="user-email text-danger"><span class="badge bg-danger">' . __('messages.Pending') . '</span></a></div>';
81
                            } else {
82
                                return '<div class="text-center"><a style="cursor:pointer" data-id="'.$verifyLink.'" class="user-email text-danger"><span class="badge bg-success">' . __('messages.Verified') . '</span></a></div>';
83
                            }
84
                        }
85
                    } else {
86
                        return '<div class="text-center">-</div>';
87
                    }
88
                    return '';
0 ignored issues
show
Unused Code introduced by
return '' is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
89
                })->html(),
90
            Column::make(__('messages.Status'), "block")
91
                ->sortable()
92
                ->format(function ($value, $row, Column $column) {
93
                    if ($row->role === 'admin' && $row->id === 1) {
94
                        return '<div class="text-center">-</div>';
95
                    } else {
96
                        $route = route('blockUser', ['block' => $row->block, 'id' => $row->id]);
97
                        if ($row->block === 'yes') {
98
                            $badge = '<div class="text-center"><a style="cursor:pointer" data-id="'.$route.'" class="user-block text-danger"><span class="badge bg-danger">'.__('messages.Pending').'</span></a></div>';
99
                        } elseif ($row->block === 'no') {
100
                            $badge = '<div class="text-center"><a style="cursor:pointer" data-id="'.$route.'" class="user-block text-danger"><span class="badge bg-success">'.__('messages.Approved').'</span></a></div>';
101
                        }
102
                        return "<a href=\"$route\">$badge</a>";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $badge does not seem to be defined for all execution paths leading up to this point.
Loading history...
103
                    }
104
                })
105
                ->html(),
106
            Column::make(__('messages.Created at'), "created_at")
107
                ->sortable()
108
                ->format(function ($value) {
109
                    if ($value) {
110
                        return $value->format('d/m/y');
111
                    } else {
112
                        return '';
113
                    }
114
                }),
115
            Column::make(__('messages.Last seen'), "updated_at")
116
                ->sortable()
117
                ->format(function ($value) {
118
                    $now = now();
119
                    $diff = $now->diff($value);
120
            
121
                    if ($diff->d < 1 && $diff->h < 1) {
122
                        return 'Now';
123
                    } elseif ($diff->d < 1 && $diff->h < 24) {
124
                        return $diff->h . ' hours ago';
125
                    } elseif ($diff->d < 365) {
126
                        return $diff->d . ' days ago';
127
                    } else {
128
                        return $diff->y . ' years ago';
129
                    }
130
                }),
131
                Column::make(__('messages.Action'), "id")
132
                ->format(function ($value, $row, Column $column) {
133
                    return view('components.table-components.action', ['user' => $row]);
134
                }),
135
        ];
136
    }
137
}
138