Completed
Push — master ( 3f7578...5307b5 )
by ARCANEDEV
04:28
created

AbstractViewComposer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 85
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0

4 Methods

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