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