GoogleAnalyticsPanel   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 41
c 3
b 0
f 0
dl 0
loc 115
ccs 0
cts 40
cp 0
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A dateLabels() 0 5 1
A render() 0 21 3
A fakeData() 0 13 2
A abortMessage() 0 4 1
A dependencyInstalled() 0 5 1
A period() 0 10 2
1
<?php
2
3
namespace Terranet\Administrator\Dashboard\Panels;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Arr;
7
use Spatie\Analytics\AnalyticsServiceProvider;
0 ignored issues
show
Bug introduced by
The type Spatie\Analytics\AnalyticsServiceProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Spatie\Analytics\Period;
0 ignored issues
show
Bug introduced by
The type Spatie\Analytics\Period was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Terranet\Administrator\Architect;
10
use Terranet\Administrator\Dashboard\Panel;
11
use Terranet\Administrator\Traits\Stringify;
12
13
class GoogleAnalyticsPanel extends Panel
14
{
15
    use Stringify;
16
17
    /**
18
     * Widget contents.
19
     *
20
     * @return mixed string|View
21
     */
22
    public function render()
23
    {
24
        $period = $this->period();
25
26
        $dailyStats = $this->dependencyInstalled() && config('analytics.view_id')
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

26
        $dailyStats = $this->dependencyInstalled() && /** @scrutinizer ignore-call */ config('analytics.view_id')
Loading history...
27
            ? \Analytics::fetchTotalVisitorsAndPageViews($period)
0 ignored issues
show
Bug introduced by
The type Analytics was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
            : $dailyStats = $this->fakeData($period);
0 ignored issues
show
Unused Code introduced by
The assignment to $dailyStats is dead and can be removed.
Loading history...
29
30
        $visitors = $dailyStats->sum('visitors');
31
        $pageViews = $dailyStats->sum('pageViews');
32
        $maxVisitors = $dailyStats->max('visitors');
33
34
        $labels = $this->dateLabels($dailyStats);
35
36
        return view(Architect::template()->dashboard('google_analytics'))->with(compact(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

36
        return /** @scrutinizer ignore-call */ view(Architect::template()->dashboard('google_analytics'))->with(compact(
Loading history...
37
            'dailyStats',
38
            'labels',
39
            'visitors',
40
            'pageViews',
41
            'maxVisitors',
42
            'period'
43
        ));
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    protected function abortMessage()
50
    {
51
        return
52
            <<<'OUT'
53
<div class="panel">
54
    <div class="panel-heading">
55
        <h4 class="panel-title">Google Analytics.</h4>
56
    </div>
57
    <div class="panel-body">
58
        <p>
59
            Spatie Google Analytics module missing, install it by running:        
60
            <code>composer require spatie/laravel-analytics</code>.
61
            <br /><br />
62
            Then follow the <a href="https://github.com/spatie/laravel-analytics" target="_blank">Setup Instructions</a>.
63
        </p>
64
    </div>
65
</div>
66
OUT;
67
    }
68
69
    /**
70
     * @return bool
71
     */
72
    protected function dependencyInstalled()
73
    {
74
        return Arr::has(
75
            app()->getLoadedProviders(),
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

75
            /** @scrutinizer ignore-call */ 
76
            app()->getLoadedProviders(),
Loading history...
76
            AnalyticsServiceProvider::class
77
        );
78
    }
79
80
    /**
81
     * @param $dailyStats
82
     *
83
     * @return mixed
84
     */
85
    protected function dateLabels($dailyStats)
86
    {
87
        return $dailyStats->pluck('date')->map(function (Carbon $carbon) {
88
            return $carbon->formatLocalized('%a, %e %B %Y');
89
        })->toArray();
90
    }
91
92
    /**
93
     * @return Period
94
     */
95
    protected function period()
96
    {
97
        $end = Carbon::today();
98
        $start = Carbon::parse($end)->subMonthNoOverflow();
99
100
        return $this->dependencyInstalled()
101
            ? Period::create($start, $end)
102
            : (object) [
103
                'startDate' => $start,
104
                'endDate' => $end,
105
            ];
106
    }
107
108
    /**
109
     * Provide fake analytics data for demo purposes.
110
     *
111
     * @param $period
112
     *
113
     * @return \Illuminate\Support\Collection
114
     */
115
    protected function fakeData($period)
116
    {
117
        $data = collect([]);
118
119
        for ($date = Carbon::parse($period->startDate); $date->lte($period->endDate); $date->addDay()) {
120
            $data->push([
121
                'date' => Carbon::parse($date),
122
                'visitors' => rand(100, 1000),
123
                'pageViews' => rand(100, 1000),
124
            ]);
125
        }
126
127
        return $data;
128
    }
129
}
130