TraversingTransformer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 37
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A transform() 0 16 4
1
<?php
2
3
namespace TreeHouse\Feeder\Modifier\Data\Transformer;
4
5
use TreeHouse\Feeder\Exception\TransformationFailedException;
6
7
class TraversingTransformer implements TransformerInterface
8
{
9
    /**
10
     * Transformer that will be applied to each value.
11
     *
12
     * @var TransformerInterface
13
     */
14
    protected $transformer;
15
16
    /**
17
     * @param TransformerInterface $transformer transformer to apply
18
     */
19 6
    public function __construct(TransformerInterface $transformer)
20
    {
21 6
        $this->transformer = $transformer;
22 6
    }
23
24
    /**
25
     * @inheritdoc
26
     */
27 6
    public function transform($value)
28
    {
29
        // not an array, must be traversable
30 6
        if (!is_array($value) && !$value instanceof \Traversable) {
31 2
            throw new TransformationFailedException(
32 2
                sprintf('Expected an array or \Traversable to transform, got "%s" instead.', gettype($value))
33 2
            );
34
        }
35
36
        // traverse through object, transforming each item
37 4
        foreach ($value as $key => $val) {
38 4
            $value[$key] = $this->transformer->transform($val);
39 4
        }
40
41 4
        return $value;
42
    }
43
}
44