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 ( b53e25...97d577 )
by Ash
02:58
created

Track::handle()   F

Complexity

Conditions 14
Paths 840

Size

Total Lines 56
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 36
nc 840
nop 0
dl 0
loc 56
rs 2.3222
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

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
     */
38
    public function __construct($collection, $items, $userId, $params, $type = 'insert')
39
    {
40
        $this->queue = 'analytics';
41
42
        $this->mongodb_connection = config('apanalytics.db_connection', 'mongodb');
43
        $this->collection         = $collection;
44
        $this->items              = $items;
45
        $this->userId             = $userId;
46
        $this->params             = $params;
47
        $this->type               = $type;
48
    }
49
50
    public function handle()
51
    {
52
        $connection = $this->mongodb_connection;
53
        $collection = $this->collection;
54
        $items      = $this->items;
55
        $userId     = $this->userId;
56
        $params     = $this->params;
57
        $type       = $this->type;
58
59
        $valid = ($items instanceof Collection) ? $items->count() : ($items instanceof Model) ? 1 : $type != 'update' ? count($items) : 1;
60
61
        if ($valid) {
62
            $collection = str_plural($collection);
63
            $items      = $type == 'update' ? $items : array_wrap(($items instanceof Paginator || $items instanceof LengthAwarePaginator) ? $items->items() : $items);
64
            $postEvent  = in_array($collection, config('apanalytics.format_collections'));
65
            $event      = $postEvent || $type == 'update' ? [] : $this->addExtraEventData($items, $userId, $params);
66
67
            try {
68
                if ($type == 'insert') {
69
                    if ($postEvent) {
70
                        foreach ($items as $item) {
71
                            $basename = strtolower(class_basename($item));
72
73
                            $data = [
74
                                $basename => [
75
                                    'id'   => $item->id ?? null,
76
                                    'type' => $item->type ?? null,
77
                                ],
78
                                'business' => [
79
                                    'id' => $item->business->id ?? null,
80
                                ],
81
                            ];
82
83
                            // Add Extra Stuff
84
                            $data = $this->addExtraEventData($data, $userId, $params);
85
86
                            event(new AnalyticTracked($collection, $basename, $data));
87
88
                            $event[] = $data;
89
                        }
90
                    }
91
92
                    return DB::connection($connection)
93
                        ->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

93
                        ->/** @scrutinizer ignore-call */ collection($collection)
Loading history...
94
                        ->insert($event);
95
                }
96
97
                // Type is update
98
                $basename = strtolower(str_singular($collection));
99
100
                return DB::connection($connection)
101
                        ->collection($collection)
102
                        ->where("{$basename}_id", $items)
103
                        ->update($params);
104
            } catch (\Exception $e) {
105
                Log::error('Error Logging Event', ['error' => $e->getMessage()]);
106
            }
107
        }
108
    }
109
110
    private function addExtraEventData($data, $userId, $params)
111
    {
112
        // Merge our extra parameters
113
        if (is_array($params) && count($params)) {
114
            $data = array_merge($data, $params);
115
        }
116
117
        // Standard stuff
118
        $data = array_merge($data, [
119
            'user_id'    => $userId ?? auth()->id() ?? null,
120
            'updated_at' => mongoTime(),
121
            'created_at' => mongoTime(),
122
        ]);
123
124
        return $data;
125
    }
126
}
127