GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( d07373...b11103 )
by Ash
08:03
created

Track   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Importance

Changes 10
Bugs 3 Features 1
Metric Value
eloc 84
c 10
b 3
f 1
dl 0
loc 164
rs 10
wmc 27

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A formatItems() 0 13 4
A addExtraEventData() 0 14 3
A prepEventData() 0 11 4
F handle() 0 85 15
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
The trait Illuminate\Queue\SerializesModels requires some properties which are not provided by AshPowell\APAnalytics\Jobs\Track: $id, $class, $relations
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
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)
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

125
                        ->/** @scrutinizer ignore-call */ collection($collection)
Loading history...
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
            }
139
        }
140
    }
141
142
    private function prepEventData($postEvent, $items, $userId, $params, $collection)
143
    {
144
        if ($postEvent) {
145
            return [];
146
        }
147
148
        if (is_array($items) && $collection != 'visits') {
149
            return $items;
150
        }
151
152
        return $this->addExtraEventData($items, $userId, $params);
153
    }
154
155
    private function formatItems($items)
156
    {
157
        $formattedItems = $items;
158
159
        if (is_array($formattedItems)) {
160
            return $formattedItems;
161
        }
162
163
        if ($items instanceof Paginator || $items instanceof LengthAwarePaginator) {
164
            $formattedItems = $items->items();
165
        }
166
167
        return Arr::wrap($formattedItems);
168
    }
169
170
    private function addExtraEventData($data, $userId, $params)
171
    {
172
        // Merge our extra parameters
173
        if (is_array($params) && count($params)) {
174
            $data = array_merge($data, $params);
175
        }
176
177
        // Standard stuff
178
        $data = array_merge($data, [
179
            'user_id'    => $userId ?? auth()->id() ?? null,
180
            'created_at' => mongoTime(),
181
        ]);
182
183
        return $data;
184
    }
185
}
186