Completed
Push — master ( a52a56...4b8006 )
by Renato
09:08
created

ActivityManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 6
rs 9.4285
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
    public function __construct(HandlerInterface $handler, AuthManager $auth, Config $config)
39
    {
40
        $this->handler = $handler;
41
        $this->config = $config;
42
        $this->auth = $auth;
43
    }
44
45
    /**
46
     * Log activity
47
     *
48
     * @param string $action
49
     * @param string $description
50
     * @param \Eloquent  $model
51
     *
52
     * @return bool
53
     */
54
    public function log($action, $description, $model = null)
55
    {
56
        return $this->handler->log($action, $description, $model, $this->getUser(), request());
57
    }
58
59
    /**
60
     * Clean old log records.
61
     *
62
     * @return integer
63
     */
64
    public function cleanLog()
65
    {
66
        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
    protected function getUser()
75
    {
76
        if (!$this->user) {
77
            $authGuard = $this->config->get('nwlaravel.activity.auth_guard') ?: $this->auth->getDefaultDriver();
78
            $this->user = $this->auth->guard($authGuard)->user();
79
        }
80
81
        return $this->user;
82
    }
83
}
84