|
1
|
|
|
<?php |
|
2
|
|
|
namespace NwLaravel\ActivityLog; |
|
3
|
|
|
|
|
4
|
|
|
use NwLaravel\ActivityLog\Handlers\HandlerInterface; |
|
5
|
|
|
use Illuminate\Config\Repository as Config; |
|
6
|
|
|
use Illuminate\Auth\AuthManager; |
|
7
|
|
|
|
|
8
|
|
|
class ActivityManager |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var HandlerInterface |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $handler; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var \Illuminate\Auth\AuthManager |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $auth; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var \Illuminate\Config\Repository |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $config; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $user; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Create the logsupervisor using a default Handler |
|
32
|
|
|
* Also register Laravels Log Handler if needed. |
|
33
|
|
|
* |
|
34
|
|
|
* @param HandlerInterface $handler |
|
35
|
|
|
* @param AuthManager $auth |
|
36
|
|
|
* @param Config $config |
|
37
|
|
|
*/ |
|
38
|
4 |
|
public function __construct(HandlerInterface $handler, AuthManager $auth, Config $config) |
|
39
|
|
|
{ |
|
40
|
4 |
|
$this->handler = $handler; |
|
41
|
4 |
|
$this->config = $config; |
|
42
|
4 |
|
$this->auth = $auth; |
|
43
|
4 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Log activity |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $action |
|
49
|
|
|
* @param string $description |
|
50
|
|
|
* @param \Eloquent $model |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
2 |
|
public function log($action, $description, $model = null) |
|
55
|
|
|
{ |
|
56
|
2 |
|
return $this->handler->log($action, $description, $model, $this->getUser(), request()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Clean old log records. |
|
61
|
|
|
* |
|
62
|
|
|
* @return integer |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function cleanLog() |
|
65
|
|
|
{ |
|
66
|
1 |
|
return $this->handler->cleanLog($this->config->get('nwlaravel.activity.deleteOlderThanMonths')); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get User |
|
71
|
|
|
* |
|
72
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable |
|
73
|
|
|
*/ |
|
74
|
2 |
|
protected function getUser() |
|
75
|
|
|
{ |
|
76
|
2 |
|
if (!$this->user) { |
|
77
|
2 |
|
$authGuard = $this->config->get('nwlaravel.activity.auth_guard') ?: $this->auth->getDefaultDriver(); |
|
78
|
2 |
|
$this->user = $this->auth->guard($authGuard)->user(); |
|
79
|
2 |
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
return $this->user; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|