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

Factory::__callStatic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
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