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

HasEvents::fireEvent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 3
nop 3
crap 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