Completed
Pull Request — master (#18)
by Iman
14:15 queued 09:49
created

EloquentEventsManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 44
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 1
A startGuarding() 0 6 2
A wrapForIgnorance() 0 5 2
1
<?php
2
3
namespace Imanghafoori\HeyMan\WatchingStrategies;
4
5
class EloquentEventsManager
6
{
7
    private $event;
8
9
    private $modelClass;
10
11
    /**
12
     * EloquentEventsManager constructor.
13
     *
14
     * @param $event
15
     * @param $modelClass
16
     *
17
     * @return EloquentEventsManager
18
     */
19 11
    public function init(string $event, array $modelClass) : self
20
    {
21 11
        $this->event = $event;
22 11
        $this->modelClass = $modelClass;
23
24 11
        return $this;
25
    }
26
27
    /**
28
     * @param $callback
29
     */
30 11
    public function startGuarding(callable $callback)
31
    {
32 11
        $callback = $this->wrapForIgnorance($callback);
33
34 11
        foreach ($this->modelClass as $model) {
35 11
            $model::{$this->event}($callback);
36
        }
37 11
    }
38
39
    /**
40
     * @param callable $callback
41
     *
42
     * @return \Closure
43
     */
44 11
    private function wrapForIgnorance(callable $callback): \Closure
45
    {
46
        return function (...$args) use ($callback) {
47 11
            if (!config('heyman_ignore_eloquent', false)) {
48 9
                $callback(...$args);
49
            }
50 11
        };
51
    }
52
}
53