DecoratorTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 42.86%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 40
ccs 6
cts 14
cp 0.4286
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A decorate() 0 7 2
A invokeDecorators() 0 12 2
1
<?php
2
3
namespace Percy\Decorator;
4
5
use InvalidArgumentException;
6
use Percy\Entity\Collection;
7
use Percy\Entity\EntityInterface;
8
9
trait DecoratorTrait
10
{
11
    /**
12
     * Iterate collection and apply decorators based on action.
13
     *
14
     * @param \Percy\Entity\Collection $collection
15
     * @param integer                  $action
16
     *
17
     * @return boolean
18
     */
19 4
    protected function decorate(Collection $collection, $action)
20
    {
21 4
        foreach ($collection->getIterator() as $entity) {
22 3
            $decorators = $entity->getDecorators($action);
23 3
            array_walk($decorators, [$this, 'invokeDecorators'], $entity);
24 4
        }
25 4
    }
26
27
    /**
28
     * Invoke callable decorator on entity.
29
     *
30
     * @param array                         $properties
31
     * @param string                        $decorator
32
     * @param \Percy\Entity\EntityInterface $entity
33
     *
34
     * @return void
35
     */
36
    protected function invokeDecorators(array $properties, $decorator, EntityInterface $entity)
37
    {
38
        $decorator = new $decorator;
39
40
        if (! $decorator instanceof DecoratorInterface) {
41
            throw new InvalidArgumentException(
42
                sprintf('(%s) must be an instance of (Percy\Decorator\DecoratorInterface)', get_class($decorator))
43
            );
44
        }
45
46
        $decorator($entity, $properties);
47
    }
48
}
49