Completed
Push — master ( d4cce2...09a4c4 )
by Francis
02:41
created

OperationsMethodGenerator::buildBodyParam()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Prometee\SwaggerClientGenerator\Swagger\Generator\Operation;
6
7
use Prometee\SwaggerClientGenerator\Base\Generator\Object\Method\MethodGenerator;
8
use Prometee\SwaggerClientGenerator\Swagger\Helper\SwaggerOperationsHelper;
9
10
class OperationsMethodGenerator extends MethodGenerator implements OperationsMethodGeneratorInterface
11
{
12
    /** @var {@inheritDoc} */
0 ignored issues
show
Documentation Bug introduced by
The doc comment {@inheritDoc} at position 0 could not be parsed: Unknown type name '{' at position 0 in {@inheritDoc}.
Loading history...
13
    protected $scope = self::SCOPE_PUBLIC;
14
15
    /**
16
     * {@inheritDoc}
17
     */
18
    public function configure(
19
        string $scope,
20
        string $name,
21
        array $returnTypes = [],
22
        bool $static = false,
23
        string $description = ''
24
    )
25
    {
26
        parent::configure(self::SCOPE_PUBLIC, $name, $returnTypes, false, $description);
27
    }
28
29
    /**
30
     * {@inheritDoc}
31
     */
32
    public function getMinifiedReturnType(): string
33
    {
34
        if (empty($this->returnTypes)) {
35
            return 'null';
36
        }
37
38
        if ($this->returnTypes === ['void']) {
39
            return 'null';
40
        }
41
42
        if ($this->returnTypes === ['array']) {
43
            return '\'array\'';
44
        }
45
46
        $suffix = '';
47
        if ($this->getPhpReturnType() === 'array') {
48
            $suffix = '.\'[]\'';
49
        }
50
51
        return rtrim($this->returnTypes[0], '[]') . '::class' . $suffix;
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57
    public function addMethodBodyFromSwaggerConfiguration(string $path, string $operation, array $operationParameters, string $indent = null): void
58
    {
59
        $bodyParam = null;
60
61
        if (in_array($operation, ['post', 'put'])) {
62
            $bodyParam = $this->buildBodyParam($operationParameters);
63
        }
64
65
        $this->addOperationTypeLines($operation, $path, $operationParameters, $bodyParam, $indent);
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public function addOperationTypeLines(string $operation, string $path, array $operationParameters, string $bodyParam = null, string $indent = null): void
72
    {
73
        $pathParamsFormat = $this->generateParamsType('path', $operationParameters, '%1$s', '%2$s%2$s');
74
        $queryParamsFormat = $this->generateParamsType('query', $operationParameters, '%1$s', '%2$s%2$s');
75
76
        $format =
77
            '%3$s$this->exec%4$sOperation(%1$s'
78
                . '%2$s\'%5$s\',%1$s'
79
                . '%2$s%6$s,%1$s'
80
                . (null === $bodyParam ? '' : '%2$s'.$bodyParam.',%1$s')
81
                . '%2$s['.$pathParamsFormat.(empty($pathParamsFormat) ? '' : '%1$s%2$s').'],%1$s'
82
                . '%2$s['.$queryParamsFormat.(empty($queryParamsFormat) ? '' : '%1$s%2$s').']%1$s'
83
            . ');%1$s'
84
        ;
85
86
        $this->addLine(sprintf($format,
87
            "\n",
88
            $indent,
89
            $this->returnTypes === ['void'] ? '' : 'return ',
90
            ucfirst($operation),
91
            $path,
92
            $this->getMinifiedReturnType()
93
        ));
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99
    public function generateParamsType(string $type, array $operationParameters, string $lineBreak = null, string $indent = null): string
100
    {
101
        $params = $this->buildParamsType($type, $operationParameters, '\'%1$s\' => $%1$s');
102
        if (count($params) === 0) {
103
            return '';
104
        }
105
106
        $glue = sprintf(',%s%2$s', $lineBreak, $indent);
107
        return sprintf('%1$s%2$s%3$s',
108
            $lineBreak,
109
            $indent,
110
            implode($glue, $params)
111
        );
112
    }
113
114
    /**
115
     * {@inheritDoc}
116
     */
117
    public function buildParamsType(string $type, array $operationParameters, string $format): array
118
    {
119
        $pathParams = [];
120
        foreach ($operationParameters as $operationParameter) {
121
            if (!isset($operationParameter['in'])) {
122
                continue;
123
            }
124
            if ($type !== $operationParameter['in']) {
125
                continue;
126
            }
127
            if (!isset($operationParameter['name'])) {
128
                continue;
129
            }
130
            $parameterName = lcfirst(SwaggerOperationsHelper::cleanStr($operationParameter['name']));
131
            $param = sprintf($format, $parameterName);
132
            $pathParams[] = $param;
133
        }
134
135
        return $pathParams;
136
    }
137
138
    /**
139
     * {@inheritDoc}
140
     */
141
    public function buildBodyParam(array $operationParameters): string
142
    {
143
        $bodyParams = $this->buildParamsType('body', $operationParameters, '$%s');
144
145
        // only one should exists
146
        return implode('', $bodyParams);
147
    }
148
}
149