Behaviours   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A without() 0 7 2
A apply() 0 10 3
A add() 0 3 1
A remove() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Orm\Entity;
5
6
use Sirius\Orm\Behaviour\BehaviourInterface;
7
use Sirius\Orm\Helpers\Str;
8
9
class Behaviours
10
{
11
12
    protected $list = [];
13
14
    public function add(BehaviourInterface $behaviour)
15
    {
16
        $this->list[$behaviour->getName()] = $behaviour;
17
    }
18
19
    public function remove($name)
20
    {
21
        unset($this->list[$name]);
22
    }
23
24
    public function without(...$names)
25
    {
26
        $clone = clone $this;
27
        foreach ($names as $name) {
28
            $clone->remove($name);
29
        }
30
        return $clone;
31
    }
32
33
    public function apply($mapper, $target, $result, ...$args)
34
    {
35
        foreach ($this->list as $behaviour) {
36
            $method = 'on' . Str::className($target);
37
            if (method_exists($behaviour, $method)) {
38
                $result = $behaviour->{$method}($mapper, $result, ...$args);
39
            }
40
        }
41
42
        return $result;
43
    }
44
}
45