HasEvents::withEvents()   A
last analyzed

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
    protected bool $useEvents = false;
13
14
    /**
15
     * Enable events in pipeline.
16
     */
17 5
    public function withEvents(): static
18
    {
19 5
        $this->useEvents = true;
20
21 5
        return $this;
22
    }
23
24
    /**
25
     * Fire the event if enabled.
26
     *
27
     * @param  mixed  ...$params
28
     */
29 31
    protected function fireEvent(string $event, ...$params): void
30
    {
31 31
        if (! $this->useEvents) {
32 26
            return;
33
        }
34
35 5
        event(new $event(...$params));
36
    }
37
}
38