|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AshPowell\APAnalytics\Listeners; |
|
4
|
|
|
|
|
5
|
|
|
use AshPowell\APAnalytics\Events\TrackAnalytic; |
|
6
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
|
7
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
|
8
|
|
|
|
|
9
|
|
|
use Illuminate\Bus\Queueable; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
11
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
12
|
|
|
use Illuminate\Foundation\Bus\Dispatchable; |
|
13
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
|
14
|
|
|
use Illuminate\Pagination\Paginator; |
|
15
|
|
|
use Illuminate\Queue\SerializesModels; |
|
16
|
|
|
use Illuminate\Support\Facades\DB; |
|
17
|
|
|
use Log; |
|
18
|
|
|
|
|
19
|
|
|
class TrackAnalyticListener implements ShouldQueue |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* The name of the queue the job should be sent to. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string|null |
|
25
|
|
|
*/ |
|
26
|
|
|
public $queue = 'analytics'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Create the event listener. |
|
30
|
|
|
* |
|
31
|
|
|
* @return void |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct() |
|
34
|
|
|
{ |
|
35
|
|
|
// |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Handle the event. |
|
40
|
|
|
* |
|
41
|
|
|
* @param TrackAnalytic $event |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
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
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function addExtraEventData($data, $userId, $params) |
|
92
|
|
|
{ |
|
93
|
|
|
// Merge our extra parameters |
|
94
|
|
|
if (is_array($params) && count($params)) { |
|
95
|
|
|
$data = array_merge($data, $params); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// Standard stuff |
|
99
|
|
|
$data = array_merge($data, [ |
|
100
|
|
|
'user_id' => $userId ?? auth()->id() ?? null, |
|
101
|
|
|
'updated_at' => mongoTime(), |
|
102
|
|
|
'created_at' => mongoTime(), |
|
103
|
|
|
]); |
|
104
|
|
|
|
|
105
|
|
|
return $data; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|