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: 3.0.0-rc1
Show newest version
partial_header}}
/**
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen
 * 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 {{apiPackage}}\{{classname}};
{{#imports}}use {{this}};
{{/imports}}

/**
 * {{controllerName}} Class Doc Comment
 *
 * @category Class
 * @package  {{controllerPackage}}
 * @author   Swagger Codegen team
 * @link     https://github.com/swagger-api/swagger-codegen
 */
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)
    {
        {{#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}}
        {{#queryParams}}
        // Handle query params
        ${{paramName}} = $this->fromQuery($request->query->get('{{paramName}}'), '{{dataType}}');
        {{/queryParams}}
        {{#headerParams}}
        // Handle header params
        ${{paramName}} = $this->fromHeader($request->headers->get('{{paramName}}'), '{{dataType}}');
        {{/headerParams}}
        {{#pathParams}}
        // Handle path params
        ${{paramName}} = $this->fromPath($request->attributes->get('{{paramName}}'), '{{dataType}}');
        {{/pathParams}}
        {{#formParams}}
        {{#isFile}}
        // Handle file params
        ${{paramName}} = $request->files->get('{{paramName}}');
        {{/isFile}}
        {{^isFile}}
        // Handle form params
        ${{paramName}} = $this->fromForm($request->request->get('{{paramName}}'), '{{dataType}}');
        {{/isFile}}
        {{/formParams}}
        {{#bodyParams}}
        // Handle body params
        ${{paramName}} = $this->deserialize($request->getContent(), '{{{dataType}}}', 'json');
        {{/bodyParams}}

        // Parse incoming parameters
        {{#allParams}}
        {{#required}}
        // Verify the required parameter '{{paramName}}' is set
        if (${{paramName}} === null) {
            return $this->createBadRequestResponse('Missing the required parameter ${{paramName}} when calling {{operationId}}');
        }
        {{/required}}
        {{#hasValidation}}
        {{#maxLength}}
        if ({{^required}}!is_null(${{paramName}}) && {{/required}}(strlen(${{paramName}}) > {{maxLength}})) {
            return $this->createBadRequestResponse('Invalid length for "${{paramName}}" when calling {{classname}}.{{operationId}}, must be smaller than or equal to {{maxLength}}.');
        }
        {{/maxLength}}
        {{#minLength}}
        if ({{^required}}!is_null(${{paramName}}) && {{/required}}(strlen(${{paramName}}) < {{minLength}})) {
            return $this->createBadRequestResponse('Invalid length for "${{paramName}}" when calling {{classname}}.{{operationId}}, must be bigger than or equal to {{minLength}}.');
        }
        {{/minLength}}
        {{#maximum}}
        if ({{^required}}!is_null(${{paramName}}) && {{/required}}(${{paramName}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}})) {
            return $this->createBadRequestResponse('Invalid value for "${{paramName}}" when calling {{classname}}.{{operationId}}, must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}.');
        }
        {{/maximum}}
        {{#minimum}}
        if ({{^required}}!is_null(${{paramName}}) && {{/required}}(${{paramName}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}})) {
            return $this->createBadRequestResponse('Invalid value for "${{paramName}}" when calling {{classname}}.{{operationId}}, must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}.');
        }
        {{/minimum}}
        {{#pattern}}
        if ({{^required}}!is_null(${{paramName}}) && {{/required}}!preg_match("{{{pattern}}}", ${{paramName}})) {
            return $this->createBadRequestResponse("Invalid value for \"{{paramName}}\" when calling {{classname}}.{{operationId}}, must conform to the pattern {{{pattern}}}.");
        }
        {{/pattern}}
        {{#maxItems}}
        if ({{^required}}!is_null(${{paramName}}) && {{/required}}(count(${{paramName}}) > {{maxItems}})) {
            return $this->createBadRequestResponse('Invalid value for "${{paramName}}" when calling {{classname}}.{{operationId}}, number of items must be less than or equal to {{maxItems}}.');
        }
        {{/maxItems}}
        {{#minItems}}
        if ({{^required}}!is_null(${{paramName}}) && {{/required}}(count(${{paramName}}) < {{minItems}})) {
            return $this->createBadRequestResponse('Invalid value for "${{paramName}}" when calling {{classname}}.{{operationId}}, number of items must be greater than or equal to {{minItems}}.');
        }
        {{/minItems}}
        {{/hasValidation}}
        {{/allParams}}

        // Call the API interface
        try {
            $handler = $this->getApiHandler();

            {{#authMethods}}
            // Set authentication method '{{name}}'
            $handler->set{{name}}($security{{name}});
            {{/authMethods}}
            {{#returnType}}
            // Expecting a return value (exception otherwise)
            $result = $handler->{{operationId}}({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});

            {{#responses}}
            {{^vendorExtensions.x-symfonyExceptionSimple}}
            // Handle {{code}} response: {{message}}
            $content = $this->serialize($result, 'json');
            return new Response($content, {{code}}, [
                'Content-Type' => 'application/json',
                'X-Swagger-Message' => '{{message}}',
            ]);
            {{/vendorExtensions.x-symfonyExceptionSimple}}
            {{/responses}}
            {{/returnType}}
            {{^returnType}}
            // No return type expected; return empty response
            $handler->{{operationId}}({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
            return new Response('', 204);
            {{/returnType}}
        {{#responses}}
        {{#vendorExtensions.x-symfonyExceptionSimple}}
        } catch ({{vendorExtensions.x-symfonyExceptionSimple}} $exception) {
            // {{message}}
            return $this->createErrorResponse($exception);
        {{/vendorExtensions.x-symfonyExceptionSimple}}
        {{/responses}}
        } 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->get('{{bundleAlias}}.api.api_server')->getApiHandler('{{pathPrefix}}');
    }
}
{{/operations}}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy