Conditions | 15 |
Paths | 324 |
Total Lines | 85 |
Code Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 8 | ||
Bugs | 2 | Features | 1 |
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 |
||
53 | public function handle() |
||
54 | { |
||
55 | $connection = $this->mongodb_connection; |
||
56 | $collection = $this->collection; |
||
57 | $items = $this->items; |
||
58 | $userId = $this->userId; |
||
59 | $params = $this->params; |
||
60 | $type = $this->type; |
||
61 | $valid = true; |
||
62 | |||
63 | if ($type != 'update') { |
||
64 | if ($items != null) { |
||
65 | $valid = ($items instanceof Collection) ? $items->count() : ($items instanceof Model) ? 1 : count($items); |
||
66 | } else { |
||
67 | $valid = false; |
||
68 | } |
||
69 | } |
||
70 | |||
71 | if ($valid) { |
||
72 | $collection = Str::plural($collection); |
||
73 | $items = $this->formatItems($items); |
||
74 | $postEvent = in_array($collection, config('apanalytics.format_collections')); |
||
75 | $event = $this->prepEventData($postEvent, $items, $userId, $params, $collection); |
||
76 | |||
77 | try { |
||
78 | if ($type == 'insert') { |
||
79 | if ($postEvent) { |
||
80 | foreach ($items as $item) { |
||
81 | $basename = strtolower(class_basename($item)); |
||
82 | |||
83 | $data = [ |
||
84 | $basename => [ |
||
85 | 'id' => $item->id ?? null, |
||
86 | 'type' => $item->type ?? null, |
||
87 | ], |
||
88 | 'business' => [ |
||
89 | 'id' => $item->business->id ?? null, |
||
90 | ], |
||
91 | ]; |
||
92 | |||
93 | // Add Extra Stuff |
||
94 | $data = $this->addExtraEventData($data, $userId, $params); |
||
95 | |||
96 | event(new AnalyticTracked($collection, $basename, $data)); |
||
97 | |||
98 | if ($item->business) { |
||
99 | event(new AnalyticTracked($collection, 'business', ['business' => ['id' => $item->business->id ?? null]])); |
||
100 | } |
||
101 | |||
102 | $event[] = $data; |
||
103 | } |
||
104 | } |
||
105 | |||
106 | // Basic created ie user |
||
107 | if (! $postEvent && $collection != 'visits') { |
||
108 | foreach ($items as $item) { |
||
109 | $basename = strtolower(Str::singular($collection)); |
||
110 | $data = [ |
||
111 | $basename => [ |
||
112 | 'id' => $item->id ?? null, |
||
113 | ], |
||
114 | ]; |
||
115 | |||
116 | event(new AnalyticTracked($collection, $basename, $data)); |
||
117 | |||
118 | if ($item->business) { |
||
119 | event(new AnalyticTracked($collection, 'business', ['business' => ['id' => $item->business->id ?? null]])); |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | |||
124 | return DB::connection($connection) |
||
125 | ->collection($collection) |
||
126 | ->insert($event); |
||
127 | } |
||
128 | |||
129 | // Type is update |
||
130 | $basename = strtolower(Str::singular($collection)); |
||
131 | |||
132 | return DB::connection($connection) |
||
133 | ->collection($collection) |
||
134 | ->where("{$basename}_id", $items) |
||
135 | ->update($params); |
||
136 | } catch (\Exception $e) { |
||
137 | Log::error('Error Logging Event', ['error' => $e->getMessage()]); |
||
138 | } |
||
186 |