Completed
Pull Request — master (#29)
by
unknown
01:39
created

MutationConfigBuilder::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Softonic\GraphQL\Config;
4
5
class MutationConfigBuilder
6
{
7
    const CHILDREN_PROPERTY_NAME = 'children';
8
9 72
    public function build(array $config): array
10
    {
11 72
        $mutationConfig = [];
12 72
        foreach ($config as $variable => $typeConfig) {
13 72
            $mutationConfig[$variable] = $this->buildMutationTypeConfig($typeConfig);
14
        }
15
16 72
        return $mutationConfig;
17
    }
18
19 72
    private function buildMutationTypeConfig(array $typeConfig): MutationTypeConfig
20
    {
21 72
        $mutationTypeConfig = new MutationTypeConfig();
22
23 72
        foreach ($typeConfig as $propertyName => $propertyValue) {
24 72
            $this->setMutationTypeConfig($mutationTypeConfig, $propertyName, $propertyValue);
25
        }
26
27 72
        return $mutationTypeConfig;
28
    }
29
30 72
    private function setMutationTypeConfig(
31
        MutationTypeConfig $mutationTypeConfig,
32
        string $propertyName,
33
        $propertyValue
34
    ): void {
35 72
        if ($propertyName === self::CHILDREN_PROPERTY_NAME) {
36 72
            foreach ($propertyValue as $childName => $childConfig) {
37 72
                $mutationTypeConfig->children[$childName] = $this->buildMutationTypeConfig($childConfig);
38
            }
39
        } else {
40 72
            $mutationTypeConfig->{$propertyName} = $propertyValue;
41
        }
42 72
    }
43
}
44