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

HasEvents::withEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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