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

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