Completed
Push — master ( 89a74b...b4fe79 )
by Iman
09:03 queued 05:19
created

EloquentEventsManager::forgetAbout()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6.0702

Importance

Changes 0
Metric Value
cc 6
eloc 7
nc 5
nop 2
dl 0
loc 13
ccs 7
cts 8
cp 0.875
crap 6.0702
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
     */
23 15
    public function init(string $event, array $modelClass) : self
24
    {
25 15
        $this->event = $event;
26 15
        $this->modelClass = $modelClass;
27
28 15
        return $this;
29
    }
30
31
    /**
32
     * @param $callback
33
     */
34 15
    public function commitChain(callable $callback)
35
    {
36 15
        $callback = app(HeyManSwitcher::class)->wrapForIgnorance($callback, 'eloquent');
37 15
        $this->data[] = [$this->modelClass, $this->event, $callback];
38 15
    }
39
40 105
    public function start()
41
    {
42 105
        foreach ($this->data as $data) {
43 13
            foreach ($data[0] as $model) {
44 13
                $model::{$data[1]}($data[2]);
45
            }
46
        }
47 105
    }
48
49 4
    public function forgetAbout($models, $event = null)
50
    {
51 4
        foreach ($models as $model) {
52 4
            foreach ($this->data as $i => $data) {
53 4
                if (($key = array_search($model, $data[0])) === false) {
54 2
                    continue;
55
                }
56
57 4
                if (!is_null($event) && $event !== $data[1]) {
58
                    continue;
59
                }
60
61 4
                unset($this->data[$i][0][$key]);
62
            }
63
        }
64 4
    }
65
}
66