Passed
Push — master ( 973a5a...376082 )
by Paul
05:11
created

RequestTransform   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A transform() 0 11 3
A applyRequestTransformers() 0 11 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CCT\Component\Rest\Http\Transform;
6
7
use CCT\Component\Rest\Serializer\ContextInterface;
8
use CCT\Component\Rest\Transformer\Request\RequestTransformerInterface;
9
10
class RequestTransform implements RequestTransformInterface
11
{
12
    /**
13
     * @var RequestTransformerInterface[]|\Closure[]
14
     */
15
    protected $transformers;
16
17
    /**
18
     * RequestTransform constructor.
19
     *
20
     * @param array $transformers
21
     */
22 6
    public function __construct(array $transformers = [])
23
    {
24 6
        $this->transformers = $transformers;
25 6
    }
26
27
    /**
28
     * Tries to identify the data object sent, and convert them
29
     * into an array properly handled
30
     *
31
     * @param array|object $formData
32
     * @param ContextInterface|null $context
33
     *
34
     * @return array|object
35
     */
36 5
    public function transform($formData = [], ContextInterface $context = null)
37
    {
38 5
        if (empty($formData)) {
39 3
            return $formData;
40
        }
41
42 2
        foreach ($this->transformers as $transformer) {
43 2
            $formData = $this->applyRequestTransformers($transformer, $formData, $context);
44
        }
45
46 2
        return $formData;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $formData also could return the type object which is incompatible with the return type mandated by CCT\Component\Rest\Http\...mInterface::transform() of array.
Loading history...
47
    }
48
49
    /**
50
     * Applied single request transformer
51
     *
52
     * @param RequestTransformerInterface|\Closure $transformer
53
     * @param array|object $formData
54
     * @param ContextInterface|null $context
55
     *
56
     * @return array|mixed
57
     */
58 2
    protected function applyRequestTransformers($transformer, $formData, ContextInterface $context = null)
59
    {
60 2
        if ($transformer instanceof RequestTransformerInterface && $transformer->supports($formData)) {
61 1
            return $transformer->transform($formData, $context);
62
        }
63
64 1
        if ($transformer instanceof \Closure) {
65
            return $transformer($formData);
66
        }
67
68 1
        return $formData;
69
    }
70
}
71