1 | <?php namespace Cviebrock\EloquentLoggable; |
||
8 | class LoggableObserver |
||
9 | { |
||
10 | |||
11 | /** |
||
12 | * @var mixed |
||
13 | */ |
||
14 | private $userId; |
||
15 | |||
16 | /** |
||
17 | * @var \Illuminate\Contracts\Events\Dispatcher |
||
18 | */ |
||
19 | private $events; |
||
20 | |||
21 | /** |
||
22 | * LoggableObserver constructor. |
||
23 | * |
||
24 | * @param \Illuminate\Foundation\Auth\User $user |
||
25 | * @param \Illuminate\Contracts\Events\Dispatcher $events |
||
26 | */ |
||
27 | public function __construct(Authenticatable $user, Dispatcher $events) |
||
32 | |||
33 | /** |
||
34 | * Log a created model. |
||
35 | * |
||
36 | * @param \Cviebrock\EloquentLoggable\Loggable|\Illuminate\Database\Eloquent\Model $model |
||
37 | * |
||
38 | * @return void |
||
39 | */ |
||
40 | public function created(Loggable $model): void |
||
46 | |||
47 | /** |
||
48 | * Log an updated model. |
||
49 | * |
||
50 | * @param \Cviebrock\EloquentLoggable\Loggable|\Illuminate\Database\Eloquent\Model $model |
||
51 | * |
||
52 | * @return void |
||
53 | */ |
||
54 | public function updated(Loggable $model): void |
||
67 | |||
68 | /** |
||
69 | * Log a deleted model. |
||
70 | * |
||
71 | * @param \Cviebrock\EloquentLoggable\Loggable|\Illuminate\Database\Eloquent\Model $model |
||
72 | * |
||
73 | * @return void |
||
74 | */ |
||
75 | public function deleted(Loggable $model): void |
||
81 | |||
82 | /** |
||
83 | * Log a restored model. |
||
84 | * |
||
85 | * @param \Cviebrock\EloquentLoggable\Loggable|\Illuminate\Database\Eloquent\Model $model |
||
86 | * |
||
87 | * @return void |
||
88 | */ |
||
89 | public function restored(Loggable $model): void |
||
95 | |||
96 | /** |
||
97 | * Build up the array of data to log. |
||
98 | * |
||
99 | * @param string $type |
||
100 | * @param \Cviebrock\EloquentLoggable\Loggable|\Illuminate\Database\Eloquent\Model $model |
||
101 | * |
||
102 | * @return array |
||
103 | */ |
||
104 | protected function buildDataToLog(string $type, Loggable $model): array |
||
117 | |||
118 | /** |
||
119 | * Return a "unique" hash to identify sets of changes. |
||
120 | * |
||
121 | * @param array $data |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | protected function generateChangeSet(array $data): string |
||
129 | |||
130 | /** |
||
131 | * Return an array of old and new values that should be logged. |
||
132 | * |
||
133 | * @param \Cviebrock\EloquentLoggable\Loggable|\Illuminate\Database\Eloquent\Model $model |
||
134 | * |
||
135 | * @return array |
||
136 | */ |
||
137 | private function getLoggableAttributes(Loggable $model): array |
||
180 | |||
181 | /** |
||
182 | * Get the timestamp attributes on the model that are normally not logged. |
||
183 | * |
||
184 | * @param \Cviebrock\EloquentLoggable\Loggable|\Illuminate\Database\Eloquent\Model $model |
||
185 | * |
||
186 | * @return array |
||
187 | */ |
||
188 | private function getTimestampAttributes(Loggable $model): array |
||
204 | |||
205 | } |
||
206 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.