Conditions | 26 |
Paths | 13832 |
Total Lines | 133 |
Code Lines | 74 |
Lines | 0 |
Ratio | 0 % |
Changes | 10 | ||
Bugs | 2 | Features | 2 |
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 |
||
58 | public function show($collection, $interval = 'count', $timeframe = null, $filters = null, $groupBy = null, $distinct = null) |
||
59 | { |
||
60 | $start = $timeframe ? Arr::get($timeframe, 'start') : null; |
||
61 | $end = $timeframe ? Arr::get($timeframe, 'end') : null; |
||
62 | $matchArray = []; |
||
63 | $filters = valid_json($filters) ? json_decode($filters) : $filters; |
||
64 | $intervalFormat = '%Y-%m-%dT%H'; |
||
65 | $aggregate = []; |
||
66 | $model = $this->namespace.Str::studly(Str::singular($collection)).'Analytic'; |
||
67 | |||
68 | if (! class_exists($model)) { |
||
69 | throw new InvalidArgumentException("Model {$model} does not exist."); |
||
70 | } |
||
71 | |||
72 | if ($filters) { |
||
73 | foreach ($filters as $filter) { |
||
74 | if (is_array($filter)) { |
||
75 | $matchArray = array_merge($matchArray, $filter); |
||
76 | } else { |
||
77 | $propertyValue = $filter->property_value; |
||
78 | |||
79 | if (is_numeric($propertyValue)) { |
||
80 | $propertyValue = (int) $propertyValue; |
||
81 | } |
||
82 | |||
83 | $matchArray = array_merge($matchArray, [$filter->property_name => $propertyValue]); |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | |||
88 | if (! app()->runningInConsole()) { |
||
89 | abort_unless(auth()->check() && auth()->user()->can('view', [(new $model), $matchArray]), 403, 'You dont have permission to view these analytics'); |
||
90 | } |
||
91 | |||
92 | if ($start) { |
||
93 | $matchArray['created_at']['$gte'] = mongoTime($start); |
||
94 | } |
||
95 | |||
96 | if ($end) { |
||
97 | $matchArray['created_at']['$lt'] = mongoTime($end); |
||
98 | } |
||
99 | |||
100 | if ($matchArray) { |
||
101 | $aggregate[] = ['$match' => $matchArray]; |
||
102 | } |
||
103 | |||
104 | if ($distinct) { |
||
105 | $aggregate[] = [ |
||
106 | '$group' => [ |
||
107 | '_id' => '$' . $distinct, |
||
108 | 'created_at' => [ |
||
109 | '$last' => '$created_at', |
||
110 | ], |
||
111 | ], |
||
112 | ]; |
||
113 | } |
||
114 | |||
115 | if ($interval != 'count') { |
||
116 | if ($interval == 'daily') { |
||
117 | $intervalFormat = '%Y-%m-%d'; |
||
118 | } |
||
119 | |||
120 | if ($interval == 'weekly') { |
||
121 | $intervalFormat = '%Y-%U'; |
||
122 | } |
||
123 | |||
124 | if ($interval == 'monthly' || $interval == 'growth') { |
||
125 | $intervalFormat = '%Y-%m'; |
||
126 | } |
||
127 | |||
128 | $aggregate[] = [ |
||
129 | '$group' => [ |
||
130 | '_id' => [ |
||
131 | '$dateToString' => ['date' => '$created_at', 'format' => $intervalFormat], |
||
132 | ], |
||
133 | 'count' => [ |
||
134 | '$sum' => 1, |
||
135 | ], |
||
136 | 'created_at' => [ |
||
137 | '$last' => '$created_at', |
||
138 | ], |
||
139 | ], |
||
140 | ]; |
||
141 | |||
142 | $aggregate[] = ['$sort' => ['created_at' => 1]]; |
||
143 | |||
144 | $aggregate[] = [ |
||
145 | '$project' => [ |
||
146 | '_id' => 0, |
||
147 | 'created_at' => 1, |
||
148 | 'count' => 1, |
||
149 | ], |
||
150 | ]; |
||
151 | } |
||
152 | |||
153 | if ($interval == 'count' && $groupBy != null) { |
||
154 | $nested = Str::contains($groupBy, '.'); |
||
155 | |||
156 | if ($nested) { |
||
157 | $aggregate[] = ['$unwind' => '$'.Str::before($groupBy, '.')]; |
||
158 | } |
||
159 | |||
160 | $aggregate[] = [ |
||
161 | '$group' => [ |
||
162 | '_id' => '$'.$groupBy, |
||
163 | 'count' => [ |
||
164 | '$sum' => 1, |
||
165 | ], |
||
166 | ], |
||
167 | ]; |
||
168 | |||
169 | $aggregate[] = ['$sort' => ['count' => 1]]; |
||
170 | |||
171 | $aggregate[] = [ |
||
172 | '$project' => [ |
||
173 | '_id' => 0, |
||
174 | Str::after($groupBy, '.') => '$_id', |
||
175 | 'count' => 1, |
||
176 | ], |
||
177 | ]; |
||
178 | } |
||
179 | |||
180 | $data = $model::raw(function ($collection) use ($matchArray, $interval, $aggregate, $groupBy) { |
||
181 | if ($interval == 'count' && ! $groupBy) { |
||
182 | return $collection->count($matchArray); |
||
183 | } |
||
184 | |||
185 | if ($aggregate) { |
||
186 | return $collection->aggregate($aggregate, ['allowDiskUse' => true]); |
||
187 | } |
||
188 | }); |
||
189 | |||
190 | return $data; |
||
191 | } |
||
193 |