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 ( d6ae2f...7a06b5 )
by Ash
04:16
created

Track::formatItems()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
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\Facades\DB;
16
use Log;
17
18
class Track implements ShouldQueue
19
{
20
    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...
21
22
    public $mongodb_connection;
23
    public $collection;
24
    public $items;
25
    public $userId;
26
    public $params;
27
    public $type;
28
29
    /**
30
     * Create a new event instance.
31
     *
32
     * @return void
33
     * @param  mixed $collection
34
     * @param  mixed $items
35
     * @param  mixed $userId
36
     * @param  mixed $params
37
     * @param  mixed $type
38
     */
39
    public function __construct($collection, $items, $userId, $params, $type = 'insert')
40
    {
41
        $this->queue = 'analytics';
42
43
        $this->mongodb_connection = config('apanalytics.db_connection', 'mongodb');
44
        $this->collection         = $collection;
45
        $this->items              = $items;
46
        $this->userId             = $userId;
47
        $this->params             = $params;
48
        $this->type               = $type;
49
    }
50
51
    public function handle()
52
    {
53
        $connection = $this->mongodb_connection;
54
        $collection = $this->collection;
55
        $items      = $this->items;
56
        $userId     = $this->userId;
57
        $params     = $this->params;
58
        $type       = $this->type;
59
        $valid      = true;
60
61
        if ($type != 'update') {
62
            $valid = ($items instanceof Collection) ? $items->count() : ($items instanceof Model) ? 1 : count($items);
63
        }
64
65
        if ($valid) {
66
            $collection = str_plural($collection);
67
            $items      = $this->formatItems($items);
68
            $postEvent  = in_array($collection, config('apanalytics.format_collections'));
69
            $event      = $this->prepEventData($postEvent, $items, $userId, $params, $collection);
70
71
            try {
72
                if ($type == 'insert') {
73
                    if ($postEvent) {
74
                        foreach ($items as $item) {
75
                            $basename = strtolower(class_basename($item));
76
77
                            $data = [
78
                                $basename => [
79
                                    'id'   => $item->id ?? null,
80
                                    'type' => $item->type ?? null,
81
                                ],
82
                                'business' => [
83
                                    'id' => $item->business->id ?? null,
84
                                ],
85
                            ];
86
87
                            // Add Extra Stuff
88
                            $data = $this->addExtraEventData($data, $userId, $params);
89
90
                            event(new AnalyticTracked($collection, $basename, $data));
91
92
                            $event[] = $data;
93
                        }
94
                    }
95
96
                    return DB::connection($connection)
97
                        ->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

97
                        ->/** @scrutinizer ignore-call */ collection($collection)
Loading history...
98
                        ->insert($event);
99
                }
100
101
                // Type is update
102
                $basename = strtolower(str_singular($collection));
103
104
                return DB::connection($connection)
105
                        ->collection($collection)
106
                        ->where("{$basename}_id", $items)
107
                        ->update($params);
108
            } catch (\Exception $e) {
109
                Log::error('Error Logging Event', ['error' => $e->getMessage()]);
110
            }
111
        }
112
    }
113
114
    private function prepEventData($postEvent, $items, $userId, $params, $collection)
115
    {
116
        if ($postEvent) {
117
            return [];
118
        }
119
120
        if (is_array($items) && $collection != 'visits') {
121
            return $items;
122
        }
123
124
        return $this->addExtraEventData($items, $userId, $params);
125
    }
126
127
    private function formatItems($items)
128
    {
129
        $formattedItems = $items;
130
131
        if (is_array($formattedItems)) {
132
            return $formattedItems;
133
        }
134
135
        if ($items instanceof Paginator || $items instanceof LengthAwarePaginator) {
136
            $formattedItems = $items->items();
137
        }
138
139
        return array_wrap($formattedItems);
140
    }
141
142
    private function addExtraEventData($data, $userId, $params)
143
    {
144
        // Merge our extra parameters
145
        if (is_array($params) && count($params)) {
146
            $data = array_merge($data, $params);
147
        }
148
149
        // Standard stuff
150
        $data = array_merge($data, [
151
            'user_id'    => $userId ?? auth()->id() ?? null,
152
            'created_at' => mongoTime(),
153
        ]);
154
155
        return $data;
156
    }
157
}
158