Completed
Push — master ( 64f0e4...83ad14 )
by Iman
07:39
created

EloquentEventsManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A start() 0 5 3
A init() 0 6 1
A startGuarding() 0 4 1
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
    private $data = [];
13
14
    /**
15
     * EloquentEventsManager constructor.
16
     *
17
     * @param $event
18
     * @param $modelClass
19
     *
20
     * @return EloquentEventsManager
21 11
     */
22
    public function init(string $event, array $modelClass) : self
23 11
    {
24 11
        $this->event = $event;
25
        $this->modelClass = $modelClass;
26 11
27
        return $this;
28
    }
29
30
    /**
31
     * @param $callback
32 11
     */
33
    public function startGuarding(callable $callback)
34 11
    {
35 11
        $callback = app(HeyManSwitcher::class)->wrapForIgnorance($callback, 'eloquent');
36 11
        $this->data[] = [$this->modelClass, $this->event, $callback];
37
    }
38 11
39
    public function start()
40
    {
41
        foreach ($this->data as $data) {
42
            foreach ($data[0] as $model) {
43
                $model::{$data[1]}($data[2]);
44
            }
45
        }
46
    }
47
}
48