Transformer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 43
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A addTransformer() 0 4 1
A __invoke() 0 10 3
1
<?php
2
3
declare (strict_types=1);
4
5
namespace Oxidmod\RestBundle\Transformer;
6
7
use League\Fractal\TransformerAbstract;
8
use Oxidmod\RestBundle\Transformer\Exception\UnsupportedObjectException;
9
10
/**
11
 * Transformer for fractal lib
12
 */
13
class Transformer extends TransformerAbstract
14
{
15
    /**
16
     * @var TransformerInterface[]
17
     */
18
    private $transformers = [];
19
20
    /**
21
     * @param TransformerInterface[] $transformers
22
     */
23
    public function __construct(array $transformers = [])
24
    {
25
        foreach ($transformers as $transformer) {
26
            $this->addTransformer($transformer);
27
        }
28
    }
29
30
    /**
31
     * @param TransformerInterface $transformer
32
     */
33
    private function addTransformer(TransformerInterface $transformer)
34
    {
35
        $this->transformers[] = $transformer;
36
    }
37
38
    /**
39
     * @param mixed $object
40
     *
41
     * @return array
42
     *
43
     * @throws UnsupportedObjectException
44
     */
45
    public function __invoke($object): array
46
    {
47
        foreach ($this->transformers as $transformer) {
48
            if ($transformer->supports($object)) {
49
                return $transformer->transform($object);
50
            }
51
        }
52
53
        throw new UnsupportedObjectException($object);
54
    }
55
}
56