InteractsWithEvents::listensTo()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Cerbero\OctaneTestbench\Concerns;
4
5
/**
6
 * The trait to interact with events.
7
 *
8
 */
9
trait InteractsWithEvents
10
{
11
    /**
12
     * Register listeners for the given event
13
     *
14
     * @param string $event
15
     * @param mixed ...$listeners
16
     * @return static
17
     */
18 4
    public function listensTo(string $event, mixed ...$listeners): static
19
    {
20 4
        foreach ($listeners as $listener) {
21 3
            $this->app->events->listen($event, $listener);
22
        }
23
24 4
        return $this;
25
    }
26
27
    /**
28
     * Exclude listeners for the given event
29
     *
30
     * @param string $event
31
     * @param mixed ...$listeners
32
     * @return static
33
     */
34 3
    public function stopsListeningTo(string $event, mixed ...$listeners): static
35
    {
36 3
        $this->app->events->forget($event);
37
38 3
        $listeners = array_diff($this->app->config['octane.listeners'][$event], $listeners);
39
40 3
        return $this->listensTo($event, ...$listeners);
41
    }
42
}
43