<?php

{{>licenseInfo}}

declare(strict_types=1);

/**
 * NOTE: This class is auto generated by the openapi generator program.
 * https://github.com/openapitools/openapi-generator
 * Do not edit the class manually.
 */{{#apiInfo}}
namespace {{appPackage}};

use Slim\Exception\HttpNotImplementedException;

/**
 * RegisterRoutes Class Doc Comment
 *
 * @package {{invokerPackage}}
 * @author  OpenAPI Generator team
 * @link    https://github.com/openapitools/openapi-generator
 */
class RegisterRoutes
{
    /** @var array[] list of all api operations */
    private $operations = [
        {{#apis}}
        {{#operations}}
        {{#operation}}
        [
            'httpMethod' => '{{httpMethod}}',
            'basePathWithoutHost' => '{{{basePathWithoutHost}}}',
            'path' => '{{{path}}}',
            'apiPackage' => '{{apiPackage}}',
            'classname' => '{{classname}}',
            'userClassname' => '{{userClassname}}',
            'operationId' => '{{operationId}}',
            'responses' => [
            {{#responses}}
                '{{#isDefault}}default{{/isDefault}}{{^isDefault}}{{code}}{{/isDefault}}' => [
                    'jsonSchema' => '{{{jsonSchema}}}',
                ],
            {{/responses}}
            ],
            'authMethods' => [
                {{#hasAuthMethods}}
                {{#authMethods}}
                // {{type}} security schema named '{{name}}'
                {{#isBasicBasic}}
                [
                    'type' => '{{type}}',
                    'isBasic' => true,
                    'isBearer' => false,
                    'isApiKey' => false,
                    'isOAuth' => false,
                ],
                {{/isBasicBasic}}
                {{#isBasicBearer}}
                [
                    'type' => '{{type}}',
                    'isBasic' => true,
                    'isBearer' => true,
                    'isApiKey' => false,
                    'isOAuth' => false,
                ],
                {{/isBasicBearer}}
                {{#isApiKey}}
                [
                    'type' => '{{type}}',
                    'isBasic' => false,
                    'isBearer' => false,
                    'isApiKey' => true,
                    'isOAuth' => false,
                    'keyParamName' => '{{keyParamName}}',
                    'isKeyInHeader' => {{#isKeyInHeader}}true{{/isKeyInHeader}}{{^isKeyInHeader}}false{{/isKeyInHeader}},
                    'isKeyInQuery' => {{#isKeyInQuery}}true{{/isKeyInQuery}}{{^isKeyInQuery}}false{{/isKeyInQuery}},
                    'isKeyInCookie' => {{#isKeyInCookie}}true{{/isKeyInCookie}}{{^isKeyInCookie}}false{{/isKeyInCookie}},
                ],
                {{/isApiKey}}
                {{#isOAuth}}
                [
                    'type' => '{{type}}',
                    'isBasic' => false,
                    'isBearer' => false,
                    'isApiKey' => false,
                    'isOAuth' => true,
                    'scopes' => [
                    {{#scopes}}
                        '{{scope}}',{{#description}} // {{.}}{{/description}}
                    {{/scopes}}
                    ],
                ],
                {{/isOAuth}}
                {{/authMethods}}
                {{/hasAuthMethods}}
            ],
        ],
        {{/operation}}
        {{/operations}}
        {{/apis}}
    ];

    /**
     * Add routes to Slim app.
     *
     * @param \Slim\App $app Pre-configured Slim application instance
     *
     * @throws HttpNotImplementedException When implementation class doesn't exists
     */
    public function __invoke(\Slim\App $app): void
    {
        foreach ($this->operations as $operation) {
            $callback = function ($request) use ($operation) {
                $message = "How about extending {$operation['classname']} by {$operation['apiPackage']}\\{$operation['userClassname']} class implementing {$operation['operationId']} as a {$operation['httpMethod']} method?";
                throw new HttpNotImplementedException($request, $message);
            };
            $middlewares = [];

            if (class_exists("\\{$operation['apiPackage']}\\{$operation['userClassname']}")) {
                // Notice how we register the controller using the class name?
                // PHP-DI will instantiate the class for us only when it's actually necessary
                $callback = ["\\{$operation['apiPackage']}\\{$operation['userClassname']}", $operation['operationId']];
            }

            $route = $app->map(
                [$operation['httpMethod']],
                "{$operation['basePathWithoutHost']}{$operation['path']}",
                $callback
            )->setName($operation['operationId']);

            foreach ($middlewares as $middleware) {
                $route->add($middleware);
            }
        }
    }
}
{{/apiInfo}}
