Completed
Push — master ( 03118f...776076 )
by Phil
05:02
created

DecoratorTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 40
ccs 0
cts 17
cp 0
rs 10

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
    protected function decorate(Collection $collection, $action)
20
    {
21
        foreach ($collection->getIterator() as $entity) {
22
            $decorators = $entity->getDecorators($action);
23
            array_walk($decorators, [$this, 'invokeDecorators'], [$entity]);
24
        }
25
    }
26
27
    /**
28
     * Invoke callable decorator on entity.
29
     *
30
     * @param string                        $decorator
31
     * @param array                         $properties
32
     * @param \Percy\Entity\EntityInterface $entity
33
     *
34
     * @return void
35
     */
36
    protected function invokeDecorators($decorator, array $properties, 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