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 ( b722ec...446e01 )
by Ash
03:53
created

APAnalytics::toModels()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 13
c 1
b 0
f 0
nc 10
nop 2
dl 0
loc 26
rs 8.8333
1
<?php
2
3
namespace AshPowell\APAnalytics;
4
5
use AshPowell\APAnalytics\Jobs\Track;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Str;
8
use InvalidArgumentException;
9
10
class APAnalytics
11
{
12
    protected $connection;
13
    protected $namespace;
14
15
    /**
16
     * Instantiate a new instance.
17
     *
18
     * @return void
19
     */
20
    public function __construct()
21
    {
22
        $this->connection = config('apanalytic.db_connection', 'mongodb');
23
        $this->namespace  = config('apanalytic.namespace', '\App\\');
24
    }
25
26
    /**
27
     * Track the Analytic.
28
     *
29
     * @return void
30
     * @param  mixed      $collection
31
     * @param  mixed      $items
32
     * @param  null|mixed $userId
33
     * @param  mixed      $params
34
     */
35
    public function track($collection, $items, $userId = null, $params = [])
36
    {
37
        Track::dispatch($collection, $items, $userId, $params);
38
39
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type void.
Loading history...
40
    }
41
42
    public function update($collection, $item, $params)
43
    {
44
        Track::dispatch($collection, $item, null, $params, 'update');
45
46
        return true;
47
    }
48
49
    /**
50
     * Get the Analytics.
51
     *
52
     * @param mixed      $collection
53
     * @param null|mixed $timeframe
54
     * @param null|mixed $filters
55
     * @param mixed      $interval
56
     * @param mixed      $groupBy
57
     */
58
    public function show($collection, $interval = 'count', $timeframe = null, $filters = null, $groupBy = null)
59
    {
60
        $start          = $timeframe ? Arr::get($timeframe, 'start') : null;
61
        $end            = $timeframe ? Arr::get($timeframe, 'end') : null;
62
        $matchArray     = [];
63
        $filters        = valid_json($filters) ? json_decode($filters) : $filters;
64
        $intervalFormat = '%Y-%m-%dT%H';
65
        $aggregate      = [];
66
        $model          = $this->namespace . Str::studly(Str::singular($collection)) . 'Analytic';
67
68
        if (! class_exists($model)) {
69
            throw new InvalidArgumentException("Model {$model} does not exist.");
70
        }
71
72
        if ($filters) {
73
            foreach ($filters as $filter) {
74
                if (is_array($filter)) {
75
                    $matchArray = array_merge($matchArray, $filter);
76
                } else {
77
                    $propertyValue = $filter->property_value;
78
79
                    if (is_numeric($propertyValue)) {
80
                        $propertyValue = (int) $propertyValue;
81
                    }
82
83
                    $matchArray = array_merge($matchArray, [$filter->property_name => $propertyValue]);
84
                }
85
            }
86
        }
87
88
        abort_unless(auth()->user()->can('view', [(new $model), $matchArray]), 403, 'You dont have permission to view these analytics');
89
90
        if ($start) {
91
            $matchArray['created_at']['$gte'] = mongoTime($start);
92
        }
93
94
        if ($end) {
95
            $matchArray['created_at']['$lt'] = mongoTime($end);
96
        }
97
98
        if ($matchArray) {
99
            $aggregate[] = ['$match' => $matchArray];
100
        }
101
102
        if ($interval != 'count') {
103
            if ($interval == 'daily') {
104
                $intervalFormat = '%Y-%m-%d';
105
            }
106
107
            if ($interval == 'weekly') {
108
                $intervalFormat = '%Y-%U';
109
            }
110
111
            if ($interval == 'monthly' || $interval == 'growth') {
112
                $intervalFormat = '%Y-%m';
113
            }
114
115
            $aggregate[] =  [
116
                '$group' => [
117
                    '_id' => [
118
                        '$dateToString' => ['date' => '$created_at', 'format' => $intervalFormat],
119
                    ],
120
                    'count' => [
121
                        '$sum' => 1,
122
                    ],
123
                    'created_at' => [
124
                        '$last' => '$created_at',
125
                    ],
126
                ],
127
            ];
128
129
            $aggregate[] = ['$sort' => ['created_at' => 1]];
130
131
            $aggregate[] = [
132
                '$project' => [
133
                    '_id'        => 0,
134
                    'created_at' => 1,
135
                    'count'      => 1,
136
                ],
137
            ];
138
        }
139
140
        if ($interval == 'count' && $groupBy != null) {
141
            $nested = Str::contains($groupBy, '.');
142
143
            if ($nested) {
144
                $aggregate[] = ['$unwind' => '$' . Str::before($groupBy, '.')];
145
            }
146
147
            $aggregate[] =  [
148
                '$group' => [
149
                    '_id'   => '$' . $groupBy,
150
                    'count' => [
151
                        '$sum' => 1,
152
                    ],
153
                ],
154
            ];
155
156
            $aggregate[] = ['$sort' => ['count' => 1]];
157
158
            $aggregate[] = [
159
                '$project' => [
160
                    '_id'   => 0,
161
                    Str::after($groupBy, '.')  => '$_id',
162
                    'count' => 1,
163
                ],
164
            ];
165
        }
166
167
        $data = $model::raw(function ($collection) use ($matchArray, $interval, $aggregate, $groupBy) {
168
            if ($interval == 'count' && ! $groupBy) {
169
                return $collection->count($matchArray);
170
            }
171
172
            if ($aggregate) {
173
                return $collection->aggregate($aggregate, ['allowDiskUse' => true]);
174
            }
175
        });
176
177
        return $data;
178
    }
179
}
180