Completed
Push — master ( 1c6d46...3dbc46 )
by Joel
10:24
created

OperationGenerator::getDenormalizer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
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\Reference\Resolver;
9
use Joli\Jane\OpenApi\Generator\Parameter\BodyParameterGenerator;
10
use Joli\Jane\OpenApi\Generator\Parameter\FormDataParameterGenerator;
11
use Joli\Jane\OpenApi\Generator\Parameter\HeaderParameterGenerator;
12
use Joli\Jane\OpenApi\Generator\Parameter\PathParameterGenerator;
13
use Joli\Jane\OpenApi\Generator\Parameter\QueryParameterGenerator;
14
use Joli\Jane\OpenApi\Operation\Operation;
15
use PhpParser\Node\Arg;
16
use PhpParser\Node\Expr;
17
use PhpParser\Node\Name;
18
use PhpParser\Node\Stmt;
19
use PhpParser\Node\Scalar;
20
use PhpParser\Comment;
21
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
22
23
class OperationGenerator
24
{
25
    use OutputGeneratorTrait;
26
    use InputGeneratorTrait;
27
28
    /**
29
     * @var DenormalizerInterface
30
     */
31 7
    protected $denormalizer;
32
33 7
    public function __construct(DenormalizerInterface $denormalizer, BodyParameterGenerator $bodyParameterGenerator, FormDataParameterGenerator $formDataParameterGenerator, HeaderParameterGenerator $headerParameterGenerator, PathParameterGenerator $pathParameterGenerator, QueryParameterGenerator $queryParameterGenerator)
34 7
    {
35 7
        $this->denormalizer               = $denormalizer;
36 7
        $this->bodyParameterGenerator     = $bodyParameterGenerator;
37 7
        $this->formDataParameterGenerator = $formDataParameterGenerator;
38 7
        $this->headerParameterGenerator   = $headerParameterGenerator;
39 7
        $this->pathParameterGenerator     = $pathParameterGenerator;
40
        $this->queryParameterGenerator    = $queryParameterGenerator;
41
    }
42
43
    /**
44
     * Generate a method for an operation
45
     *
46
     * @param string    $name
47
     * @param Operation $operation
48
     * @param Context   $context
49
     *
50 7
     * @return Stmt\ClassMethod
51
     */
52
    public function generate($name, Operation $operation, Context $context)
53 7
    {
54 7
        // Input
55 7
        list($queryParamDocumentation, $queryParamStatements, $queryParamVariable) = $this->createQueryParamStatements($operation);
56 7
        list($documentationParameters, $parameters) = $this->createParameters($operation, $queryParamDocumentation, $context);
57 7
        list($urlStatements, $urlVariable) = $this->createUrlStatements($operation, $queryParamVariable);
58
        list($bodyStatements, $bodyVariable) = $this->createBodyStatements($operation, $queryParamVariable, $context);
59 7
        list($headerStatements, $headerVariable) = $this->createHeaderStatements($operation, $queryParamVariable);
60
61 7
        $statements = array_merge($queryParamStatements, $urlStatements, $headerStatements, $bodyStatements, [
62 7
            // $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...
63 7
            new Expr\Assign(new Expr\Variable('request'), new Expr\MethodCall(
64 1
                new Expr\PropertyFetch(new Expr\Variable('this'), 'messageFactory'),
65 7
                'createRequest',
66 7
                [
67 7
                    new Arg(new Scalar\String_($operation->getMethod())),
68 7
                    new Arg($urlVariable),
69 7
                    new Arg($headerVariable),
70 7
                    new Arg($bodyVariable)
71
                ]
72 7
            )),
73 7
            // $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...
74 7
            new Expr\Assign(new Expr\Variable('promise'), new Expr\MethodCall(
75 7
                new Expr\PropertyFetch(new Expr\Variable('this'), 'httpClient'),
76 7
                'sendAsyncRequest',
77
                [new Arg(new Expr\Variable('request'))]
78 7
            )),
79 7
            // 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...
80
            new Stmt\If_(
81
                new Expr\BinaryOp\Identical(new Expr\ConstFetch(new Name('self::FETCH_PROMISE')), new Expr\Variable('fetch')),
82 7
                [
83 7
                    'stmts' => [
84 7
                        new Stmt\Return_(new Expr\Variable('promise'))
85 7
                    ]
86
                ]
87 7
            ),
88 7
            // $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...
89
            new Expr\Assign(new Expr\Variable('response'), new Expr\MethodCall(
90 7
                new Expr\Variable('promise'),
91 7
                'wait'
92
            )),
93
        ]);
94 7
95 7
        // Output
96
        $outputStatements = [];
97 7
        $outputTypes = ["\\Psr\\Http\\Message\\ResponseInterface"];
98 7
99 2
        foreach ($operation->getOperation()->getResponses() as $status => $response) {
100 2
            if ($response instanceof Reference) {
101
                list(, $response) = $this->resolve($response, Response::class);
102 7
            }
103
104 7
            list($outputType, $ifStatus) = $this->createResponseDenormalizationStatement($status, $response->getSchema(), $context);
105 2
106 2
            if (null !== $outputType) {
107 2
                if (!in_array($outputType, $outputTypes)) {
108
                    $outputTypes[] = $outputType;
109 2
                }
110 2
111 7
                $outputStatements[] = $ifStatus;
112
            }
113 7
        }
114 2
115 2
        if (!empty($outputStatements)) {
116
            $statements[] = new Stmt\If_(
117
                new Expr\BinaryOp\Equal(new Expr\ConstFetch(new Name('self::FETCH_OBJECT')), new Expr\Variable('fetch')),
118 2
                [
119 2
                    'stmts' => $outputStatements
120 2
                ]
121
            );
122
        }
123 7
124 7
        // return $response
125
        $statements[] = new Stmt\Return_(new Expr\Variable('response'));
126 7
        $documentation = array_merge(
127 7
            [
128 7
                '/**',
129 7
                sprintf(" * %s", $operation->getOperation()->getDescription()),
130 7
                ' *',
131
            ],
132 7
            $documentationParameters,
133 7
            [
134
                ' *',
135 7
                ' * @return ' . implode('|', $outputTypes),
136 7
                ' */'
137
            ]
138 7
        );
139 7
140 7
        return new Stmt\ClassMethod($name, [
141
            'type'     => Stmt\Class_::MODIFIER_PUBLIC,
142 7
            'params'   => $parameters,
143 7
            'stmts'    => $statements
144 7
        ], [
145
            'comments' => [new Comment\Doc(implode("\n", $documentation))]
146
        ]);
147
    }
148
149
    /**
150 3
     * @return DenormalizerInterface
151
     */
152 3
    protected function getDenormalizer()
153
    {
154
        return $this->denormalizer;
155
    }
156
}
157