| Conditions | 14 |
| Paths | 840 |
| Total Lines | 56 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 50 | public function handle() |
||
| 51 | { |
||
| 52 | $connection = $this->mongodb_connection; |
||
| 53 | $collection = $this->collection; |
||
| 54 | $items = $this->items; |
||
| 55 | $userId = $this->userId; |
||
| 56 | $params = $this->params; |
||
| 57 | $type = $this->type; |
||
| 58 | |||
| 59 | $valid = ($items instanceof Collection) ? $items->count() : ($items instanceof Model) ? 1 : $type != 'update' ? count($items) : 1; |
||
| 60 | |||
| 61 | if ($valid) { |
||
| 62 | $collection = str_plural($collection); |
||
| 63 | $items = $type == 'update' ? $items : array_wrap(($items instanceof Paginator || $items instanceof LengthAwarePaginator) ? $items->items() : $items); |
||
| 64 | $postEvent = in_array($collection, config('apanalytics.format_collections')); |
||
| 65 | $event = $postEvent || $type == 'update' ? [] : $this->addExtraEventData($items, $userId, $params); |
||
| 66 | |||
| 67 | try { |
||
| 68 | if ($type == 'insert') { |
||
| 69 | if ($postEvent) { |
||
| 70 | foreach ($items as $item) { |
||
| 71 | $basename = strtolower(class_basename($item)); |
||
| 72 | |||
| 73 | $data = [ |
||
| 74 | $basename => [ |
||
| 75 | 'id' => $item->id ?? null, |
||
| 76 | 'type' => $item->type ?? null, |
||
| 77 | ], |
||
| 78 | 'business' => [ |
||
| 79 | 'id' => $item->business->id ?? null, |
||
| 80 | ], |
||
| 81 | ]; |
||
| 82 | |||
| 83 | // Add Extra Stuff |
||
| 84 | $data = $this->addExtraEventData($data, $userId, $params); |
||
| 85 | |||
| 86 | event(new AnalyticTracked($collection, $basename, $data)); |
||
| 87 | |||
| 88 | $event[] = $data; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | return DB::connection($connection) |
||
| 93 | ->collection($collection) |
||
| 94 | ->insert($event); |
||
| 95 | } |
||
| 96 | |||
| 97 | // Type is update |
||
| 98 | $basename = strtolower(str_singular($collection)); |
||
| 99 | |||
| 100 | return DB::connection($connection) |
||
| 101 | ->collection($collection) |
||
| 102 | ->where("{$basename}_id", $items) |
||
| 103 | ->update($params); |
||
| 104 | } catch (\Exception $e) { |
||
| 105 | Log::error('Error Logging Event', ['error' => $e->getMessage()]); |
||
| 106 | } |
||
| 127 |