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

BehaviorRegistry   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 29
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A findBehaviorByClass() 0 9 3
A hasBehavior() 0 9 3
A lock() 0 3 1
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