Completed
Push — master ( 0df42c...ab08ca )
by Iman
10:11
created

BasicEventManager   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 35
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A wrapForIgnorance() 0 5 2
A startGuarding() 0 3 1
1
<?php
2
3
namespace Imanghafoori\HeyMan\WatchingStrategies;
4
5
use Illuminate\Support\Facades\Event;
6
7
class BasicEventManager
8
{
9
    private $events;
10
11
    /**
12
     * BasicEventManager constructor.
13
     *
14
     * @param $events
15
     *
16
     * @return BasicEventManager
17
     */
18 6
    public function init(array $events): BasicEventManager
19
    {
20 6
        $this->events = $events;
21
22 6
        return $this;
23
    }
24
25
    /**
26
     * @param $listener
27
     */
28 6
    public function startGuarding(callable $listener)
29
    {
30
        Event::listen($this->events, $this->wrapForIgnorance($listener));
31 6
    }
32 5
33
    /**
34 6
     * @param callable $callback
35 6
     * @return \Closure
36 6
     */
37 6
    private function wrapForIgnorance(callable $callback): \Closure
38
    {
39
        return function () use ($callback) {
40
            if (! config('heyman_ignore_event', false)) {
41
                $callback();
42
            }
43
        };
44
    }
45
}
46