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.8.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 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}}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}];
        $inputFormat = $request->headers->has('Content-Type')?$request->headers->get('Content-Type'):$consumes[0];
        if (!in_array($inputFormat, $consumes)) {
            // We can't consume the content that the client is sending us
            return new Response('', 415);
        }

        {{/bodyParams}}
        // Figure out what data format to return to the client
        $produces = [{{#produces}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/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);
        }

        // 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}}
        {{/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}}

        // Deserialize the input values that needs it
        {{#allParams}}
        {{^isFile}}
        {{#isBodyParam}}
        ${{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}}

        // 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(
                $result?$this->serialize($result, $responseFormat):'',
                $responseCode,
                array_merge(
                    $responseHeaders,
                    [
                        'Content-Type' => $responseFormat,
                        '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