HasEvents::getEventDispatcher()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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