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

RequestTransform   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 42.86%

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 6
cts 14
cp 0.4286
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 8 4
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