Passed
Pull Request — master (#104)
by
unknown
02:45 queued 23s
created

BehaviorRegistry   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

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