Passed
Pull Request — master (#78)
by Iman
278:27 queued 274:28
created

BaseManager::forgetAbout()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 3
1
<?php
2
3
namespace Imanghafoori\HeyMan\WatchingStrategies;
4
5
use Imanghafoori\HeyMan\Normilizers\InputNormalizer;
6
use Imanghafoori\HeyMan\Reactions\ReactionFactory;
7
use Imanghafoori\HeyMan\Switching\HeyManSwitcher;
8
9
class BaseManager
10
{
11
    use InputNormalizer;
12
13
    protected $watchedEntities = [];
14
15
    protected $event;
16
17
    public $manager;
18
19
    public $data = [];
20
21 123
    public function start()
22
    {
23 123
        foreach ($this->data as $manager => $f) {
24 43
            if ($manager == RouterEventManager::class) {
0 ignored issues
show
Bug introduced by
The type Imanghafoori\HeyMan\Watc...gies\RouterEventManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
                continue;
26
            }
27 43
            foreach ($f as $value => $callbacks) {
28 39
                foreach ($callbacks as $key => $cb) {
29 43
                    resolve($manager)->register($value, $cb, $key);
30
                }
31
            }
32
        }
33 123
    }
34
35 14
    public function forgetAbout($manager, $models, $event = null)
36
    {
37 14
        $models = $this->normalizeInput($models);
38
39 14
        foreach ($models as $model) {
40 14
            if ($event) {
41 4
                unset($this->data[$manager][$model][$event]);
42
            } else {
43 14
                unset($this->data[$manager][$model]);
44
            }
45
        }
46 14
    }
47
48
    /**
49
     * ViewEventManager constructor.
50
     *
51
     * @param $manager
52
     * @param array  $values
53
     * @param string $param
54
     *
55
     * @return self
56
     */
57 104
    public function init($manager, array $values, string $param = 'default'): self
58
    {
59 104
        $this->manager = $manager;
60 104
        $this->watchedEntities = $values;
61 104
        $this->event = $param;
62
63 104
        return $this;
64
    }
65
66 96
    public function commitChain()
67
    {
68 96
        $callback = resolve(ReactionFactory::class)->make();
69 96
        $switchableListener = resolve(HeyManSwitcher::class)->wrapForIgnorance($callback, $this->manager);
70
71 96
        foreach ($this->watchedEntities as $value) {
72 96
            $this->data[$this->manager][$value][$this->event][] = $switchableListener;
73
        }
74 96
    }
75
}
76