1 | <?php |
||
2 | |||
3 | namespace OwenIt\Auditing; |
||
4 | |||
5 | use DateTimeInterface; |
||
6 | use Illuminate\Support\Str; |
||
7 | use Illuminate\Support\Facades\Config; |
||
8 | use Illuminate\Database\Eloquent\Model; |
||
9 | use OwenIt\Auditing\Contracts\AttributeEncoder; |
||
10 | |||
11 | trait Audit |
||
12 | { |
||
13 | /** |
||
14 | * Audit data. |
||
15 | * |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $data = []; |
||
19 | |||
20 | /** |
||
21 | * The Audit attributes that belong to the metadata. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $metadata = []; |
||
26 | |||
27 | /** |
||
28 | * The Auditable attributes that were modified. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $modified = []; |
||
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | 12 | public function auditable() |
|
38 | { |
||
39 | 12 | return $this->morphTo(); |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | 16 | public function user() |
|
46 | { |
||
47 | 16 | return $this->morphTo(); |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | 49 | public function getConnectionName() |
|
54 | { |
||
55 | 49 | return Config::get('audit.drivers.database.connection'); |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | 49 | public function getTable(): string |
|
62 | { |
||
63 | 49 | return Config::get('audit.drivers.database.table', parent::getTable()); |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | 16 | public function resolveData(): array |
|
70 | { |
||
71 | 16 | $morphPrefix = Config::get('audit.user.morph_prefix', 'user'); |
|
72 | |||
73 | // Metadata |
||
74 | 16 | $this->data = [ |
|
75 | 16 | 'audit_id' => $this->id, |
|
76 | 16 | 'audit_event' => $this->event, |
|
77 | 16 | 'audit_url' => $this->url, |
|
78 | 16 | 'audit_ip_address' => $this->ip_address, |
|
79 | 16 | 'audit_user_agent' => $this->user_agent, |
|
80 | 16 | 'audit_tags' => $this->tags, |
|
81 | 16 | 'audit_created_at' => $this->serializeDate($this->created_at), |
|
82 | 16 | 'audit_updated_at' => $this->serializeDate($this->updated_at), |
|
83 | 16 | 'user_id' => $this->getAttribute($morphPrefix.'_id'), |
|
84 | 16 | 'user_type' => $this->getAttribute($morphPrefix.'_type'), |
|
85 | ]; |
||
86 | |||
87 | 16 | if ($this->user) { |
|
88 | 11 | foreach ($this->user->getArrayableAttributes() as $attribute => $value) { |
|
89 | 11 | $this->data['user_'.$attribute] = $value; |
|
90 | } |
||
91 | } |
||
92 | |||
93 | 16 | $this->metadata = array_keys($this->data); |
|
94 | |||
95 | // Modified Auditable attributes |
||
96 | 16 | foreach ($this->new_values as $key => $value) { |
|
97 | 13 | $this->data['new_'.$key] = $value; |
|
98 | } |
||
99 | |||
100 | 16 | foreach ($this->old_values as $key => $value) { |
|
101 | 3 | $this->data['old_'.$key] = $value; |
|
102 | } |
||
103 | |||
104 | 16 | $this->modified = array_diff_key(array_keys($this->data), $this->metadata); |
|
105 | |||
106 | 16 | return $this->data; |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * Get the formatted value of an Eloquent model. |
||
111 | * |
||
112 | * @param Model $model |
||
113 | * @param string $key |
||
114 | * @param mixed $value |
||
115 | * |
||
116 | * @return mixed |
||
117 | */ |
||
118 | 10 | protected function getFormattedValue(Model $model, string $key, $value) |
|
119 | { |
||
120 | // Apply defined get mutator |
||
121 | 10 | if ($model->hasGetMutator($key)) { |
|
122 | 9 | return $model->mutateAttribute($key, $value); |
|
123 | } |
||
124 | |||
125 | // Cast to native PHP type |
||
126 | 10 | if ($model->hasCast($key)) { |
|
127 | 6 | return $model->castAttribute($key, $value); |
|
128 | } |
||
129 | |||
130 | // Honour DateTime attribute |
||
131 | 10 | if ($value !== null && in_array($key, $model->getDates(), true)) { |
|
132 | 5 | return $model->asDateTime($value); |
|
133 | } |
||
134 | |||
135 | 10 | return $value; |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | 12 | public function getDataValue(string $key) |
|
142 | { |
||
143 | 12 | if (!array_key_exists($key, $this->data)) { |
|
144 | 1 | return; |
|
145 | } |
||
146 | |||
147 | 12 | $value = $this->data[$key]; |
|
148 | |||
149 | // User value |
||
150 | 12 | if ($this->user && Str::startsWith($key, 'user_')) { |
|
151 | 3 | return $this->getFormattedValue($this->user, substr($key, 5), $value); |
|
152 | } |
||
153 | |||
154 | // Auditable value |
||
155 | 12 | if ($this->auditable && Str::startsWith($key, ['new_', 'old_'])) { |
|
156 | 8 | $attribute = substr($key, 4); |
|
157 | |||
158 | 8 | return $this->getFormattedValue( |
|
159 | 8 | $this->auditable, |
|
160 | 8 | $attribute, |
|
161 | 8 | $this->decodeAttributeValue($this->auditable, $attribute, $value) |
|
162 | ); |
||
163 | } |
||
164 | |||
165 | 4 | return $value; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * Decode attribute value. |
||
170 | * |
||
171 | * @param Contracts\Auditable $auditable |
||
172 | * @param string $attribute |
||
173 | * @param mixed $value |
||
174 | * |
||
175 | * @return mixed |
||
176 | */ |
||
177 | 8 | protected function decodeAttributeValue(Contracts\Auditable $auditable, string $attribute, $value) |
|
178 | { |
||
179 | 8 | $attributeModifiers = $auditable->getAttributeModifiers(); |
|
180 | |||
181 | 8 | if (!array_key_exists($attribute, $attributeModifiers)) { |
|
182 | 8 | return $value; |
|
183 | } |
||
184 | |||
185 | 1 | $attributeDecoder = $attributeModifiers[$attribute]; |
|
186 | |||
187 | 1 | if (is_subclass_of($attributeDecoder, AttributeEncoder::class)) { |
|
188 | 1 | return call_user_func([$attributeDecoder, 'decode'], $value); |
|
189 | } |
||
190 | |||
191 | 1 | return $value; |
|
192 | } |
||
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | 4 | public function getMetadata(bool $json = false, int $options = 0, int $depth = 512) |
|
198 | { |
||
199 | 4 | if (empty($this->data)) { |
|
200 | 4 | $this->resolveData(); |
|
201 | } |
||
202 | |||
203 | 4 | $metadata = []; |
|
204 | |||
205 | 4 | foreach ($this->metadata as $key) { |
|
206 | 4 | $value = $this->getDataValue($key); |
|
207 | |||
208 | 4 | $metadata[$key] = $value instanceof DateTimeInterface |
|
209 | 2 | ? $this->serializeDate($value) |
|
210 | 4 | : $value; |
|
211 | } |
||
212 | |||
213 | 4 | return $json ? json_encode($metadata, $options, $depth) : $metadata; |
|
214 | } |
||
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | 9 | public function getModified(bool $json = false, int $options = 0, int $depth = 512) |
|
220 | { |
||
221 | 9 | if (empty($this->data)) { |
|
222 | 9 | $this->resolveData(); |
|
223 | } |
||
224 | |||
225 | 9 | $modified = []; |
|
226 | |||
227 | 9 | foreach ($this->modified as $key) { |
|
228 | 7 | $attribute = substr($key, 4); |
|
229 | 7 | $state = substr($key, 0, 3); |
|
230 | |||
231 | 7 | $value = $this->getDataValue($key); |
|
232 | |||
233 | 7 | $modified[$attribute][$state] = $value instanceof DateTimeInterface |
|
234 | 2 | ? $this->serializeDate($value) |
|
235 | 7 | : $value; |
|
236 | } |
||
237 | |||
238 | 9 | return $json ? json_encode($modified, $options, $depth) : $modified; |
|
239 | } |
||
240 | |||
241 | /** |
||
242 | * Get the Audit tags as an array. |
||
243 | * |
||
244 | * @return array |
||
245 | */ |
||
246 | 2 | public function getTags(): array |
|
247 | { |
||
248 | 2 | return preg_split('/,/', $this->tags, null, PREG_SPLIT_NO_EMPTY); |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
249 | } |
||
250 | } |
||
251 |