AshPowell /
APAnalytics
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace AshPowell\APAnalytics\Jobs; |
||||
| 4 | |||||
| 5 | use AshPowell\APAnalytics\Events\AnalyticTracked; |
||||
| 6 | use Illuminate\Bus\Queueable; |
||||
| 7 | use Illuminate\Contracts\Queue\ShouldQueue; |
||||
| 8 | use Illuminate\Database\Eloquent\Collection; |
||||
| 9 | use Illuminate\Database\Eloquent\Model; |
||||
| 10 | use Illuminate\Foundation\Bus\Dispatchable; |
||||
| 11 | use Illuminate\Pagination\LengthAwarePaginator; |
||||
| 12 | use Illuminate\Pagination\Paginator; |
||||
| 13 | use Illuminate\Queue\InteractsWithQueue; |
||||
| 14 | use Illuminate\Queue\SerializesModels; |
||||
| 15 | use Illuminate\Support\Arr; |
||||
| 16 | use Illuminate\Support\Facades\DB; |
||||
| 17 | use Illuminate\Support\Str; |
||||
| 18 | use Log; |
||||
| 19 | |||||
| 20 | class Track implements ShouldQueue |
||||
| 21 | { |
||||
| 22 | use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 23 | |||||
| 24 | public $mongodb_connection; |
||||
| 25 | public $collection; |
||||
| 26 | public $items; |
||||
| 27 | public $userId; |
||||
| 28 | public $params; |
||||
| 29 | public $type; |
||||
| 30 | |||||
| 31 | /** |
||||
| 32 | * Create a new event instance. |
||||
| 33 | * |
||||
| 34 | * @return void |
||||
| 35 | * @param mixed $collection |
||||
| 36 | * @param mixed $items |
||||
| 37 | * @param mixed $userId |
||||
| 38 | * @param mixed $params |
||||
| 39 | * @param mixed $type |
||||
| 40 | */ |
||||
| 41 | public function __construct($collection, $items, $userId, $params, $type = 'insert') |
||||
| 42 | { |
||||
| 43 | $this->queue = 'analytics'; |
||||
| 44 | |||||
| 45 | $this->mongodb_connection = config('apanalytics.db_connection', 'mongodb'); |
||||
| 46 | $this->collection = $collection; |
||||
| 47 | $this->items = $items; |
||||
| 48 | $this->userId = $userId; |
||||
| 49 | $this->params = $params; |
||||
| 50 | $this->type = $type; |
||||
| 51 | } |
||||
| 52 | |||||
| 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) |
||||
|
0 ignored issues
–
show
The method
collection() does not exist on Illuminate\Database\ConnectionInterface. It seems like you code against a sub-type of Illuminate\Database\ConnectionInterface such as Jenssegers\Mongodb\Connection.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 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 | } |
||||
| 143 | } |
||||
| 144 | } |
||||
| 145 | |||||
| 146 | private function prepEventData($postEvent, $items, $userId, $params, $collection) |
||||
| 147 | { |
||||
| 148 | if ($postEvent) { |
||||
| 149 | return []; |
||||
| 150 | } |
||||
| 151 | |||||
| 152 | if (is_array($items) && $collection != 'visits') { |
||||
| 153 | return $items; |
||||
| 154 | } |
||||
| 155 | |||||
| 156 | return $this->addExtraEventData($items, $userId, $params); |
||||
| 157 | } |
||||
| 158 | |||||
| 159 | private function formatItems($items) |
||||
| 160 | { |
||||
| 161 | $formattedItems = $items; |
||||
| 162 | |||||
| 163 | if (is_array($formattedItems)) { |
||||
| 164 | return $formattedItems; |
||||
| 165 | } |
||||
| 166 | |||||
| 167 | if ($items instanceof Paginator || $items instanceof LengthAwarePaginator) { |
||||
| 168 | $formattedItems = $items->items(); |
||||
| 169 | } |
||||
| 170 | |||||
| 171 | return Arr::wrap($formattedItems); |
||||
| 172 | } |
||||
| 173 | |||||
| 174 | private function addExtraEventData($data, $userId, $params) |
||||
| 175 | { |
||||
| 176 | // Merge our extra parameters |
||||
| 177 | if (is_array($params) && count($params)) { |
||||
| 178 | $data = array_merge($data, $params); |
||||
| 179 | } |
||||
| 180 | |||||
| 181 | // Standard stuff |
||||
| 182 | $data = array_merge($data, [ |
||||
| 183 | 'user_id' => $userId ?? auth()->id() ?? null, |
||||
| 184 | ]); |
||||
| 185 | |||||
| 186 | data_fill($data, 'created_at', mongoTime()); |
||||
| 187 | |||||
| 188 | return $data; |
||||
| 189 | } |
||||
| 190 | } |
||||
| 191 |