Passed
Pull Request — master (#104)
by
unknown
02:33
created

BehaviorRegistry::hasBehavior()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace hiqdev\php\billing\product\behavior;
6
7
use hiqdev\php\billing\product\trait\HasLockInterface;
8
9
/**
10
 * @template TParentCollection
11
 * @implements HasBehaviorsInterface<TParentCollection>
12
 */
13
abstract class BehaviorRegistry implements HasLockInterface, HasBehaviorsInterface
14
{
15
    /**
16
     * @return BehaviorCollectionInterface<TParentCollection>
17
     */
18
    abstract protected function getBehaviorCollection(): BehaviorCollectionInterface;
19
20
    public function hasBehavior(string $behaviorClassName): bool
21
    {
22
        foreach ($this->getBehaviorCollection() as $behavior) {
23
            if ($behavior instanceof $behaviorClassName) {
24
                return true;
25
            }
26
        }
27
28
        return false;
29
    }
30
31
    public function findBehaviorByClass(string $class)
32
    {
33
        foreach ($this->getBehaviorCollection() as $behavior) {
34
            if ($behavior instanceof $class) {
35
                return $behavior;
36
            }
37
        }
38
39
        return null;
40
    }
41
42
    public function lock(): void
43
    {
44
        $this->getBehaviorCollection()->lock();
45
    }
46
}
47