Completed
Push — master ( d1f27a...3f2c58 )
by wen
02:24
created

AbstractEvent   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 26 2
A getContent() 0 7 1
A setAttribute() 0 8 3
A getAttribute() 0 4 1
1
<?php
2
3
4
namespace Sco\ActionLog\Events;
5
6
use Auth;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Arr;
9
10
abstract class AbstractEvent
11
{
12
    protected $attributes = [];
13
14
    protected $type;
15
16
    public function __construct(Model $model)
17
    {
18
        $this->setAttribute([
19
            'model.original'   => $model->getOriginal(),
20
            'model.attributes' => $model->getAttributes(),
21
            'model.table'      => $model->getTable(),
22
            'user_id'          => intval(Auth::id()),
23
            'client_ip'        => request()->getClientIp(),
24
            'type'             => $this->type,
25
        ]);
26
27
        if (class_exists('\Jenssegers\Agent\Agent')) {
28
            $agent = new \Jenssegers\Agent\Agent();
29
30
            $device = $agent->device();
31
            $platform = $agent->platform();
32
            $browser = $agent->browser();
33
34
            $this->setAttribute('client', [
35
                'device' =>   $device . ' ' . $agent->version($device),
36
                'platform' => $platform . ' ' . $agent->version($platform),
37
                '$browser' => $browser . ' ' . $agent->version($browser),
38
            ]);
39
        }
40
41
    }
42
43
    public function getContent()
44
    {
45
        return [
46
            'from' => $this->getAttribute('model.original'),
47
            'to'   => $this->getAttribute('model.attributes'),
48
        ];
49
    }
50
51
    /**
52
     * Set a given log value.
53
     *
54
     * @param array|string $key
55
     * @param mixed        $value
56
     */
57
    private function setAttribute($key, $value = null)
58
    {
59
        $keys = is_array($key) ? $key : [$key => $value];
60
61
        foreach ($keys as $key => $value) {
62
            Arr::set($this->attributes, $key, $value);
63
        }
64
    }
65
66
    /**
67
     * Get the specified log value.
68
     *
69
     * @param null|string $key
70
     * @param mixed       $default
71
     *
72
     * @return mixed
73
     */
74
    public function getAttribute($key = null, $default = null)
75
    {
76
        return Arr::get($this->attributes, $key, $default);
77
    }
78
}
79