| Total Complexity | 6 |
| Total Lines | 70 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 10 | class EloquentModelObserver |
||
| 11 | { |
||
| 12 | |||
| 13 | const FLASH_MESSAGE_KEY = "message"; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Listen to the model created event. |
||
| 17 | * |
||
| 18 | * @param Model $model |
||
| 19 | */ |
||
| 20 | public function created(Model $model) |
||
| 21 | { |
||
| 22 | Session::flash( |
||
| 23 | self::FLASH_MESSAGE_KEY, |
||
| 24 | __("The new " . $this->getModelName($model) . " has been created") |
||
| 25 | ); |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Get a human readable string representation of the model class. |
||
| 30 | * |
||
| 31 | * @param Model $model |
||
| 32 | * @return string |
||
| 33 | */ |
||
| 34 | private function getModelName(Model $model) : string |
||
| 35 | { |
||
| 36 | switch (get_class($model)) { |
||
| 37 | case User::class: |
||
| 38 | return $model->role; |
||
| 39 | default: |
||
| 40 | return rtrim($model->getTable(), "s"); |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Listen to the model updated event. |
||
| 46 | * |
||
| 47 | * @param Model $model |
||
| 48 | */ |
||
| 49 | public function updated(Model $model) |
||
| 54 | ); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Listen to the model deleted event. |
||
| 59 | * |
||
| 60 | * @param Model $model |
||
| 61 | */ |
||
| 62 | public function deleted(Model $model) |
||
| 67 | ); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Listen to the model restored event. |
||
| 72 | * |
||
| 73 | * @param Model $model |
||
| 74 | */ |
||
| 75 | public function restored(Model $model) |
||
| 80 | ); |
||
| 81 | } |
||
| 84 |