Completed
Push — master ( 4c8a68...e69ae5 )
by wen
01:54
created

AbstractEvent::getAttribute()   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
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'          => Auth::id(),
23
            'client_ip'        => request()->getClientIp(),
24
            'type'             => $this->type,
25
        ]);
26
    }
27
28
    public function getContent()
29
    {
30
        return [
31
            'from' => $this->getAttribute('model.original'),
32
            'to'   => $this->getAttribute('model.attributes'),
33
        ];
34
    }
35
36
    /**
37
     * Set a given log value.
38
     *
39
     * @param array|string $key
40
     * @param mixed        $value
41
     */
42
    private function setAttribute($key, $value = null)
43
    {
44
        $keys = is_array($key) ? $key : [$key => $value];
45
46
        foreach ($keys as $key => $value) {
47
            Arr::set($this->attributes, $key, $value);
48
        }
49
    }
50
51
    /**
52
     * Get the specified log value.
53
     *
54
     * @param null|string $key
55
     * @param mixed       $default
56
     *
57
     * @return mixed
58
     */
59
    public function getAttribute($key = null, $default = null)
60
    {
61
        return Arr::get($this->attributes, $key, $default);
62
    }
63
}
64