Completed
Push — administration ( 67d1c3...18dbd2 )
by Fèvre
05:11 queued 02:35
created

PageController::index()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 65
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 9.3571
c 0
b 0
f 0
cc 2
eloc 37
nc 2
nop 0

How to fix   Long Method   

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
namespace Xetaravel\Http\Controllers\Admin;
3
4
use Carbon\Carbon;
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Cache;
7
use Xetaravel\Http\Components\AnalyticsComponent;
8
use Xetaravel\Models\Article;
9
use Xetaravel\Models\Comment;
10
use Xetaravel\Models\User;
11
12
class PageController extends Controller
13
{
14
    use AnalyticsComponent;
15
16
    /**
17
     * Constructor
18
     */
19
    public function __construct()
20
    {
21
        parent::__construct();
22
23
        $this->endDate = Carbon::now()->subDay();
24
    }
25
26
    /**
27
     * Show the application dashboard.
28
     *
29
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
30
     */
31
    public function index()
32
    {
33
        $minutes = config('analytics.cache_lifetime_in_minutes');
34
35
        if (config('analytics.enabled')) {
36
            // @codeCoverageIgnoreStart
37
            $visitorsData = Cache::remember('Analytics.visitors', $minutes, function () {
38
                return $this->buildVisitorsGraph();
39
            });
40
41
            $browsersData = Cache::remember('Analytics.browsers', $minutes, function () {
42
                return $this->buildBrowsersGraph();
43
            });
44
45
            $countriesData = Cache::remember('Analytics.countries', $minutes, function () {
46
                return $this->buildCountriesGraph();
47
            });
48
49
            $devicesGraph = Cache::remember('Analytics.devices', $minutes, function () {
50
                return $this->buildDevicesGraph();
51
            });
52
53
            $topCountries = array_slice($countriesData->rows, 0, 3);
54
55
            $operatingSystemGraph = Cache::remember('Analytics.operatingsystem', $minutes, function () {
56
                return $this->buildOperatingSytemGraph();
57
            });
58
59
            $todayVisitors = Cache::remember('Analytics.todayvisitors', $minutes, function () {
60
                return $this->buildTodayVisitors();
61
            });
62
            // @codeCoverageIgnoreEnd
63
        }
64
65
        $usersCount = Cache::remember('Analytics.users.count', $minutes, function () {
66
            return User::count();
67
        });
68
69
        $articlesCount = Cache::remember('Analytics.articles.count', $minutes, function () {
70
            return Article::count();
71
        });
72
73
        $commentsCount = Cache::remember('Analytics.comments.count', $minutes, function () {
74
            return Comment::count();
75
        });
76
77
        $breadcrumbs = $this->breadcrumbs;
0 ignored issues
show
Bug introduced by
The property breadcrumbs does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
78
79
        return view(
80
            'Admin::page.index',
81
            compact(
82
                'breadcrumbs',
83
                'visitorsData',
84
                'browsersData',
85
                'countriesData',
86
                'topCountries',
87
                'devicesGraph',
88
                'operatingSystemGraph',
89
                'usersCount',
90
                'articlesCount',
91
                'commentsCount',
92
                'todayVisitors'
93
            )
94
        );
95
    }
96
}
97