Lines 100.00% 2 / 2
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
StoppableEvent 100.00% 2 / 2 100.00% 2 / 2 2 100.00% 1 / 1
 stopPropagation 100.00% 1 / 1 100.00% 1 / 1 1
 isPropagationStopped 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2declare(strict_types=1);
3
4
5namespace Lucent\EventDispatcher;
6
7use Psr\EventDispatcher\StoppableEventInterface;
8
9/**
10 * Base class for events whose propagation may be halted.
11 *
12 * A listener may call {@see stopPropagation()} to prevent any remaining
13 * listeners from being invoked for the event. The dispatcher checks
14 * {@see isPropagationStopped()} after each listener.
15 *
16 * ```php
17 * class UserCreated extends StoppableEvent
18 * {
19 *     public function __construct(public readonly User $user) {}
20 * }
21 * ```
22 */
23abstract class StoppableEvent implements StoppableEventInterface
24{
25    /**
26     * Whether propagation has been stopped.
27     *
28     * @var bool
29     */
30    private bool $propagationStopped = false;
31
32    /**
33     * Stop further listeners from being invoked for this event.
34     *
35     * @return void
36     */
37    public function stopPropagation(): void
38    {
39        $this->propagationStopped = true;
40    }
41
42    /**
43     * Whether propagation has been stopped.
44     *
45     * @return bool True if no further listeners should be invoked
46     */
47    public function isPropagationStopped(): bool
48    {
49        return $this->propagationStopped;
50    }
51}