Test Failed
Push — master ( 4afff4...f236e4 )
by
unknown
08:58
created

DispatchAudit   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
dl 0
loc 101
rs 10
c 1
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getModelPropertyValue() 0 7 1
A __unserialize() 0 10 2
A __construct() 0 3 1
A __serialize() 0 33 3
A setModelPropertyValue() 0 7 1
1
<?php
2
3
namespace OwenIt\Auditing\Events;
4
5
use OwenIt\Auditing\Contracts\Auditable;
6
use ReflectionClass;
7
8
class DispatchAudit
9
{
10
    /**
11
     * The Auditable model.
12
     *
13
     * @var Auditable
14
     */
15
    public $model;
16
17
    /**
18
     * Create a new DispatchAudit event instance.
19
     *
20
     * @param Auditable $model
21
     */
22
    public function __construct(Auditable $model)
23
    {
24
        $this->model = $model;
25
    }
26
27
    /**
28
     * Prepare the instance values for serialization.
29
     *
30
     * @return array
31
     */
32
    public function __serialize()
33
    {
34
        $values = [
35
            'class' => get_class($this->model),
36
            'model_data' => [
37
                'exists' => true,
38
                'connection' => $this->model->getQueueableConnection()
0 ignored issues
show
Bug introduced by
The method getQueueableConnection() does not exist on OwenIt\Auditing\Contracts\Auditable. Since it exists in all sub-types, consider adding an abstract or default implementation to OwenIt\Auditing\Contracts\Auditable. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
                'connection' => $this->model->/** @scrutinizer ignore-call */ getQueueableConnection()
Loading history...
39
            ]
40
        ];
41
42
        $customProperties = array_merge([
43
            'attributes',
44
            'original',
45
            'excludedAttributes',
46
            'auditEvent',
47
            'auditExclude',
48
            'auditCustomOld',
49
            'auditCustomNew',
50
            'isCustomEvent',
51
            'preloadedResolverData',
52
        ], $this->model->auditEventSerializedProperties ?? []);
0 ignored issues
show
Bug introduced by
Accessing auditEventSerializedProperties on the interface OwenIt\Auditing\Contracts\Auditable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
53
54
        $reflection = new ReflectionClass($this->model);
55
56
        foreach ($customProperties as $key) {
57
            try {
58
                $values['model_data'][$key] = $this->getModelPropertyValue($reflection, $key);
59
            } catch (\Throwable $e){
60
                //
61
            }
62
        }
63
64
        return $values;
65
    }
66
67
    /**
68
     * Restore the model after serialization.
69
     *
70
     * @param  array  $values
71
     * @return array
72
     */
73
    public function __unserialize(array $values)
74
    {
75
        $this->model = new $values['class'];
76
77
        $reflection = new ReflectionClass($this->model);
78
        foreach ($values['model_data'] as $key => $value) {
79
            $this->setModelPropertyValue($reflection, $key, $value);
80
        }
81
82
        return $values;
83
    }
84
85
    /**
86
     * Set the property value for the given property.
87
     */
88
    protected function setModelPropertyValue(ReflectionClass $reflection, string $name, $value)
89
    {
90
        $property = $reflection->getProperty($name);
91
92
        $property->setAccessible(true);
93
94
        $property->setValue($this->model, $value);
95
    }
96
97
    /**
98
     * Get the property value for the given property.
99
     *
100
     * @return mixed
101
     */
102
    protected function getModelPropertyValue(ReflectionClass $reflection, string $name)
103
    {
104
        $property = $reflection->getProperty($name);
105
106
        $property->setAccessible(true);
107
108
        return $property->getValue($this->model);
109
    }
110
}
111