PluginFormNormalizer   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
dl 0
loc 78
ccs 30
cts 32
cp 0.9375
rs 10
c 0
b 0
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
B normalizeParams() 0 20 5
A __construct() 0 4 1
B setConfigParams() 0 12 5
A normalize() 0 16 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CCT\Kong\Form\Normalizer;
6
7
use JMS\Serializer\SerializationContext;
8
use JMS\Serializer\SerializerInterface;
9
10
class PluginFormNormalizer implements FormNormalizerInterface
11
{
12
    /**
13
     * @var Serializer|SerializerInterface
0 ignored issues
show
Bug introduced by
The type CCT\Kong\Form\Normalizer\Serializer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
     */
15
    protected $serializer;
16
17
    /**
18
     * @var SerializationContext|null
19
     */
20
    protected $serializationContext;
21
22 4
    public function __construct(SerializerInterface $serializer, SerializationContext $context = null)
23
    {
24 4
        $this->serializer = $serializer;
25 4
        $this->serializationContext = $context;
26 4
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 4
    public function normalize($formData = []): array
32
    {
33 4
        if (empty($formData)) {
34
            return [];
35
        }
36
37 4
        if (is_object($formData)) {
38 4
            $formData = $this->serializer->toArray(
39 4
                $formData,
40 4
                $this->serializationContext
41
            );
42
        }
43
44 4
        $formParams = $this->normalizeParams($formData);
45
46 4
        return $formParams;
47
    }
48
49
    /**
50
     * @param array $formData
51
     *
52
     * @return array
53
     */
54 4
    protected function normalizeParams(array $formData): array
55
    {
56 4
        $formParams = [];
57 4
        foreach ($formData as $key => $value) {
58 4
            if (empty($value)) {
59 1
                continue;
60
            }
61
62 4
            if ($key === 'config') {
63 3
                $this->setConfigParams($formParams, $value);
64 3
                continue;
65
            }
66
67 4
            $formParams[$key] = (is_array($value))
68
                ? join(',', $value)
69 4
                : $value
70
            ;
71
        }
72
73 4
        return $formParams;
74
    }
75
76 3
    protected function setConfigParams(&$formParams, array $configData): void
77
    {
78 3
        foreach ($configData as $key => $data) {
79 3
            if (false === strpos($key, 'config.')) {
80 2
                $key = sprintf('config.%s', $key);
81
            }
82
83 3
            if (is_bool($data)) {
84 2
                $data = $data ? 'true' : 'false';
85
            }
86
87 3
            $formParams[$key] = $data;
88
        }
89 3
    }
90
}
91