All Downloads are FREE. Search and download functionalities are using the official Maven repository.

php-symfony.api_controller.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
partial_header}}
/**
 * NOTE: This class is auto generated by the openapi generator program.
 * https://github.com/openapitools/openapi-generator
 * Do not edit the class manually.
 */

namespace {{controllerPackage}};

use \Exception;
use JMS\Serializer\Exception\RuntimeException as SerializerRuntimeException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Validator\Constraints as Assert;
use {{apiPackage}}\{{classname}};
{{#imports}}use {{import}};
{{/imports}}

/**
 * {{controllerName}} Class Doc Comment
 *
 * @category Class
 * @package  {{controllerPackage}}
 * @author   OpenAPI Generator team
 * @link     https://github.com/openapitools/openapi-generator
 */
class {{controllerName}} extends Controller
{
    {{#operation}}

    /**
     * Operation {{{operationId}}}
{{#summary}}
     *
     * {{{summary}}}
{{/summary}}
     *
{{#description}}
     * {{.}}
     *
{{/description}}
     * @param Request $request The Symfony request to handle.
     * @return Response The Symfony response.
     */
    public function {{operationId}}Action(Request $request{{#hasPathParams}}{{#pathParams}}, ${{paramName}}{{/pathParams}}{{/hasPathParams}})
    {
        {{#bodyParams}}
        // Make sure that the client is providing something that we can consume
        $consumes = [{{#consumes}}'{{{mediaType}}}'{{^-last}}, {{/-last}}{{/consumes}}];
        if (!static::isContentTypeAllowed($request, $consumes)) {
            // We can't consume the content that the client is sending us
            return new Response('', 415);
        }

        {{/bodyParams}}
        {{#returnType}}
        // Figure out what data format to return to the client
        $produces = [{{#produces}}'{{{mediaType}}}'{{^-last}}, {{/-last}}{{/produces}}];
        // Figure out what the client accepts
        $clientAccepts = $request->headers->has('Accept')?$request->headers->get('Accept'):'*/*';
        $responseFormat = $this->getOutputFormat($clientAccepts, $produces);
        if ($responseFormat === null) {
            return new Response('', 406);
        }

        {{/returnType}}
        // Handle authentication
        {{#authMethods}}
        // Authentication '{{name}}' required
        {{#isApiKey}}
        {{#isKeyInHeader}}
        // Set key with prefix in header
        $security{{name}} = $request->headers->get('{{keyParamName}}');
        {{/isKeyInHeader}}
        {{#isKeyInQuery}}
        // Set key with prefix in query string
        $security{{name}} = $request->query->get('{{keyParamName}}');
        {{/isKeyInQuery}}
        {{#isKeyInCookie}}
        // Set key with prefix in cookies
        $security{{name}} = $request->cookies->get('{{keyParamName}}');
        {{/isKeyInCookie}}
        {{/isApiKey}}
        {{#isBasic}}
        // HTTP basic authentication required
        $security{{name}} = $request->headers->get('authorization');
        {{/isBasic}}
        {{#isOAuth}}
        // Oauth required
        $security{{name}} = $request->headers->get('authorization');
        {{/isOAuth}}
        {{/authMethods}}

        // Read out all input parameter values into variables
        {{#queryParams}}
        ${{paramName}} = $request->query->get('{{paramName}}');
        {{/queryParams}}
        {{#headerParams}}
        ${{paramName}} = $request->headers->get('{{baseName}}');
        {{/headerParams}}
        {{#formParams}}
        {{#isFile}}
        ${{paramName}} = $request->files->get('{{paramName}}');
        {{/isFile}}
        {{^isFile}}
        ${{paramName}} = $request->request->get('{{paramName}}');
        {{/isFile}}
        {{/formParams}}
        {{#bodyParams}}
        ${{paramName}} = $request->getContent();
        {{/bodyParams}}

        // Use the default value if no value was provided
        {{^required}}
        {{#isContainer}}
        {{#items}}
        {{#defaultValue}}
        ${{paramName}} = ${{paramName}}?:[{{{defaultValue}}}];
        {{/defaultValue}}
        {{/items}}
        {{/isContainer}}
        {{^isContainer}}
        {{#defaultValue}}
        ${{paramName}} = ${{paramName}}?:{{{defaultValue}}};
        {{/defaultValue}}
        {{/isContainer}}
        {{/required}}

        {{#hasParams}}
        // Deserialize the input values that needs it
        try {
            {{#allParams}}
            {{^isFile}}
            {{#isBodyParam}}
            $inputFormat = $request->getMimeType($request->getContentType());
            ${{paramName}} = $this->deserialize(${{paramName}}, '{{#isContainer}}{{#items}}array<{{dataType}}>{{/items}}{{/isContainer}}{{^isContainer}}{{dataType}}{{/isContainer}}', $inputFormat);
            {{/isBodyParam}}
            {{^isBodyParam}}
            ${{paramName}} = $this->deserialize(${{paramName}}, '{{#isContainer}}array<{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}{{^collectionFormat}}csv{{/collectionFormat}},{{dataType}}>{{/isContainer}}{{^isContainer}}{{dataType}}{{/isContainer}}', 'string');
            {{/isBodyParam}}
            {{/isFile}}
            {{/allParams}}
        } catch (SerializerRuntimeException $exception) {
            return $this->createBadRequestResponse($exception->getMessage());
        }

        {{/hasParams}}
        // Validate the input values
{{>api_input_validation}}

        try {
            $handler = $this->getApiHandler();

            {{#authMethods}}
            // Set authentication method '{{name}}'
            $handler->set{{name}}($security{{name}});
            {{/authMethods}}
            
            // Make the call to the business logic
            $responseCode = {{#returnType}}200{{/returnType}}{{^returnType}}204{{/returnType}};
            $responseHeaders = [];
            $result = $handler->{{operationId}}({{#allParams}}${{paramName}}, {{/allParams}}$responseCode, $responseHeaders);

            // Find default response message
            $message = '{{#responses}}{{#isDefault}}{{message}}{{/isDefault}}{{/responses}}';

            // Find a more specific message, if available
            switch ($responseCode) {
            {{#responses}}
                case {{code}}:
                    $message = '{{message}}';
                    break;
            {{/responses}}
            }

            return new Response(
                {{#returnType}}$result !== null ?$this->serialize($result, $responseFormat):''{{/returnType}}{{^returnType}}''{{/returnType}},
                $responseCode,
                array_merge(
                    $responseHeaders,
                    [
                        {{#returnType}}
                        'Content-Type' => $responseFormat,
                        {{/returnType}}
                        'X-OpenAPI-Message' => $message
                    ]
                )
            );
        } catch (Exception $fallthrough) {
            return $this->createErrorResponse(new HttpException(500, 'An unsuspected error occurred.', $fallthrough));
        }
    }
    {{/operation}}

    /**
     * Returns the handler for this API controller.
     * @return {{classname}}
     */
    public function getApiHandler()
    {
        return $this->apiServer->getApiHandler('{{pathPrefix}}');
    }
}
{{/operations}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy