Conditions | 15 |
Paths | 384 |
Total Lines | 89 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 12 | ||
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 | $basenameId = "{$basename}_id"; |
||
83 | $data = [ |
||
84 | $basename => [ |
||
85 | 'id' => $item->id ?? $item->{$basenameId} ?? null, |
||
86 | 'type' => $item->type ?? null, |
||
87 | ], |
||
88 | ]; |
||
89 | |||
90 | if ($item->business) { |
||
91 | $data = array_merge($data, ['business' => [ |
||
92 | 'id' => $item->business->id ?? null, |
||
93 | ]]); |
||
94 | } |
||
95 | |||
96 | // Add Extra Stuff |
||
97 | $data = $this->addExtraEventData($data, $userId, $params); |
||
98 | |||
99 | event(new AnalyticTracked($collection, $basename, $data)); |
||
100 | |||
101 | if ($item->business) { |
||
102 | event(new AnalyticTracked($collection, 'business', ['business' => ['id' => $item->business->id ?? null]])); |
||
103 | } |
||
104 | |||
105 | $event[] = $data; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | // Basic created ie user |
||
110 | if (! $postEvent && $collection != 'visits') { |
||
111 | foreach ($items as $item) { |
||
112 | $basename = strtolower(Str::singular($collection)); |
||
113 | $basenameId = "{$basename}_id"; |
||
114 | $data = [ |
||
115 | $basename => [ |
||
116 | 'id' => $item->{$basenameId} ?? $item->id ?? null, |
||
117 | ], |
||
118 | ]; |
||
119 | |||
120 | event(new AnalyticTracked($collection, $basename, $data)); |
||
121 | |||
122 | // if (is_object($item) && $item->business) { |
||
123 | // event(new AnalyticTracked($collection, 'business', ['business' => ['id' => $item->business->id ?? null]])); |
||
124 | // } |
||
125 | } |
||
126 | } |
||
127 | |||
128 | return DB::connection($connection) |
||
129 | ->collection($collection) |
||
130 | ->insert($event); |
||
131 | } |
||
132 | |||
133 | // Type is update |
||
134 | $basename = strtolower(Str::singular($collection)); |
||
135 | |||
136 | return DB::connection($connection) |
||
137 | ->collection($collection) |
||
138 | ->where("{$basename}_id", $items) |
||
139 | ->update($params); |
||
140 | } catch (\Exception $e) { |
||
141 | Log::error('Error Logging Event', ['error' => $e->getMessage()]); |
||
142 | } |
||
191 |