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

AbstractEvent::__construct()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 26
rs 8.8571
c 1
b 0
f 0
cc 2
eloc 17
nc 2
nop 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