AbstractViewComposer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 97
ccs 0
cts 14
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCachedVisitors() 0 6 1
A getCachedVisits() 0 6 1
A cacheResults() 0 4 1
A getVisitorsFilteredByDateRange() 0 6 1
A getVisitsFilteredByDateRange() 0 6 1
1
<?php namespace Arcanesoft\Tracker\ViewComposers;
2
3
use Arcanesoft\Tracker\Models;
4
use Arcanesoft\Tracker\Models\Visitor;
5
use Arcanesoft\Tracker\Models\VisitorActivity;
6
use Carbon\Carbon;
7
use Closure;
8
use Illuminate\Support\Facades\Cache;
9
10
/**
11
 * Class     AbstractViewComposer
12
 *
13
 * @package  Arcanesoft\Tracker\ViewComposers
14
 * @author   ARCANEDEV <[email protected]>
15
 */
16
abstract class AbstractViewComposer
17
{
18
    /* -----------------------------------------------------------------
19
     |  Properties
20
     | -----------------------------------------------------------------
21
     */
22
23
    /**
24
     * The View instance.
25
     *
26
     * @var \Illuminate\Contracts\View\View
27
     */
28
    protected $view;
29
30
    /**
31
     * Caching time.
32
     *
33
     * @var int
34
     */
35
    protected $minutes = 5;
36
37
    /* -----------------------------------------------------------------
38
     |  Main Methods
39
     | -----------------------------------------------------------------
40
     */
41
42
    /**
43
     * Get the cached visitors.
44
     *
45
     * @return \Illuminate\Database\Eloquent\Collection
46
     */
47
    protected function getCachedVisitors()
48
    {
49
        return $this->cacheResults('visitors', function () {
50
            return Models\Visitor::with(['user', 'device', 'language', 'agent', 'cookie', 'referer', 'geoip'])->get();
51
        });
52
    }
53
54
    /**
55
     * Get the cached visits.
56
     *
57
     * @return \Illuminate\Database\Eloquent\Collection
58
     */
59
    protected function getCachedVisits()
60
    {
61
        return $this->cacheResults('visits', function () {
62
            return Models\VisitorActivity::all();
63
        });
64
    }
65
66
    /* ------------------------------------------------------------------------------------------------
67
     |  Other Functions
68
     | ------------------------------------------------------------------------------------------------
69
     */
70
    /**
71
     * Cache the results.
72
     *
73
     * @param  string    $name
74
     * @param  \Closure  $callback
75
     *
76
     * @return mixed
77
     */
78
    protected function cacheResults($name, Closure $callback)
79
    {
80
        return Cache::remember("tracker::{$name}", $this->minutes, $callback);
81
    }
82
83
    /**
84
     * Get the filtered visitors by date range.
85
     *
86
     * @param  \Carbon\Carbon  $start
87
     * @param  \Carbon\Carbon  $end
88
     *
89
     * @return \Illuminate\Support\Collection
90
     */
91
    protected function getVisitorsFilteredByDateRange(Carbon $start, Carbon $end)
92
    {
93
        return $this->getCachedVisitors()->filter(function (Visitor $session) use ($start, $end) {
94
            return $session->updated_at->between($start, $end);
95
        });
96
    }
97
98
    /**
99
     * Get the filtered visits by date range.
100
     *
101
     * @param  \Carbon\Carbon  $start
102
     * @param  \Carbon\Carbon  $end
103
     *
104
     * @return \Illuminate\Support\Collection
105
     */
106
    protected function getVisitsFilteredByDateRange(Carbon $start, Carbon $end)
107
    {
108
        return $this->getCachedVisits()->filter(function (VisitorActivity $visit) use ($start, $end) {
109
            return $visit->created_at->between($start, $end);
110
        });
111
    }
112
}
113