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