FilterController::segments()   D
last analyzed

Complexity

Conditions 25
Paths 25

Size

Total Lines 155
Code Lines 110

Duplication

Lines 105
Ratio 67.74 %

Importance

Changes 0
Metric Value
cc 25
eloc 110
nc 25
nop 1
dl 105
loc 155
rs 4.5682
c 0
b 0
f 0

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\Controllers\Agent\helpdesk\Filter;
4
5
use App\Http\Controllers\Agent\helpdesk\TicketController;
6
use App\Http\Controllers\Controller;
7
use App\Model\helpdesk\Filters\Filter;
8
use App\Model\helpdesk\Filters\Label;
9
use App\Model\helpdesk\Ticket\Tickets;
10
use Auth;
11
use DB;
12
use Illuminate\Http\Request;
13
14
class FilterController extends Controller
15
{
16
    protected $request;
17
18
    public function __construct(Request $req)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $req. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
19
    {
20
        $this->middleware(['auth', 'role.agent']);
21
        $this->request = $req;
22
    }
23
24
    public function getFilter(Request $request)
25
    {
26
        $labels = $this->request->input('labels');
27
        $tags = $this->request->input('tags');
28
        if ($request->has('department')) {
29
            $table = $this->departmentTickets($request->input('department'), $request->input('status'));
30
        } else {
31
            $segment = $this->request->input('segment');
32
            $table = $this->segments($segment);
33
        }
34
        $tickets = [];
0 ignored issues
show
Unused Code introduced by
$tickets is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
35
        $render = false;
0 ignored issues
show
Unused Code introduced by
$render is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
36
        if (is_array($labels) && count($labels) > 0) {
37
            $table = $table
38
                     ->leftJoin('filters as label', function ($join) {
39
                         $join->on('tickets.id', '=', 'label.ticket_id')
40
                                ->where('label.key', '=', 'label');
41
                     })
42
                    ->whereIn('label.value', $labels);
43
        }
44
        if (is_array($tags) && count($tags) > 0) {
45
            $table = $table
46
                    ->leftJoin('filters as tag', function ($join) {
47
                        $join->on('tickets.id', '=', 'tag.ticket_id')
48
                                ->where('tag.key', '=', 'tag');
49
                    })
50
                    ->whereIn('tag.value', $tags);
51
        }
52
        if ((is_array($tags) && count($tags) > 0) || (is_array($labels) && count($labels) > 0)) {
53
            $render = true;
0 ignored issues
show
Unused Code introduced by
$render is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
54
        }
55
        // return \Datatables::of($table)->make();
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
56
        return \Ttable::getTable($table);
57
    }
58
59
    public function filterByKey($key, $labels = [])
60
    {
61
        $filter = new Filter();
62
        $query = $filter->where('key', $key)
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<App\Model\helpdesk\Filters\Filter>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
63
                ->where(function ($query) use ($labels) {
64
                    if (is_array($labels) && count($labels) > 0) {
65
                        for ($i = 0; $i < count($labels); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
66
                            $query->orWhere('value', 'LIKE', '%'.$labels[$i].'%');
67
                        }
68
                    }
69
                })
70
                ->lists('ticket_id')
71
                ->toArray();
72
73
        return $query;
74
    }
75
76
    public function segments($segment)
77
    {
78
        if (strpos($segment, 'user') !== false) {
79
            return $this->formatUserTickets($segment);
80
        }
81
        $table = $this->table();
82
        switch ($segment) {
83 View Code Duplication
            case '/ticket/inbox':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
                if (Auth::user()->role == 'agent') {
85
                    $id = Auth::user()->primary_dpt;
86
                    $table = $table->where('tickets.dept_id', '=', $id)->orWhere('assigned_to', '=', Auth::user()->id);
87
                }
88
89
                return $table
90
                    ->Join('ticket_status', function ($join) {
91
                        $join->on('ticket_status.id', '=', 'tickets.status')
92
                        ->whereIn('ticket_status.id', [1, 7]);
93
                    });
94 View Code Duplication
            case '/ticket/closed':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
                if (Auth::user()->role == 'agent') {
96
                    $id = Auth::user()->primary_dpt;
97
                    $table = $table->where('tickets.dept_id', '=', $id);
98
                }
99
100
                return $table
101
                    ->Join('ticket_status', function ($join) {
102
                        $join->on('ticket_status.id', '=', 'tickets.status')
103
                                ->whereIn('ticket_status.state', ['closed']);
104
                    });
105
            case '/ticket/myticket':
106
                    return $table
107
                      ->leftJoin('ticket_status', function ($join) {
108
                          $join->on('ticket_status.id', '=', 'tickets.status');
109
                      })
110
                    ->orWhere('tickets.assigned_to', '=', Auth::user()->id)
111
                    ->where('tickets.status', '=', 1);
112 View Code Duplication
            case '/unassigned':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
                if (Auth::user()->role == 'agent') {
114
                    $id = Auth::user()->primary_dpt;
115
                    $table = $table->where('tickets.dept_id', '=', $id);
116
                }
117
118
                return $table
119
                     ->leftJoin('ticket_status', function ($join) {
120
                         $join->on('ticket_status.id', '=', 'tickets.status');
121
                     })
122
                    ->where('tickets.assigned_to', '=', null)
123
                    ->where('tickets.status', '=', 1);
124
            case '/ticket/overdue':
125
                if (Auth::user()->role == 'agent') {
126
                    $id = Auth::user()->primary_dpt;
127
                    $table = $table->where('tickets.dept_id', '=', $id);
128
                }
129
130
                  return $table
131
                    ->leftJoin('ticket_status', function ($join) {
132
                        $join->on('ticket_status.id', '=', 'tickets.status');
133
                    })
134
                    ->where('tickets.status', '=', 1)
135
                    ->where('tickets.isanswered', '=', 0)
136
                    ->whereNotNull('tickets.duedate')
137
                    ->where('tickets.duedate', '!=', '00-00-00 00:00:00')
138
139
                    // ->where('duedate','>',\Carbon\Carbon::now());
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
140
                    ->where('tickets.duedate', '<', \Carbon\Carbon::now());
141 View Code Duplication
            case '/ticket/approval/closed':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
                if (Auth::user()->role == 'agent') {
143
                    $id = Auth::user()->primary_dpt;
144
                    $table = $table->where('tickets.dept_id', '=', $id);
145
                }
146
147
                return $table
148
                    ->Join('ticket_status', function ($join) {
149
                        $join->on('ticket_status.id', '=', 'tickets.status')
150
                                ->where('tickets.status', '=', 7);
151
                    });
152
153 View Code Duplication
            case '/trash':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
                if (Auth::user()->role == 'agent') {
155
                    $id = Auth::user()->primary_dpt;
156
                    $table = $table->where('tickets.dept_id', '=', $id);
157
                }
158
159
                return $table
160
                    ->Join('ticket_status', function ($join) {
161
                        $join->on('ticket_status.id', '=', 'tickets.status')
162
                                ->where('tickets.status', '=', 5);
163
                    });
164
165 View Code Duplication
            case '/ticket/answered':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
166
                if (Auth::user()->role == 'agent') {
167
                    $id = Auth::user()->primary_dpt;
168
                    $table = $table->where('tickets.dept_id', '=', $id);
169
                }
170
171
                return $table
172
                    ->Join('ticket_status', function ($join) {
173
                        $join->on('ticket_status.id', '=', 'tickets.status')
174
                                ->where('tickets.status', '=', 1)
175
                                ->where('tickets.isanswered', '=', 1);
176
                    });
177 View Code Duplication
            case '/ticket/assigned':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
                if (Auth::user()->role == 'agent') {
179
                    $id = Auth::user()->primary_dpt;
180
                    $table = $table->where('tickets.dept_id', '=', $id);
181
                }
182
183
                return $table
184
                     ->leftJoin('ticket_status', function ($join) {
185
                         $join->on('ticket_status.id', '=', 'tickets.status');
186
                     })
187
                    ->where('tickets.assigned_to', '>', 0)
188
                    ->where('tickets.status', '=', 1);
189 View Code Duplication
            case '/ticket/open':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
190
                if (Auth::user()->role == 'agent') {
191
                    $id = Auth::user()->primary_dpt;
192
                    $table = $table->where('tickets.dept_id', '=', $id);
193
                }
194
195
                return $table
196
                     ->leftJoin('ticket_status', function ($join) {
197
                         $join->on('ticket_status.id', '=', 'tickets.status');
198
                     })
199
                    ->where('isanswered', '=', 0)
200
                    ->where('tickets.status', '=', 1);
201
            case '/duetoday':
202
                if (Auth::user()->role == 'agent') {
203
                    $id = Auth::user()->primary_dpt;
204
                    $table = $table->where('tickets.dept_id', '=', $id);
205
                }
206
207
               return $table
208
                    ->leftJoin('ticket_status', function ($join) {
209
                        $join->on('ticket_status.id', '=', 'tickets.status');
210
                    })
211
                    ->where('tickets.status', '=', 1)
212
213
                    ->whereNotNull('tickets.duedate')
214
                    ->whereDate('tickets.duedate', '=', \Carbon\Carbon::now()->format('Y-m-d'));
215
216 View Code Duplication
            case '/ticket/followup':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
217
                if (Auth::user()->role == 'agent') {
218
                    $id = Auth::user()->primary_dpt;
219
                    $table = $table->where('tickets.dept_id', '=', $id);
220
                }
221
222
                return $table
223
                    ->leftJoin('ticket_status', function ($join) {
224
                        $join->on('ticket_status.id', '=', 'tickets.status');
225
                    })
226
                    ->where('tickets.status', '=', 1)
227
                    // ->where('tickets.isanswered', '=', 0)
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
228
                    ->where('tickets.follow_up', '=', 1);
229
        }
230
    }
231
232
    public function table()
233
    {
234
        // if (Auth::user()->role == 'admin') {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
235
            $ticket = new Tickets();
236
        $tickets = $ticket
0 ignored issues
show
Documentation Bug introduced by
The method leftJoin does not exist on object<App\Model\helpdesk\Ticket\Tickets>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
237
                    ->leftJoin('ticket_thread', function ($join) {
238
                        $join->on('tickets.id', '=', 'ticket_thread.ticket_id')
239
                        ->whereNotNull('title')
240
                        ->where('ticket_thread.is_internal', '<>', 1);
241
                    })
242
                    ->leftJoin('ticket_thread as ticket_thread2', 'ticket_thread2.ticket_id', '=', 'tickets.id')
243
                    ->Join('ticket_source', 'ticket_source.id', '=', 'tickets.source')
244
                    ->leftJoin('ticket_priority', 'ticket_priority.priority_id', '=', 'tickets.priority_id')
245
                    ->leftJoin('users as u', 'u.id', '=', 'tickets.user_id')
246
                    ->leftJoin('users as u1', 'u1.id', '=', 'tickets.assigned_to')
247
                    ->leftJoin('ticket_attachment', 'ticket_attachment.thread_id', '=', 'ticket_thread.id')
248
249
                    ->leftJoin('ticket_collaborator', 'ticket_collaborator.ticket_id', '=', 'tickets.id')
250
                    ->select(
251
                        'tickets.id',
252
                        'ticket_thread.title',
253
                        'tickets.ticket_number',
254
                        'ticket_priority.priority',
255
                        'u.user_name as user_name',
256
                        'u1.user_name as assign_user_name',
257
                        \DB::raw('max(ticket_thread.updated_at) as updated_at'),
258
                        \DB::raw('min(ticket_thread.updated_at) as created_at'),
259
                        'u.first_name as first_name',
260
                        'u.last_name as last_name',
261
                        'u1.first_name as assign_first_name',
262
                        'u1.last_name as assign_last_name',
263
                        'ticket_priority.priority_color',
264
                        DB::raw('COUNT(DISTINCT ticket_thread2.id) as countthread'),
265
                        DB::raw('COUNT(ticket_attachment.thread_id) as countattachment'),
266
                        DB::raw('COUNT(ticket_collaborator.ticket_id) as countcollaborator'),
267
                        'tickets.status',
268
                        'tickets.user_id',
269
                        'tickets.priority_id', 'tickets.assigned_to',
270
                        'ticket_status.name as tickets_status',
271
                        'ticket_source.css_class as css',
272
                        DB::raw('substring_index(group_concat(ticket_thread.poster order by ticket_thread.id desc) , ",", 1) as last_replier'),
273
                        DB::raw('substring_index(group_concat(ticket_thread.title order by ticket_thread.id asc) , ",", 1) as ticket_title'),
274
                        'u.active as verified')
275
                    ->groupby('tickets.id');
276
277
        return $tickets;
278
    }
279
280
    public function filter($render, $ticket_id = [])
281
    {
282
        if (Auth::user()->role == 'admin') {
283
            $tickets = Tickets::whereIn('status', [1, 7]);
284
        } else {
285
            $dept = DB::table('department')->where('id', '=', Auth::user()->primary_dpt)->first();
286
            $tickets = Tickets::whereIn('status', [1, 7])->where('dept_id', '=', $dept->id);
287
        }
288
        if ($render == true) {
289
            $tickets = $tickets->whereIn('id', $ticket_id);
290
        }
291
292
        return $tickets;
293
    }
294
295 View Code Duplication
    public function ticketController()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
296
    {
297
        $PhpMailController = new \App\Http\Controllers\Common\PhpMailController();
298
        $NotificationController = new \App\Http\Controllers\Common\NotificationController();
299
        $ticket_controller = new TicketController($PhpMailController, $NotificationController);
300
301
        return $ticket_controller;
302
    }
303
304
    public function departmentTickets($dept, $status)
305
    {
306
        $table = $this->table();
307
308
        return $table->leftJoin('department as dep', 'tickets.dept_id', '=', 'dep.id')
309
                ->leftJoin('ticket_status', 'tickets.status', '=', 'ticket_status.id')
310
                ->where('dep.name', $dept)
311
                ->where('ticket_status.name', $status);
312
    }
313
314
    /**
315
     *@category function to format and return user tickets
316
     *
317
     *@param string $segment
318
     *
319
     *@return builder
320
     */
321
    public function formatUserTickets($segment)
322
    {
323
        $convert_to_array = explode('/', $segment);
324
        $user_id = $convert_to_array[2];
325
        $user = \DB::table('users')->select('role', 'id')->where('id', '=', $user_id)->first();
326
        $table = $this->table();
327
        if ($user->role == 'user') {
328
            $table = $table->leftJoin('ticket_status', 'tickets.status', '=', 'ticket_status.id')
329
                     ->where('tickets.user_id', '=', $user->id)
330
                     ->where('ticket_status.name', $convert_to_array[3]);
331
        } else {
332
            $table = $table->leftJoin('ticket_status', 'tickets.status', '=', 'ticket_status.id')
333
                    ->where('tickets.assigned_to', '=', $user->id)
334
                    ->where('ticket_status.name', $convert_to_array[3]);
335
        }
336
337
        return $table;
338
    }
339
}
340