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.

APAnalytics::show()   F
last analyzed

Complexity

Conditions 26
Paths 13832

Size

Total Lines 133
Code Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 2 Features 2
Metric Value
cc 26
eloc 74
c 10
b 2
f 2
nc 13832
nop 6
dl 0
loc 133
rs 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;
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, $distinct = 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
        if (! app()->runningInConsole()) {
0 ignored issues
show
introduced by
The method runningInConsole() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
        if (! app()->/** @scrutinizer ignore-call */ runningInConsole()) {
Loading history...
89
            abort_unless(auth()->check() && auth()->user()->can('view', [(new $model), $matchArray]), 403, 'You dont have permission to view these analytics');
90
        }
91
92
        if ($start) {
93
            $matchArray['created_at']['$gte'] = mongoTime($start);
94
        }
95
96
        if ($end) {
97
            $matchArray['created_at']['$lt'] = mongoTime($end);
98
        }
99
100
        if ($matchArray) {
101
            $aggregate[] = ['$match' => $matchArray];
102
        }
103
104
        if ($distinct) {
105
            $aggregate[] =  [
106
                '$group' => [
107
                    '_id'        => '$' . $distinct,
108
                    'created_at' => [
109
                        '$last' => '$created_at',
110
                    ],
111
                ],
112
            ];
113
        }
114
115
        if ($interval != 'count') {
116
            if ($interval == 'daily') {
117
                $intervalFormat = '%Y-%m-%d';
118
            }
119
120
            if ($interval == 'weekly') {
121
                $intervalFormat = '%Y-%U';
122
            }
123
124
            if ($interval == 'monthly' || $interval == 'growth') {
125
                $intervalFormat = '%Y-%m';
126
            }
127
128
            $aggregate[] =  [
129
                '$group' => [
130
                    '_id' => [
131
                        '$dateToString' => ['date' => '$created_at', 'format' => $intervalFormat],
132
                    ],
133
                    'count' => [
134
                        '$sum' => 1,
135
                    ],
136
                    'created_at' => [
137
                        '$last' => '$created_at',
138
                    ],
139
                ],
140
            ];
141
142
            $aggregate[] = ['$sort' => ['created_at' => 1]];
143
144
            $aggregate[] = [
145
                '$project' => [
146
                    '_id'        => 0,
147
                    'created_at' => 1,
148
                    'count'      => 1,
149
                ],
150
            ];
151
        }
152
153
        if ($interval == 'count' && $groupBy != null) {
154
            $nested = Str::contains($groupBy, '.');
155
156
            if ($nested) {
157
                $aggregate[] = ['$unwind' => '$'.Str::before($groupBy, '.')];
158
            }
159
160
            $aggregate[] =  [
161
                '$group' => [
162
                    '_id'   => '$'.$groupBy,
163
                    'count' => [
164
                        '$sum' => 1,
165
                    ],
166
                ],
167
            ];
168
169
            $aggregate[] = ['$sort' => ['count' => 1]];
170
171
            $aggregate[] = [
172
                '$project' => [
173
                    '_id'   => 0,
174
                    Str::after($groupBy, '.')  => '$_id',
175
                    'count' => 1,
176
                ],
177
            ];
178
        }
179
180
        $data = $model::raw(function ($collection) use ($matchArray, $interval, $aggregate, $groupBy) {
181
            if ($interval == 'count' && ! $groupBy) {
182
                return $collection->count($matchArray);
183
            }
184
185
            if ($aggregate) {
186
                return $collection->aggregate($aggregate, ['allowDiskUse' => true]);
187
            }
188
        });
189
190
        return $data;
191
    }
192
}
193