ReportController::chartdataHelptopic()   F
last analyzed

Complexity

Conditions 33
Paths 210

Size

Total Lines 198
Code Lines 140

Duplication

Lines 50
Ratio 25.25 %

Importance

Changes 0
Metric Value
cc 33
eloc 140
nc 210
nop 4
dl 50
loc 198
rs 3.995
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;
4
5
// controllers
6
use App\Http\Controllers\Controller;
7
use App\Model\helpdesk\Manage\Help_topic;
8
// request
9
use App\Model\helpdesk\Ticket\Tickets;
10
// Model
11
use Illuminate\Http\Request;
12
// classes
13
use PDF;
14
15
/**
16
 * ReportController
17
 * This controlleris used to fetch reports in the agent panel.
18
 *
19
 * @author      Ladybird <[email protected]>
20
 */
21
class ReportController extends Controller
22
{
23
    /**
24
     * Create a new controller instance.
25
     * constructor to check
26
     * 1. authentication
27
     * 2. user roles
28
     * 3. roles must be agent.
29
     *
30
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
31
     */
32
    public function __construct()
33
    {
34
        // checking for authentication
35
        $this->middleware('auth');
36
        // checking if the role is agent
37
        $this->middleware('role.agent');
38
    }
39
40
    /**
41
     * Get the Report page.
42
     *
43
     * @return type view
44
     */
45
    public function index()
46
    {
47
        try {
48
            return view('themes.default1.agent.helpdesk.report.index');
49
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class App\Http\Controllers\Agent\helpdesk\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
50
        }
51
    }
52
53
    /**
54
     * function to get help_topic graph.
55
     *
56
     * @param type $date111
57
     * @param type $date122
58
     * @param type $helptopic
59
     */
60
    public function chartdataHelptopic(Request $request, $date111 = '', $date122 = '', $helptopic = '')
61
    {
62
        $date11 = strtotime($date122);
63
        $date12 = strtotime($date111);
64
        $help_topic = $helptopic;
65
        $duration = $request->input('duration');
66
        if ($date11 && $date12 && $help_topic) {
67
            $date2 = $date12;
68
            $date1 = $date11;
69
            $duration = null;
70
            if ($request->input('open') == null || $request->input('closed') == null || $request->input('reopened') == null || $request->input('overdue') == null || $request->input('deleted') == null) {
71
                $duration = 'day';
72
            }
73
        } else {
74
            // generating current date
75
            $date2 = strtotime(date('Y-m-d'));
76
            $date3 = date('Y-m-d');
77
            $format = 'Y-m-d';
78
            // generating a date range of 1 month
79
            if ($request->input('duration') == 'day') {
80
                $date1 = strtotime(date($format, strtotime('-15 day'.$date3)));
81
            } elseif ($request->input('duration') == 'week') {
82
                $date1 = strtotime(date($format, strtotime('-69 days'.$date3)));
83
            } elseif ($request->input('duration') == 'month') {
84
                $date1 = strtotime(date($format, strtotime('-179 days'.$date3)));
85
            } else {
86
                $date1 = strtotime(date($format, strtotime('-30 days'.$date3)));
87
            }
88
//            $help_topic = Help_topic::where('status', '=', '1')->min('id');
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% 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...
89
        }
90
91
        $return = '';
92
        $last = '';
0 ignored issues
show
Unused Code introduced by
$last 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...
93
        $j = 0;
94
        $created1 = '';
95
        $closed1 = '';
96
        $reopened1 = '';
97
        $in_progress = \DB::table('tickets')->where('help_topic_id', '=', $help_topic)->where('status', '=', 1)->count();
98
99
        for ($i = $date1; $i <= $date2; $i = $i + 86400) {
100
            $j++;
101
            $thisDate = date('Y-m-d', $i);
102
            $thisDate1 = date('jS F', $i);
103
            $open_array = [];
104
            $closed_array = [];
105
            $reopened_array = [];
106
107
            if ($request->input('open') || $request->input('closed') || $request->input('reopened')) {
108 View Code Duplication
                if ($request->input('open') && $request->input('open') == 'on') {
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...
109
                    $created = \DB::table('tickets')->select('created_at')->where('help_topic_id', '=', $help_topic)->where('created_at', 'LIKE', '%'.$thisDate.'%')->count();
110
                    $open_array = ['open' => $created];
111
                }
112 View Code Duplication
                if ($request->input('closed') && $request->input('closed') == 'on') {
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
                    $closed = \DB::table('tickets')->select('closed_at')->where('help_topic_id', '=', $help_topic)->where('closed_at', 'LIKE', '%'.$thisDate.'%')->count();
114
                    $closed_array = ['closed' => $closed];
115
                }
116 View Code Duplication
                if ($request->input('reopened') && $request->input('reopened') == 'on') {
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...
117
                    $reopened = \DB::table('tickets')->select('reopened_at')->where('help_topic_id', '=', $help_topic)->where('reopened_at', 'LIKE', '%'.$thisDate.'%')->count();
118
                    $reopened_array = ['reopened' => $reopened];
119
                }
120
//                if ($request->input('overdue') && $request->input('overdue') == 'on') {
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
121
//                    $overdue = Tickets::where('status', '=', 1)->where('isanswered', '=', 0)->where('dept_id', '=', $dept->id)->orderBy('id', 'DESC')->get();
122
//                }
123
//                        $open_array = ['open'=>$created1];
124
//                        $closed_array = ['closed'=>$closed1];
125
//                        $reopened_array = ['reopened'=>$reopened1];
126
                $value = ['date' => $thisDate1];
127
//                        if($open_array) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
128
                $value = array_merge($value, $open_array);
129
                $value = array_merge($value, $closed_array);
130
                $value = array_merge($value, $reopened_array);
131
                $value = array_merge($value, ['inprogress' => $in_progress]);
132
//                        } else {
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% 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...
133
//                            $value = "";
134
//                        }
135
                $array = array_map('htmlentities', $value);
136
                $json = html_entity_decode(json_encode($array));
137
                $return .= $json.',';
138
            } else {
139
                if ($duration == 'week') {
140
                    $created = \DB::table('tickets')->select('created_at')->where('help_topic_id', '=', $help_topic)->where('created_at', 'LIKE', '%'.$thisDate.'%')->count();
141
                    $created1 += $created;
142
                    $closed = \DB::table('tickets')->select('closed_at')->where('help_topic_id', '=', $help_topic)->where('closed_at', 'LIKE', '%'.$thisDate.'%')->count();
143
                    $closed1 += $closed;
144
                    $reopened = \DB::table('tickets')->select('reopened_at')->where('help_topic_id', '=', $help_topic)->where('reopened_at', 'LIKE', '%'.$thisDate.'%')->count();
145
                    $reopened1 += $reopened;
146 View Code Duplication
                    if ($j % 7 == 0) {
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...
147
                        $open_array = ['open' => $created1];
148
                        $created1 = 0;
149
                        $closed_array = ['closed' => $closed1];
150
                        $closed1 = 0;
151
                        $reopened_array = ['reopened' => $reopened1];
152
                        $reopened1 = 0;
153
                        $value = ['date' => $thisDate1];
154
//                        if($open_array) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
155
                        $value = array_merge($value, $open_array);
156
                        $value = array_merge($value, $closed_array);
157
                        $value = array_merge($value, $reopened_array);
158
                        $value = array_merge($value, ['inprogress' => $in_progress]);
159
//                        } else {
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% 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...
160
//                            $value = "";
161
//                        }
162
                        $array = array_map('htmlentities', $value);
163
                        $json = html_entity_decode(json_encode($array));
164
                        $return .= $json.',';
165
                    }
166
                } elseif ($duration == 'month') {
167
                    $created_month = \DB::table('tickets')->select('created_at')->where('help_topic_id', '=', $help_topic)->where('created_at', 'LIKE', '%'.$thisDate.'%')->count();
168
                    $created1 += $created_month;
169
                    $closed_month = \DB::table('tickets')->select('closed_at')->where('help_topic_id', '=', $help_topic)->where('closed_at', 'LIKE', '%'.$thisDate.'%')->count();
170
                    $closed1 += $closed_month;
171
                    $reopened_month = \DB::table('tickets')->select('reopened_at')->where('help_topic_id', '=', $help_topic)->where('reopened_at', 'LIKE', '%'.$thisDate.'%')->count();
172
                    $reopened1 += $reopened_month;
173 View Code Duplication
                    if ($j % 30 == 0) {
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...
174
                        $open_array = ['open' => $created1];
175
                        $created1 = 0;
176
                        $closed_array = ['closed' => $closed1];
177
                        $closed1 = 0;
178
                        $reopened_array = ['reopened' => $reopened1];
179
                        $reopened1 = 0;
180
                        $value = ['date' => $thisDate1];
181
182
                        $value = array_merge($value, $open_array);
183
                        $value = array_merge($value, $closed_array);
184
                        $value = array_merge($value, $reopened_array);
185
                        $value = array_merge($value, ['inprogress' => $in_progress]);
186
187
                        $array = array_map('htmlentities', $value);
188
                        $json = html_entity_decode(json_encode($array));
189
                        $return .= $json.',';
190
                    }
191
                } else {
192
                    if ($request->input('default') == null) {
193
                        $help_topic = Help_topic::where('status', '=', '1')->min('id');
194
                    }
195
                    $created = \DB::table('tickets')->select('created_at')->where('help_topic_id', '=', $help_topic)->where('created_at', 'LIKE', '%'.$thisDate.'%')->count();
196
                    $open_array = ['open' => $created];
0 ignored issues
show
Unused Code introduced by
$open_array 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...
197
                    $closed = \DB::table('tickets')->select('closed_at')->where('help_topic_id', '=', $help_topic)->where('closed_at', 'LIKE', '%'.$thisDate.'%')->count();
198
                    $closed_array = ['closed' => $closed];
0 ignored issues
show
Unused Code introduced by
$closed_array 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...
199
                    $reopened = \DB::table('tickets')->select('reopened_at')->where('help_topic_id', '=', $help_topic)->where('reopened_at', 'LIKE', '%'.$thisDate.'%')->count();
200
                    $reopened_array = ['reopened' => $reopened];
0 ignored issues
show
Unused Code introduced by
$reopened_array 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...
201
                    if ($j % 1 == 0) {
202
                        $open_array = ['open' => $created];
203
                        $created = 0;
0 ignored issues
show
Unused Code introduced by
$created 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...
204
                        $closed_array = ['closed' => $closed];
205
                        $closed = 0;
0 ignored issues
show
Unused Code introduced by
$closed 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...
206
                        $reopened_array = ['reopened' => $reopened];
207
                        $reopened = 0;
0 ignored issues
show
Unused Code introduced by
$reopened 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...
208
                        $value = ['date' => $thisDate1];
209
                        if ($request->input('default') == null) {
210
                            $value = array_merge($value, $open_array);
211
                            $value = array_merge($value, $closed_array);
212
                            $value = array_merge($value, $reopened_array);
213
                            $value = array_merge($value, ['inprogress' => $in_progress]);
214
                        } else {
215
                            if ($duration == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $duration of type string|null|array against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
216
                                if ($request->input('open') == 'on') {
217
                                    $value = array_merge($value, $open_array);
218
                                }
219
                                if ($request->input('closed') == 'on') {
220
                                    $value = array_merge($value, $closed_array);
221
                                }
222
                                if ($request->input('reopened') == 'on') {
223
                                    $value = array_merge($value, $reopened_array);
224
                                }
225
                            } else {
226
                                $value = array_merge($value, $open_array);
227
                                $value = array_merge($value, $closed_array);
228
                                $value = array_merge($value, $reopened_array);
229
                                $value = array_merge($value, ['inprogress' => $in_progress]);
230
                            }
231
                        }
232
233
//                        } else {
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% 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...
234
//                            $value = "";
235
//                        }
236
                        $array = array_map('htmlentities', $value);
237
                        $json = html_entity_decode(json_encode($array));
238
                        $return .= $json.',';
239
                    }
240
                }
241
            }
242
243
//            $value = ['date' => $thisDate];
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% 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...
244
//            if($open_array) {
245
//                $value = array_merge($value,$open_array);
246
//            }
247
//            if($closed_array) {
248
//                $value = array_merge($value,$closed_array);
249
//            }
250
//            if($reopened_array) {
251
//                $value = array_merge($value,$reopened_array);
252
//            }
253
        }
254
        $last = rtrim($return, ',');
255
256
        return '['.$last.']';
257
    }
258
259
    public function helptopicPdf(Request $request)
260
    {
261
        $table_datas = json_decode($request->input('pdf_form'));
262
        $table_help_topic = json_decode($request->input('pdf_form_help_topic'));
263
        $html = view('themes.default1.agent.helpdesk.report.pdf', compact('table_datas', 'table_help_topic'))->render();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
264
        $html1 = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
265
266
        return PDF::load($html1)->show();
267
    }
268
}
269