Completed
Push — master ( 0ae3fd...89a74b )
by Iman
05:30
created

EloquentEventsManager::forgetAbout()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 7
nc 5
nop 2
dl 0
loc 15
ccs 0
cts 0
cp 0
crap 42
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\HeyMan\WatchingStrategies;
4
5
use Imanghafoori\HeyMan\HeyManSwitcher;
6
7
class EloquentEventsManager
8
{
9
    private $event;
10
11
    private $modelClass;
12
13
    private $data = [];
14
15
    /**
16
     * EloquentEventsManager constructor.
17
     *
18
     * @param $event
19
     * @param $modelClass
20
     *
21
     * @return EloquentEventsManager
22 11
     */
23
    public function init(string $event, array $modelClass) : self
24 11
    {
25 11
        $this->event = $event;
26
        $this->modelClass = $modelClass;
27 11
28
        return $this;
29
    }
30
31
    /**
32
     * @param $callback
33 11
     */
34
    public function commitChain(callable $callback)
35 11
    {
36 11
        $callback = app(HeyManSwitcher::class)->wrapForIgnorance($callback, 'eloquent');
37 11
        $this->data[] = [$this->modelClass, $this->event, $callback];
38
    }
39 101
40
    public function start()
41 101
    {
42 9
        foreach ($this->data as $data) {
43 9
            foreach ($data[0] as $model) {
44
                $model::{$data[1]}($data[2]);
45
            }
46 101
        }
47
    }
48
49
    public function forgetAbout($models, $event = null)
50
    {
51
        foreach ($models as $model) {
52
53
            foreach ($this->data as $i => $data) {
54
55
                if (($key = array_search($model, $data[0])) === false) {
56
                    continue;
57
                }
58
59
                if (! is_null($event) && $event !== $data[1]) {
60
                    continue;
61
                }
62
63
                unset($this->data[$i][0][$key]);
64
            }
65
        }
66
    }
67
}
68