Completed
Pull Request — master (#42)
by Jose Manuel
01:40
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 102
    public function build(array $config): array
10
    {
11 102
        $mutationConfig = [];
12 102
        foreach ($config as $variable => $typeConfig) {
13 102
            $mutationConfig[$variable] = $this->buildMutationTypeConfig($typeConfig);
14
        }
15
16 102
        return $mutationConfig;
17
    }
18
19 102
    private function buildMutationTypeConfig(array $typeConfig): MutationTypeConfig
20
    {
21 102
        $mutationTypeConfig = new MutationTypeConfig();
22
23 102
        foreach ($typeConfig as $propertyName => $propertyValue) {
24 102
            $this->setMutationTypeConfig($mutationTypeConfig, $propertyName, $propertyValue);
25
        }
26
27 102
        return $mutationTypeConfig;
28
    }
29
30 102
    private function setMutationTypeConfig(
31
        MutationTypeConfig $mutationTypeConfig,
32
        string $propertyName,
33
        $propertyValue
34
    ): void {
35 102
        if ($propertyName === self::CHILDREN_PROPERTY_NAME) {
36 102
            foreach ($propertyValue as $childName => $childConfig) {
37 102
                $mutationTypeConfig->children[$childName] = $this->buildMutationTypeConfig($childConfig);
38
            }
39
        } else {
40 102
            $mutationTypeConfig->{$propertyName} = $propertyValue;
41
        }
42 102
    }
43
}
44