TransformationProviderAbstract   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __invoke() 0 15 4
1
<?php
2
3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ICanBoogie\Transformation;
13
14
/**
15
 * An abstract transformation provider.
16
 */
17
abstract class TransformationProviderAbstract implements TransformationProvider
18
{
19
    /**
20
     * @var array
21
     */
22
    private $transformations;
23
24
    /**
25
     * @param array $transformations An array of key/value pairs where _key_ is a supported class and _value_
26
     * a transformation. What qualifies as _transformation_ depends on the implementation. It could be a
27
     * Transformation instance, a callable, or a reference to a service…
28
     */
29
    public function __construct(array $transformations)
30
    {
31
        $this->transformations = $transformations;
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function __invoke($data): callable
38
    {
39
        foreach ($this->transformations as $type => $transformation) {
40
            if (!$data instanceof $type) {
41
                continue;
42
            }
43
44
            try {
45
                return $this->resolve($transformation);
46
            } catch (\Throwable $e) {
47
                throw new TransformationNotFound($data, null, $e);
48
            }
49
        }
50
51
        throw new TransformationNotFound($data);
52
    }
53
54
    /**
55
     * Resolves a transformation.
56
     *
57
     * @param mixed $transformation
58
     *
59
     * @return Transformation|callable
60
     */
61
    abstract protected function resolve($transformation): callable;
62
}
63