HasEvents   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 52.94%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 0
cbo 0
dl 0
loc 60
ccs 9
cts 17
cp 0.5294
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A registerEvent() 0 8 2
A fireEvent() 0 13 3
A getEventDispatcher() 0 4 1
A setEventDispatcher() 0 4 1
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