SupportsEvents::getEventDispatcher()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace ShiftOneLabs\LaravelDbEvents\Traits;
3
4
use Illuminate\Events\Dispatcher;
5
6
trait SupportsEvents
7
{
8
9
    /**
10
     * The event dispatcher instance.
11
     *
12
     * @var \Illuminate\Events\Dispatcher
13
     */
14
    protected $events;
15
16
    /**
17
     * Get the event dispatcher used by the object.
18
     *
19
     * @return \Illuminate\Events\Dispatcher
20
     */
21 36
    public function getEventDispatcher()
22
    {
23 36
        return $this->events;
24
    }
25
26
    /**
27
     * Set the event dispatcher instance on the object.
28
     *
29
     * In order to support both Laravel 4 and Laravel 5, the events
30
     * parameter is typehinted to the implementation and not the
31
     * interface, as the interface doesn't exist in Laravel 4.
32
     *
33
     * @param  \Illuminate\Events\Dispatcher  $events
34
     * @return void
35
     */
36 55
    public function setEventDispatcher(Dispatcher $events)
37
    {
38 55
        $this->events = $events;
39 55
    }
40
41
    /**
42
     * Unset the event dispatcher instance on the object.
43
     *
44
     * @return void
45
     */
46 12
    public function unsetEventDispatcher()
47
    {
48 12
        $this->events = null;
49 12
    }
50
51
    /**
52
     * Check if the object is using events.
53
     *
54
     * @return boolean
55
     */
56 47
    public function usingEvents()
57
    {
58 47
        return isset($this->events);
59
    }
60
61
    /**
62
     * Fire the given event for the object.
63
     *
64
     * @param  object  $event
65
     * @param  boolean  $halt
66
     * @return mixed
67
     */
68 39
    protected function fireEvent($event, $halt = false)
69
    {
70 39
        if (!$this->usingEvents()) {
71 8
            return true;
72
        }
73
74 31
        $method = $halt ? 'until' : 'fire';
75
76 31
        return $this->events->$method(get_class($event), $event);
77
    }
78
}
79