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

BehaviorRegistry::hasBehavior()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
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
abstract class BehaviorRegistry implements HasLockInterface, HasBehaviorsInterface
10
{
11
    abstract protected function getBehaviorCollection(): BehaviorCollectionInterface;
12
13
    public function hasBehavior(string $behaviorClassName): bool
14
    {
15
        foreach ($this->getBehaviorCollection() as $behavior) {
16
            if ($behavior instanceof $behaviorClassName) {
17
                return true;
18
            }
19
        }
20
21
        return false;
22
    }
23
24
    public function findBehaviorByClass(string $class)
25
    {
26
        foreach ($this->withBehaviors() as $behavior) {
27
            if ($behavior instanceof $class) {
28
                return $behavior;
29
            }
30
        }
31
32
        return null;
33
    }
34
35
    public function lock(): void
36
    {
37
        $this->getBehaviorCollection()->lock();
38
    }
39
}
40