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 | 15 | public function __construct(Auditable $model) |
|
23 | { |
||
24 | 15 | $this->model = $model; |
|
25 | } |
||
26 | |||
27 | /** |
||
28 | * Prepare the instance values for serialization. |
||
29 | * |
||
30 | * @return array<string,mixed> |
||
31 | */ |
||
32 | 11 | public function __serialize() |
|
33 | { |
||
34 | 11 | $values = [ |
|
35 | 11 | 'class' => get_class($this->model), |
|
36 | 11 | 'model_data' => [ |
|
37 | 11 | 'exists' => true, |
|
38 | 11 | 'connection' => $this->model->getQueueableConnection() |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
39 | 11 | ] |
|
40 | 11 | ]; |
|
41 | |||
42 | 11 | $customProperties = array_merge([ |
|
43 | 11 | 'attributes', |
|
44 | 11 | 'original', |
|
45 | 11 | 'excludedAttributes', |
|
46 | 11 | 'auditEvent', |
|
47 | 11 | 'auditExclude', |
|
48 | 11 | 'auditCustomOld', |
|
49 | 11 | 'auditCustomNew', |
|
50 | 11 | 'isCustomEvent', |
|
51 | 11 | 'preloadedResolverData', |
|
52 | 11 | ], $this->model->auditEventSerializedProperties ?? []); |
|
0 ignored issues
–
show
|
|||
53 | |||
54 | 11 | $reflection = new ReflectionClass($this->model); |
|
55 | |||
56 | 11 | foreach ($customProperties as $key) { |
|
57 | try { |
||
58 | 11 | $values['model_data'][$key] = $this->getModelPropertyValue($reflection, $key); |
|
59 | 11 | } catch (\Throwable $e) { |
|
60 | // |
||
61 | } |
||
62 | } |
||
63 | |||
64 | 11 | return $values; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * Restore the model after serialization. |
||
69 | * |
||
70 | * @param array<string,mixed> $values |
||
71 | */ |
||
72 | public function __unserialize(array $values): void |
||
73 | 11 | { |
|
74 | $model = new $values['class']; |
||
75 | 11 | ||
76 | if (! $model instanceof Auditable) { |
||
77 | 11 | return; |
|
78 | 11 | } |
|
79 | 11 | ||
80 | $this->model = $model; |
||
81 | $reflection = new ReflectionClass($this->model); |
||
82 | 11 | foreach ($values['model_data'] as $key => $value) { |
|
83 | $this->setModelPropertyValue($reflection, $key, $value); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | 11 | * Set the property value for the given property. |
|
89 | * |
||
90 | 11 | * @param ReflectionClass<Auditable> $reflection |
|
91 | * @param mixed $value |
||
92 | 11 | */ |
|
93 | protected function setModelPropertyValue(ReflectionClass $reflection, string $name, $value): void |
||
94 | 11 | { |
|
95 | $property = $reflection->getProperty($name); |
||
96 | |||
97 | $property->setAccessible(true); |
||
98 | |||
99 | $property->setValue($this->model, $value); |
||
100 | } |
||
101 | |||
102 | 11 | /** |
|
103 | * Get the property value for the given property. |
||
104 | 11 | * |
|
105 | * @param ReflectionClass<Auditable> $reflection |
||
106 | 11 | * @return mixed |
|
107 | */ |
||
108 | 11 | protected function getModelPropertyValue(ReflectionClass $reflection, string $name) |
|
109 | { |
||
110 | $property = $reflection->getProperty($name); |
||
111 | |||
112 | $property->setAccessible(true); |
||
113 | |||
114 | return $property->getValue($this->model); |
||
115 | } |
||
116 | } |
||
117 |