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
|
|
|
|