Passed
Push — master ( 1eb67a...d212d2 )
by Dmitry
02:28
created

ModelTransformer::findSupportedModelTransformer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 3
nop 2
1
<?php
2
3
namespace Tonic\Component\ApiLayer\ModelTransformer;
4
5
use Tonic\Component\ApiLayer\ModelTransformer\Exception\UnsupportedTransformationException;
6
7
/**
8
 * Manages transformers.
9
 */
10
class ModelTransformer implements ModelTransformerInterface
11
{
12
    /**
13
     * @var array
14
     */
15
    private $modelTransformers = [];
16
17
    /**
18
     * @var array
19
     */
20
    private $sorted = [];
21
22
    /**
23
     * @param ModelTransformerInterface $modelTransformer
24
     *
25
     * @param int $priority
26
     * @return $this|ModelTransformerInterface
27
     */
28
    public function addModelTransformer(ModelTransformerInterface $modelTransformer, $priority = 0)
29
    {
30
        if (!isset($this->modelTransformers[$priority])) {
31
            $this->modelTransformers[$priority] = [];
32
        }
33
34
        $this->modelTransformers[$priority][] = $modelTransformer;
35
        unset($this->sorted);
36
37
        return $this;
38
    }
39
40
    /**
41
     * @return ModelTransformerInterface[]
42
     */
43
    public function getModelTransformers()
44
    {
45
        if (isset($this->sorted)) {
46
            return $this->sorted;
47
        }
48
49
        krsort($this->modelTransformers);
50
        $this->sorted = [];
51
        foreach ($this->modelTransformers as $modelTransformers) {
52
            $this->sorted = array_merge($this->sorted, $modelTransformers);
53
        }
54
55
        return $this->sorted;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function supports($object, $targetClass)
62
    {
63
        foreach ($this->getModelTransformers() as $modelTransformer) {
64
            if ($modelTransformer->supports($object, $targetClass)) {
65
                return true;
66
            }
67
        }
68
69
        return false;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function transform($object, $targetClass)
76
    {
77
        $modelTransformer = $this->findSupportedModelTransformer($object, $targetClass);
78
        if ($modelTransformer) {
79
            return $modelTransformer->transform($object, $targetClass);
80
        }
81
82
        $objectType = is_object($object) ? get_class($object) : gettype($object);
83
        throw new UnsupportedTransformationException(sprintf(
84
            'Can not transform object of type "%s" to object of type "%s"',
85
            $objectType,
86
            $targetClass
87
        ));
88
    }
89
90
    /**
91
     * Finds and returns model transformer which supports specified object and target class.
92
     *
93
     * @param object|array $object
94
     * @param string $targetClass
95
     *
96
     * @return ModelTransformerInterface|null
97
     */
98
    public function findSupportedModelTransformer($object, $targetClass)
99
    {
100
        foreach ($this->getModelTransformers() as $modelTransformer) {
101
            if ($modelTransformer->supports($object, $targetClass)) {
102
                return $modelTransformer;
103
            }
104
        }
105
106
        return null;
107
    }
108
}
109