Passed
Pull Request — main (#5)
by Michael
03:57
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
use MichaelRubel\EnhancedPipeline\Events\PipePassed;
8
use MichaelRubel\EnhancedPipeline\Events\PipeStarted;
9
10
trait HasEvents
11
{
12
    /**
13
     * Determines whether pipeline uses events.
14
     *
15
     * @var bool
16
     */
17
    protected bool $useEvents = false;
18
19
    /**
20
     * Enable events in pipeline.
21
     *
22
     * @return static
23
     */
24 3
    public function withEvents(): static
25
    {
26 3
        $this->useEvents = true;
27
28 3
        return $this;
29
    }
30
31
    /**
32
     * Fire the started event if enabled.
33
     *
34
     * @param  mixed  $pipe
35
     * @param  mixed  $passable
36
     *
37
     * @return void
38
     */
39 24
    protected function fireStartedEvent($pipe, $passable): void
40
    {
41 24
        if (! $this->useEvents) {
42 21
            return;
43
        }
44
45 3
        event(new PipeStarted($pipe, $passable));
46
    }
47
48
    /**
49
     * Fire the passed event if enabled.
50
     *
51
     * @param  mixed  $pipe
52
     * @param  mixed  $passable
53
     *
54
     * @return void
55
     */
56 16
    protected function firePassedEvent($pipe, $passable): void
57
    {
58 16
        if (! $this->useEvents) {
59 14
            return;
60
        }
61
62 2
        event(new PipePassed($pipe, $passable));
63
    }
64
}
65