Passed
Push — master ( fe8874...312ccc )
by Paul
02:35
created

RequestTransform::transform()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.125

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 11
ccs 3
cts 6
cp 0.5
crap 4.125
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CCT\Component\Rest\Http\Transform;
6
7
use CCT\Component\Rest\Transformer\Request\RequestTransformerInterface;
8
9
class RequestTransform implements RequestTransformInterface
10
{
11
12
    /**
13
     * @var RequestTransformerInterface[]|\Closure[]
14
     */
15
    protected $transformers;
16
17
    /**
18
     * RequestTransform constructor.
19
     *
20
     * @param array $transformers
21
     */
22 3
    public function __construct(array $transformers = [])
23
    {
24 3
        $this->transformers = $transformers;
25 3
    }
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
     *
33
     * @return array
34
     */
35 2
    public function transform($formData = [])
36
    {
37 2
        if (empty($formData)) {
38 2
            return $formData;
39
        }
40
41
        foreach ($this->transformers as $transformer) {
42
            $this->applyRequestTransformers($transformer, $formData);
43
        }
44
45
        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 documented return type array.
Loading history...
46
    }
47
48
49
    /**
50
     * Applied single request transformer
51
     *
52
     * @param RequestTransformerInterface|\Closure $transformer
53
     * @param array|object $formData
54
     */
55
    protected function applyRequestTransformers($transformer, $formData)
56
    {
57
        if ($transformer instanceof RequestTransformerInterface && $transformer->supports($formData)) {
58
            $transformer->transform($formData);
59
        }
60
61
        if ($transformer instanceof \Closure) {
62
            $transformer($formData);
63
        }
64
    }
65
}
66