Conditions | 10 |
Paths | 60 |
Total Lines | 43 |
Code Lines | 28 |
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 |
||
44 | public function handle(TrackAnalytic $event) |
||
45 | { |
||
46 | $connection = $event->mongodb_connection; |
||
47 | $collection = $event->collection; |
||
48 | $items = $event->items; |
||
49 | $userId = $event->userId; |
||
50 | $params = $event->params; |
||
51 | |||
52 | $valid = ($items instanceof Collection) ? $items->count() : ($items instanceof Model) ? 1 : count($items); |
||
53 | |||
54 | if ($valid) { |
||
55 | $collection = str_plural($collection); |
||
56 | $items = array_wrap(($items instanceof Paginator || $items instanceof LengthAwarePaginator) ? $items->items() : $items); |
||
57 | $postEvent = in_array($collection, config('apanalytics.format_collections')); |
||
58 | $event = $postEvent ? [] : $this->addExtraEventData($items, $userId, $params); |
||
59 | |||
60 | try { |
||
61 | if ($postEvent) { |
||
62 | foreach ($items as $object) { |
||
63 | $basename = strtolower(class_basename($object)); |
||
64 | |||
65 | $data = [ |
||
66 | $basename => [ |
||
67 | 'id' => $object->id ?? null, |
||
68 | 'type' => $object->type ?? null, |
||
69 | ], |
||
70 | 'business' => [ |
||
71 | 'id' => $object->business->id ?? null, |
||
72 | ], |
||
73 | ]; |
||
74 | |||
75 | // Add Extra Stuff |
||
76 | $data = $this->addExtraEventData($data, $userId, $params); |
||
77 | |||
78 | $event[] = $data; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | return DB::connection($connection) |
||
83 | ->collection($collection) |
||
|
|||
84 | ->insert($event); |
||
85 | } catch (\Exception $e) { |
||
86 | Log::error('Error Logging Event', ['error' => $e->getMessage()]); |
||
87 | } |
||
108 |