Completed
Push — master ( a9ac3c...b00023 )
by Francis
02:09
created

configureBodyFromPropertyGenerator()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 31
rs 8.4444
c 0
b 0
f 0
cc 8
nc 8
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Prometee\SwaggerClientGenerator\Swagger\Generator\Model\Method;
6
7
use Prometee\SwaggerClientGenerator\Base\Generator\Object\Method\ConstructorGenerator;
8
use Prometee\SwaggerClientGenerator\Swagger\Generator\Model\Attribute\ModelPropertyGeneratorInterface;
9
use Prometee\SwaggerClientGenerator\Swagger\Generator\Model\Other\ModelPropertiesGeneratorInterface;
10
11
class ModelConstructorGenerator extends ConstructorGenerator implements ModelConstructorGeneratorInterface
12
{
13
    /**
14
     * {@inheritDoc}
15
     */
16
    public function configureFromPropertiesGenerator(ModelPropertiesGeneratorInterface $modelPropertiesGenerator): void
17
    {
18
        $inheritedRequiredProperties = [];
19
        /** @var ModelPropertyGeneratorInterface $modelPropertyGenerator */
20
        foreach ($modelPropertiesGenerator->getProperties() as $modelPropertyGenerator) {
21
            $this->configureParameterFromPropertyGenerator($modelPropertyGenerator);
22
            $this->configureBodyFromPropertyGenerator($modelPropertyGenerator);
23
            if (false === $modelPropertyGenerator->isInherited()) {
24
                continue;
25
            }
26
            if (false === $modelPropertyGenerator->isRequired()) {
27
                continue;
28
            }
29
            $inheritedRequiredProperties[] = $modelPropertyGenerator->getPhpName();
30
        }
31
32
        if (empty($this->getLines())) {
33
            return;
34
        }
35
36
        if (empty($inheritedRequiredProperties)) {
37
            return;
38
        }
39
40
        $this->addLine('');
41
        $this->addLine(sprintf('parent::%s(%s);', $this->name, implode(', ', $inheritedRequiredProperties)));
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    public function configureBodyFromPropertyGenerator(ModelPropertyGeneratorInterface $modelPropertyGenerator): void
48
    {
49
        if ($modelPropertyGenerator->isInherited()) {
50
            return;
51
        }
52
53
        if ($modelPropertyGenerator->isRequired()) {
54
            $this->addLine(sprintf('$this->%1$s = $%1$s;', $modelPropertyGenerator->getName()));
55
            return;
56
        }
57
58
        $defaultValue = null;
59
        switch ($modelPropertyGenerator->getPhpType()) {
60
            case 'array':
61
                $defaultValue = '[]';
62
                break;
63
            case 'string':
64
                $defaultValue = '\'\'';
65
                break;
66
            case 'bool':
67
                $defaultValue = 'true';
68
                break;
69
            case 'int':
70
                $defaultValue = '0';
71
                break;
72
            case 'float':
73
                $defaultValue = '.0';
74
                break;
75
        }
76
77
        $modelPropertyGenerator->setValue($defaultValue);
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function configureParameterFromPropertyGenerator(ModelPropertyGeneratorInterface $modelPropertyGenerator): void
84
    {
85
        if (false === $modelPropertyGenerator->isRequired()) {
86
            return;
87
        }
88
89
        $methodParameterGenerator = clone $this->methodParameterGeneratorSkel;
90
        $methodParameterGenerator->configure(
91
            $modelPropertyGenerator->getTypes(),
92
            $modelPropertyGenerator->getName(),
93
            $modelPropertyGenerator->getValue(),
94
            false,
95
            $modelPropertyGenerator->getDescription()
96
        );
97
98
        $this->addParameter($methodParameterGenerator);
99
    }
100
}