Passed
Push — master ( ee800c...4c8938 )
by Andrea Marco
03:01 queued 17s
created

InteractsWithEvents   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 32
ccs 8
cts 8
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A listensTo() 0 7 2
A stopsListeningTo() 0 7 1
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