Completed
Push — master ( c650bf...79b6e2 )
by
unknown
23s queued 18s
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
/**
10
 * @psalm-suppress MissingTemplateParam
11
 * @psalm-suppress InvalidTemplateParam
12
 */
13
abstract class BehaviorRegistry implements HasLockInterface, HasBehaviorsInterface
14
{
15
    public function hasBehavior(string $behaviorClassName): bool
16
    {
17
        foreach ($this->withBehaviors() as $behavior) {
18
            if ($behavior instanceof $behaviorClassName) {
19
                return true;
20
            }
21
        }
22
23
        return false;
24
    }
25
26
    public function findBehaviorByClass(string $class)
27
    {
28
        foreach ($this->withBehaviors() as $behavior) {
29
            if ($behavior instanceof $class) {
30
                return $behavior;
31
            }
32
        }
33
34
        return null;
35
    }
36
37
    public function lock(): void
38
    {
39
        $this->withBehaviors()->lock();
40
    }
41
}
42