Passed
Push — main ( b7653b...a2f1c7 )
by Michael
09:49 queued 06:09
created

HasEvents   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 4
eloc 9
c 4
b 1
f 1
dl 0
loc 42
ccs 9
cts 9
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A withEvents() 0 5 1
A fireEvent() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\EnhancedPipeline\Traits;
6
7
trait HasEvents
8
{
9
    /**
10
     * Determines whether pipeline uses events.
11
     *
12
     * @var bool
13
     */
14
    protected bool $useEvents = false;
15
16
    /**
17
     * Enable events in pipeline.
18
     *
19
     * @return static
20
     */
21 3
    public function withEvents(): static
22
    {
23 3
        $this->useEvents = true;
24
25 3
        return $this;
26
    }
27
28
    /**
29
     * Fire the event if enabled.
30
     *
31
     * @param  string  $event
32
     * @param  string|callable|mixed  $pipe
33
     * @param  mixed  $passable
34
     *
35
     * @return void
36
     */
37 25
    protected function fireEvent(string $event, $pipe, $passable): void
38
    {
39 25
        if (! $this->useEvents) {
40 22
            return;
41
        }
42
43 3
        if (is_object($pipe)) {
44
            /** @var object $pipe */
45 2
            $pipe = $pipe::class;
46
        }
47
48 3
        event(new $event($pipe, $passable));
49
    }
50
}
51