Completed
Push — master ( 07981c...e259b1 )
by wen
25:43
created

HasEvents::getEventDispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
4
namespace Sco\Admin\Component\Concerns;
5
6
7
use Illuminate\Contracts\Events\Dispatcher;
8
9
trait HasEvents
10
{
11
    /**
12
     * Register a component event with the dispatcher.
13
     *
14
     * @param  string  $event
15
     * @param  \Closure|string  $callback
16
     * @return void
17
     */
18
    protected static function registerEvent($event, $callback)
19
    {
20
        if (isset(static::$dispatcher)) {
21
            $name = static::class;
22
23
            static::$dispatcher->listen("admin.component.{$event}: {$name}", $callback);
24
        }
25
    }
26
27
    protected function fireEvent($event, $halt = true)
28
    {
29
        if (!isset(static::$dispatcher)) {
30
            return true;
31
        }
32
33
        $method = $halt ? 'until' : 'fire';
34
        return static::$dispatcher->{$method}(
35
            "admin.component.{$event}: ".static::class, $this
36
        );
37
    }
38
39
    /**
40
     * Get the event dispatcher instance.
41
     *
42
     * @return \Illuminate\Contracts\Events\Dispatcher
43
     */
44
    public static function getEventDispatcher()
45
    {
46
        return static::$dispatcher;
47
    }
48
49
    /**
50
     * Set the event dispatcher instance.
51
     *
52
     * @param  \Illuminate\Contracts\Events\Dispatcher  $dispatcher
53
     * @return void
54
     */
55
    public static function setEventDispatcher(Dispatcher $dispatcher)
56
    {
57
        static::$dispatcher = $dispatcher;
58
    }
59
}
60