Completed
Push — master ( 124f2b...1346f4 )
by Arnold
03:01
created

Event::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jasny\EventDispatcher;
6
7
use Psr\EventDispatcher\StoppableEventInterface;
8
9
/**
10
 * Event used by event dispatcher.
11
 */
12
class Event implements StoppableEventInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $name;
18
19
    /**
20
     * @var object|null
21
     */
22
    protected $subject;
23
24
    /**
25
     * @var mixed
26
     */
27
    protected $payload;
28
29
    /**
30
     * @var bool
31
     */
32
    protected $propagationStopped = false;
33
34
    /**
35
     * Event constructor.
36
     *
37
     * @param string      $name
38
     * @param object|null $subject
39
     * @param mixed       $payload
40
     */
41 4
    public function __construct(string $name, ?object $subject = null, $payload = null)
42
    {
43 4
        if (strpos($name, '*') !== false) {
44 1
            throw new \InvalidArgumentException("Invalid event name '$name': illegal character '*'");
45
        }
46
47 3
        $this->name = $name;
48 3
        $this->subject = $subject;
49 3
        $this->payload = $payload;
50 3
    }
51
52
53
    /**
54
     * Get the event name
55
     *
56
     * @return string
57
     */
58 1
    public function getName(): string
59
    {
60 1
        return $this->name;
61
    }
62
63
    /**
64
     * Get the event subject (typically an object)
65
     *
66
     * @return object|null
67
     */
68 1
    public function getSubject(): ?object
69
    {
70 1
        return $this->subject;
71
    }
72
73
    /**
74
     * Change the event payload.
75
     *
76
     * @param mixed $payload
77
     */
78 1
    public function setPayload($payload): void
79
    {
80 1
        $this->payload = $payload;
81 1
    }
82
83
    /**
84
     * Get the event payload.
85
     *
86
     * @return mixed
87
     */
88 2
    public function getPayload()
89
    {
90 2
        return $this->payload;
91
    }
92
93
94
    /**
95
     * Do call subsequent event listeners.
96
     */
97 1
    public function stopPropagation(): void
98
    {
99 1
        $this->propagationStopped = true;
100 1
    }
101
102
    /**
103
     * Is propagation stopped?
104
     *
105
     * @return bool
106
     */
107 1
    public function isPropagationStopped(): bool
108
    {
109 1
        return $this->propagationStopped;
110
    }
111
}
112