Conditions | 5 |
Paths | 6 |
Total Lines | 52 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
80 | public function sumChartsByPeriod(Request $request) |
||
81 | { |
||
82 | $rules = [ |
||
83 | 'dateStart' => 'required|date', |
||
84 | 'dateEnd' => 'required|date', |
||
85 | ]; |
||
86 | |||
87 | $validator = \Validator::make($request->all(), $rules); |
||
88 | |||
89 | if ($validator->fails()) { |
||
90 | return response()->json(['status' => 'error', 'errors' => $validator->errors()]); |
||
91 | } |
||
92 | |||
93 | $user = $this->model->where('id', $request->user()->id)->first(); |
||
94 | |||
95 | if (!$user) { |
||
96 | return response()->json(['status' => 'error', 'message' => 'Opss. Usuário não foi encontrado, favor verifique se esta logado.']); |
||
97 | } |
||
98 | |||
99 | $dateStart = $request->dateStart; |
||
100 | $dateEnd = $request->dateEnd; |
||
101 | |||
102 | $categoriesPay = $user->categories()->selectRaw('categories.name, sum(value) as value') |
||
103 | ->leftJoin('bill_pays', 'bill_pays.category_id', '=', 'categories.id') |
||
104 | ->whereBetween('date_launch', [$dateStart, $dateEnd]) |
||
105 | ->whereNotNull('bill_pays.category_id') |
||
106 | ->groupBy('categories.name') |
||
107 | ->where('status', '1') |
||
108 | ->get(); |
||
109 | |||
110 | $categoriesReceive = $user->categories()->selectRaw('categories.name, sum(value) as value') |
||
111 | ->leftJoin('bill_receives', 'bill_receives.category_id', '=', 'categories.id') |
||
112 | ->whereBetween('date_launch', [$dateStart, $dateEnd]) |
||
113 | ->whereNotNull('bill_receives.category_id') |
||
114 | ->groupBy('categories.name') |
||
115 | ->where('status', '1') |
||
116 | ->get(); |
||
117 | |||
118 | foreach ($categoriesPay as $key => $value) { |
||
119 | $categoriesPay[$key]['name'] = $value->name; |
||
120 | $categoriesPay[$key]['y'] = (float)$value->value; |
||
121 | } |
||
122 | |||
123 | foreach ($categoriesReceive as $key => $value) { |
||
124 | $categoriesReceive[$key]['name'] = $value->name; |
||
125 | $categoriesReceive[$key]['y'] = (float)$value->value; |
||
126 | } |
||
127 | |||
128 | $data["categoriesPay"] = $categoriesPay; |
||
129 | $data["categoriesReceive"] = $categoriesReceive; |
||
130 | |||
131 | return response()->json(['status' => 'success', 'data' => $data]); |
||
132 | } |
||
159 | } |