OperationGenerator::getDenormalizer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Joli\Jane\OpenApi\Generator;
4
5
use Joli\Jane\Generator\Context\Context;
6
use Joli\Jane\OpenApi\Model\Response;
7
use Joli\Jane\Runtime\Reference;
8
use Joli\Jane\OpenApi\Generator\Parameter\BodyParameterGenerator;
9
use Joli\Jane\OpenApi\Generator\Parameter\FormDataParameterGenerator;
10
use Joli\Jane\OpenApi\Generator\Parameter\HeaderParameterGenerator;
11
use Joli\Jane\OpenApi\Generator\Parameter\PathParameterGenerator;
12
use Joli\Jane\OpenApi\Generator\Parameter\QueryParameterGenerator;
13
use Joli\Jane\OpenApi\Operation\Operation;
14
use PhpParser\Node\Arg;
15
use PhpParser\Node\Expr;
16
use PhpParser\Node\Name;
17
use PhpParser\Node\Stmt;
18
use PhpParser\Node\Scalar;
19
use PhpParser\Comment;
20
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
21
22
class OperationGenerator
23
{
24
    use OutputGeneratorTrait;
25
    use InputGeneratorTrait;
26
27
    /**
28
     * @var DenormalizerInterface
29
     */
30
    protected $denormalizer;
31
32 13
    public function __construct(DenormalizerInterface $denormalizer, BodyParameterGenerator $bodyParameterGenerator, FormDataParameterGenerator $formDataParameterGenerator, HeaderParameterGenerator $headerParameterGenerator, PathParameterGenerator $pathParameterGenerator, QueryParameterGenerator $queryParameterGenerator)
33
    {
34 13
        $this->denormalizer               = $denormalizer;
35 13
        $this->bodyParameterGenerator     = $bodyParameterGenerator;
36 13
        $this->formDataParameterGenerator = $formDataParameterGenerator;
37 13
        $this->headerParameterGenerator   = $headerParameterGenerator;
38 13
        $this->pathParameterGenerator     = $pathParameterGenerator;
39 13
        $this->queryParameterGenerator    = $queryParameterGenerator;
40 13
    }
41
42
    /**
43
     * Generate a method for an operation
44
     *
45
     * @param string    $name
46
     * @param Operation $operation
47
     * @param Context   $context
48
     *
49
     * @return Stmt\ClassMethod
50
     */
51 12
    public function generate($name, Operation $operation, Context $context)
52
    {
53
        // Input
54 12
        list($queryParamDocumentation, $queryParamStatements, $queryParamVariable) = $this->createQueryParamStatements($operation);
55 12
        list($documentationParameters, $parameters) = $this->createParameters($operation, $queryParamDocumentation, $context);
56 12
        list($urlStatements, $urlVariable) = $this->createUrlStatements($operation, $queryParamVariable);
57 12
        list($bodyStatements, $bodyVariable) = $this->createBodyStatements($operation, $queryParamVariable, $context);
58 12
        list($headerStatements, $headerVariable) = $this->createHeaderStatements($operation, $queryParamVariable);
59
60 12
        $statements = array_merge($queryParamStatements, $urlStatements, $headerStatements, $bodyStatements, [
61
            // $request = $this->messageFactory->createRequest('method', $url, $headers, $body);
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
62 12
            new Expr\Assign(new Expr\Variable('request'), new Expr\MethodCall(
63 12
                new Expr\PropertyFetch(new Expr\Variable('this'), 'messageFactory'),
64 12
                'createRequest',
65
                [
66 12
                    new Arg(new Scalar\String_($operation->getMethod())),
67 12
                    new Arg($urlVariable),
68 12
                    new Arg($headerVariable),
69 12
                    new Arg($bodyVariable)
70
                ]
71
            )),
72
            // $response = $this->httpClient->sendRequest($request);
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
73 12
            new Expr\Assign(new Expr\Variable('promise'), new Expr\MethodCall(
74 12
                new Expr\PropertyFetch(new Expr\Variable('this'), 'httpClient'),
75 12
                'sendAsyncRequest',
76 12
                [new Arg(new Expr\Variable('request'))]
77
            )),
78
            // if ($fetch === self::FETCH_PROMISE) { return $promise }
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
79 12
            new Stmt\If_(
80 12
                new Expr\BinaryOp\Identical(new Expr\ConstFetch(new Name('self::FETCH_PROMISE')), new Expr\Variable('fetch')),
81
                [
82
                    'stmts' => [
83 12
                        new Stmt\Return_(new Expr\Variable('promise'))
84
                    ]
85
                ]
86
            ),
87
            // $response = $promise->wait();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
88 12
            new Expr\Assign(new Expr\Variable('response'), new Expr\MethodCall(
89 12
                new Expr\Variable('promise'),
90 12
                'wait'
91
            )),
92
        ]);
93
94
        // Output
95 12
        $outputStatements = [];
96 12
        $outputTypes = ["\\Psr\\Http\\Message\\ResponseInterface"];
97
98 12
        if ($operation->getOperation()->getResponses()) {
99 11
            foreach ($operation->getOperation()->getResponses() as $status => $response) {
100 11
                if ($response instanceof Reference) {
101 2
                    list(, $response) = $this->resolve($response, Response::class);
102
                }
103
104 11
                list($outputType, $ifStatus) = $this->createResponseDenormalizationStatement(
105 11
                    $status,
106 11
                    $response->getSchema(),
107 11
                    $context,
108 11
                    $operation->getReference() . '/responses/' . $status
109
                );
110
111 11
                if (null !== $outputType) {
112 11
                    if (!in_array($outputType, $outputTypes)) {
113 11
                        $outputTypes[] = $outputType;
114
                    }
115
116 11
                    $outputStatements[] = $ifStatus;
117
                }
118
            }
119
        }
120
121 12
        if (!empty($outputStatements)) {
122 11
            $statements[] = new Stmt\If_(
123 11
                new Expr\BinaryOp\Equal(new Expr\ConstFetch(new Name('self::FETCH_OBJECT')), new Expr\Variable('fetch')),
124
                [
125 11
                    'stmts' => $outputStatements
126
                ]
127
            );
128
        }
129
130
        // return $response
131 12
        $statements[] = new Stmt\Return_(new Expr\Variable('response'));
132 12
        $documentation = array_merge(
133
            [
134 12
                '/**',
135 12
                sprintf(" * %s", $operation->getOperation()->getDescription()),
136 12
                ' *',
137
            ],
138 12
            $documentationParameters,
139
            [
140 12
                ' *',
141 12
                ' * @return ' . implode('|', $outputTypes),
142 12
                ' */'
143
            ]
144
        );
145
146 12
        return new Stmt\ClassMethod($name, [
147 12
            'type'     => Stmt\Class_::MODIFIER_PUBLIC,
148 12
            'params'   => $parameters,
149 12
            'stmts'    => $statements
150
        ], [
151 12
            'comments' => [new Comment\Doc(implode("\n", $documentation))]
152
        ]);
153
    }
154
155
    /**
156
     * @return DenormalizerInterface
157
     */
158 7
    protected function getDenormalizer()
159
    {
160 7
        return $this->denormalizer;
161
    }
162
}
163