Issues (276)

src/EventManager/HasEvents.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Nip\Records\EventManager;
4
5
use Nip\Records\AbstractModels\Record;
6
use Nip\Records\EventManager\Events\Event;
7
use Nip\Records\EventManager\Events\Observe;
8
9
/**
10
 * Trait HasEvents
11
 * @package Nip\Records\Events
12
 */
13
trait HasEvents
14
{
15
    use HasObservables;
16
17
    /**
18
     * Register a retrieved model event with the dispatcher.
19
     *
20
     * @param \Closure|string $callback
21
     * @return void
22
     */
23
    public static function retrieved($callback)
24
    {
25
        static::registerModelEvent('retrieved', $callback);
26
    }
27
28
    /**
29
     * Register a saving model event with the dispatcher.
30
     *
31
     * @param \Closure|string $callback
32
     * @return void
33
     */
34
    public static function saving($callback)
35
    {
36
        static::registerModelEvent('saving', $callback);
37
    }
38
39
    /**
40
     * Register a saved model event with the dispatcher.
41
     *
42
     * @param \Closure|string $callback
43
     * @return void
44
     */
45
    public static function saved($callback)
46
    {
47
        static::registerModelEvent('saved', $callback);
48
    }
49
50
    /**
51
     * Register an updating model event with the dispatcher.
52
     *
53
     * @param \Closure|string $callback
54
     * @return void
55
     */
56
    public static function updating($callback)
57
    {
58
        static::registerModelEvent('updating', $callback);
59
    }
60
61
    /**
62
     * Register an updated model event with the dispatcher.
63
     *
64
     * @param \Closure|string $callback
65
     * @return void
66
     */
67
    public static function updated($callback)
68
    {
69
        static::registerModelEvent('updated', $callback);
70
    }
71
72
    /**
73
     * Register a creating model event with the dispatcher.
74
     *
75
     * @param \Closure|string $callback
76
     * @return void
77
     */
78 3
    public static function creating($callback)
79
    {
80 3
        static::registerModelEvent(Observe::CREATING, $callback);
81 3
    }
82
83
    /**
84
     * Register a created model event with the dispatcher.
85
     *
86
     * @param \Closure|string $callback
87
     * @return void
88
     */
89 1
    public static function created($callback)
90
    {
91 1
        static::registerModelEvent(Observe::CREATED, $callback);
92 1
    }
93
94
    /**
95
     * Register a replicating model event with the dispatcher.
96
     *
97
     * @param \Closure|string $callback
98
     * @return void
99
     */
100
    public static function replicating($callback)
101
    {
102
        static::registerModelEvent('replicating', $callback);
103
    }
104
105
    /**
106
     * Register a deleting model event with the dispatcher.
107
     *
108
     * @param \Closure|string $callback
109
     * @return void
110
     */
111
    public static function deleting($callback)
112
    {
113
        static::registerModelEvent('deleting', $callback);
114
    }
115
116
    /**
117
     * Register a deleted model event with the dispatcher.
118
     *
119
     * @param \Closure|string $callback
120
     * @return void
121
     */
122
    public static function deleted($callback)
123
    {
124
        static::registerModelEvent('deleted', $callback);
125
    }
126
127
    /**
128
     * @param $event
129
     * @param $callback
130
     */
131 3
    protected static function registerModelEvent($event, $callback)
132
    {
133 3
        static::eventManager()->registerModelEvent($event, static::class, $callback);
0 ignored issues
show
static::class of type string is incompatible with the type Nip\Records\AbstractMode...\EventManager\HasEvents expected by parameter $manager of Nip\Records\EventManager...r::registerModelEvent(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

133
        static::eventManager()->registerModelEvent($event, /** @scrutinizer ignore-type */ static::class, $callback);
Loading history...
134 3
    }
135
136
    /**
137
     * Fire the given event for the model.
138
     *
139
     * @param string $event
140
     * @param Record $record
141
     * @return mixed
142
     */
143 1
    protected function fireModelEvent($event, Record $record)
144
    {
145 1
        $event = Event::create($event, $this)->withRecord($record);
146 1
        return static::eventManager()->dispatch($event);
147
    }
148
149
    /**
150
     * @return EventManager
151
     */
152 3
    protected static function eventManager()
153
    {
154 3
        return EventManager::instance();
155
    }
156
}
157