EloquentModelObserver::restored()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace App\Observers;
5
6
use App\User;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Facades\Session;
9
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)
50
    {
51
        Session::flash(
52
            self::FLASH_MESSAGE_KEY,
53
            __("The " . $this->getModelName($model) . " has been updated")
54
        );
55
    }
56
57
    /**
58
     * Listen to the model deleted event.
59
     *
60
     * @param Model $model
61
     */
62
    public function deleted(Model $model)
63
    {
64
        Session::flash(
65
            self::FLASH_MESSAGE_KEY,
66
            __("The " . $this->getModelName($model) . " has been deleted")
67
        );
68
    }
69
70
    /**
71
     * Listen to the model restored event.
72
     *
73
     * @param Model $model
74
     */
75
    public function restored(Model $model)
76
    {
77
        Session::flash(
78
            self::FLASH_MESSAGE_KEY,
79
            __("The " . $this->getModelName($model) . " has been restored")
80
        );
81
    }
82
83
}
84