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

EloquentEventsManager::wrapForIgnorance()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
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) : EloquentEventsManager
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
        $callback = $this->wrapForIgnorance($callback);
33 11
34 9
        foreach ($this->modelClass as $model) {
35
            $model::{$this->event}($callback);
36 11
        }
37
    }
38 11
39 11
    /**
40
     * @param callable $callback
41 11
     * @return \Closure
42
     */
43
    private function wrapForIgnorance(callable $callback): \Closure
44
    {
45
        return function (...$args) use ($callback) {
46
            if (! config('heyman_ignore_eloquent', false)) {
47
                $callback(...$args);
48
            }
49
        };
50
    }
51
}
52