Completed
Push — master ( b6a028...d1f27a )
by wen
01:50
created

Factory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 2 Features 1
Metric Value
wmc 9
c 2
b 2
f 1
lcom 1
cbo 2
dl 0
loc 69
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A event() 0 19 3
A info() 0 19 4
A __call() 0 4 1
A __callStatic() 0 4 1
1
<?php
2
3
namespace Sco\ActionLog;
4
5
use Auth;
6
use Sco\ActionLog\Events\AbstractEvent;
7
use Sco\ActionLog\Models\ActionLogModel;
8
9
class Factory
10
{
11
    /**
12
     * Logging Action By Event
13
     *
14
     * @param \Sco\ActionLog\Events\AbstractEvent $event
15
     *
16
     * @return bool
17
     */
18
    public function event(AbstractEvent $event)
19
    {
20
        $userId = $event->getAttribute('user_id', 0);
21
        if (!$userId && !config('actionlog.guest')) {
22
            return false;
23
        }
24
25
        $log = new ActionLogModel();
26
27
        $log->user_id    = $userId;
28
        $log->type       = $event->getAttribute('type', '');
29
        $log->table_name = $event->getAttribute('model.table', '');
30
        $log->content    = json_encode($event->getContent());
31
        $log->client_ip  = $event->getAttribute('client_ip', '');
32
33
        $log->save();
34
35
        return true;
36
    }
37
38
    /**
39
     * Logging Action
40
     *
41
     * @param string       $type
42
     * @param string|array $content
43
     * @param string       $tableName
44
     *
45
     * @return bool
46
     */
47
    public function info($type, $content, $tableName = '')
48
    {
49
        $log = new ActionLogModel();
50
51
        $userId = Auth::id();
52
        if (!$userId && !config('actionlog.guest')) {
53
            return false;
54
        }
55
56
        $log->user_id    = $userId;
57
        $log->type       = $type;
58
        $log->table_name = $tableName;
59
        $log->content    = is_array($content) ? json_encode($content) : $content;
60
        $log->client_ip  = request()->getClientIp();
61
62
        $log->save();
63
64
        return true;
65
    }
66
67
    public function __call($method, $parameters)
68
    {
69
        return (new ActionLogModel)->$method(...$parameters);
70
    }
71
72
73
    public static function __callStatic($method, $parameters)
74
    {
75
        return (new static)->$method(...$parameters);
76
    }
77
}
78