Phalcon Controller

<?php
/**
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 *
 * Encoding: UTF-8
 *
 * @package CesarEngineExample\Api\Controller
 * @author cesar romero - 2015-10-23 15:40:40
 */

namespace CesarEngineExample\Api\Controller;

use Cesar\Exception\Api\AP0021;
use Cesar\Mvc\RepositoryLocator;
use Cesar\Mvc\ServiceLocator;

/**
 * Api Module's Game controller
 *
 * @RoutePrefix("/api/game")
 *
 * @package CesarEngineExample\Api\Controller
 * @uses \CesarEngineExample\Api\ControllerRestBase
 */
class GameController extends ControllerRestBase
{
    /**
     * Game Service
     *
     * @var \CesarEngineExample\Service\Game
     */
    protected $service;

    /**
     * Initialize method for controller
     *
     * @ignore
     */
    public function initialize()
    {
        $this->service = ServiceLocator::getService('Game');
    }

    /**
     * List Games
     *
     * @SWG\Get(
     *   path="/api/game",
     *   tags={"API Module","System Scope","Merchant Scope"},
     *   summary="List Games",
     *   description="Returns a list of Games matching the search criteria.",
     *   operationId="api.game.list",
     *   @SWG\Parameter(ref="#/parameters/Game\id"),
     *   @SWG\Parameter(ref="#/parameters/Game\provider_id"),
     *   @SWG\Parameter(ref="#/parameters/Game\brand_id"),
     *   @SWG\Parameter(ref="#/parameters/Game\category_id"),
     *   @SWG\Parameter(ref="#/parameters/Game\name"),
     *   @SWG\Parameter(ref="#/parameters/Game\active"),
     *   @SWG\Parameter(ref="#/parameters/Game\locked"),
     *   @SWG\Parameter(ref="#/parameters/Game\assigned"),
     *   @SWG\Parameter(ref="#/parameters/Game\activated_by_merchant"),
     *   @SWG\Parameter(ref="#/parameters/Global\created_at"),
     *   @SWG\Parameter(ref="#/parameters/Global\updated_at"),
     *   @SWG\Parameter(ref="#/parameters/QP\search"),
     *   @SWG\Parameter(ref="#/parameters/QP\cols"),
     *   @SWG\Parameter(ref="#/parameters/QP\sort"),
     *   @SWG\Parameter(ref="#/parameters/QP\limit"),
     *   @SWG\Parameter(ref="#/parameters/QP\page"),
     *   @SWG\Parameter(ref="#/parameters/QP\offset"),
     *   @SWG\Response(
     *     response="200",
     *     description="Array of Resources matching the search criteria.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Game\List")
     *   )
     * )
     *
     * @Acl(
     *   map="api/game/get"
     * )
     *
     * @Get(
     *   "/",
     *   name="api.game.list"
     * )
     */
    public function listAction()
    {
        try {
            return $this->sendJsonContent($this->service->apiList());
        } catch (\Exception $e) {
            return $this->processJsonExceptions($e);
        }
    }

    /**
     * Get Game
     *
     * @SWG\Get(
     *   path="/api/game/{gameId}",
     *   tags={"API Module","System Scope","Merchant Scope"},
     *   summary="Get Game by ID",
     *   description="Returns a Game by ID.",
     *   operationId="api.game.get",
     *   @SWG\Parameter(
     *     name="gameId",
     *     in="path",
     *     description="Game ID.",
     *     type="integer",
     *     format="int32",
     *     required=true
     *   ),
     *   @SWG\Response(
     *     response="200",
     *     description="The requested Resource.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Game\GetWithAttributes")
     *   ),
     *   @SWG\Response(
     *     response="404",
     *     description="Invalid Resource ID specified.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\NotFound")
     *   )
     * )
     *
     * @Acl(
     *   operation="read",
     *   description="Allows to read Games."
     * )
     *
     * @Get(
     *   "/{gameId:[0-9]+}",
     *   name="api.game.get"
     * )
     */
    public function getAction()
    {
        try {
            $id = $this->dispatcher->getParam('gameId');
            return $this->sendJsonContent(
                $this->service->apiGetWithAttributesById($id),
                (int) $this->dispatcher->getParam('statusCode', null, 200)
            );
        } catch (\Exception $e) {
            return $this->processJsonExceptions($e);
        }
    }

    /**
     * Game options
     *
     * @SWG\Options(
     *   path="/api/game/{gameId}",
     *   tags={"API Module","System Scope"},
     *   summary="Get endpoint options",
     *   description="Get possible operations over the endpoint.",
     *   operationId="api.game.get.options",
     *   @SWG\Parameter(
     *     name="gameId",
     *     in="path",
     *     description="Game ID.",
     *     type="integer",
     *     format="int32",
     *     required=true
     *   ),
     *   @SWG\Response(
     *     response="200",
     *     description="Endpoint options."
     *   )
     * )
     *
     * @Acl(
     *   scope="SYSTEM",
     *   map="api/game/create"
     * )
     *
     * @Options(
     *   "/{gameId:[0-9]+}",
     *   name="api.game.get.options"
     * )
     */
    public function getOptionsAction()
    {
        try {
            $id = $this->dispatcher->getParam('gameId');

            /** @var \CesarEngineExample\Service\GameAttributeType $service */
            $service = ServiceLocator::getService('GameAttributeType');

            $data = $this->view
                ->enable()
                ->getRender('game', 'get-options', array(
                    'attribs' => $service->apiListByGameIdAndLevel($id, 'Game')['result'],
                ));

            return $this->response
                ->setHeader('Allow', 'DELETE,GET,OPTIONS,PUT')
                ->setStatusCode(200, 'OK')
                ->setContentType('application/json', 'utf-8')
                ->setHeader('Content-Length', strlen($data))
                ->setContent($data);
        } catch (\Exception $e) {
            return $this->processJsonExceptions($e);
        }
    }

    /**
     * Create new Game
     *
     * @SWG\Post(
     *   path="/api/game",
     *   tags={"API Module","System Scope"},
     *   summary="Create new Game",
     *   description="Creates a new Game in the Application.",
     *   operationId="api.game.create",
     *   @SWG\Parameter(
     *     name="body",
     *     in="body",
     *     description="Game that needs to be added to the Application.",
     *     required=true,
     *     @SWG\Schema(ref="#/definitions/Game\Post")
     *   ),
     *   @SWG\Response(
     *     response="201",
     *     description="The newly created Resource.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Game\GetWithAttributes")
     *   ),
     *   @SWG\Response(
     *     response="400",
     *     description="Bad Request.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\BadRequestCreate")
     *   ),
     *   @SWG\Response(
     *     response="403",
     *     description="Forbidden.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\LockedResource")
     *   )
     * )
     *
     * @Acl(
     *   scope="SYSTEM",
     *   description="Creates a new Game in the Application."
     * )
     *
     * @Post(
     *   "/",
     *   name="api.game.create"
     * )
     */
    public function createAction()
    {
        try {
            $post = $this->getPost(true);
            if (empty($post['provider_id']) && 0 !== ($providerId = (int) $this->dispatcher->getParam('provider'))) {
                $post['provider_id'] = $providerId;
            }
            $model = $this->service->apiCreate($post);
            $this->dispatcher->forward(array(
                'action' => 'get',
                'params' => array(
                    'gameId' => $model->getId(),
                    'statusCode' => 201,
                ),
            ));
            return null;
        } catch (\Exception $e) {
            return $this->processJsonExceptions($e);
        }
    }

    /**
     * Update a Game
     *
     * @SWG\Put(
     *   path="/api/game/{gameId}",
     *   tags={"API Module","System Scope"},
     *   summary="Update Game",
     *   description="Updates an existing Game.",
     *   operationId="api.game.update",
     *   @SWG\Parameter(
     *     name="gameId",
     *     in="path",
     *     description="Game ID.",
     *     type="integer",
     *     format="int32",
     *     required=true
     *   ),
     *   @SWG\Parameter(
     *     name="body",
     *     in="body",
     *     description="List of properties to update.",
     *     required=true,
     *     @SWG\Schema(ref="#/definitions/Game\Put")
     *   ),
     *   @SWG\Response(
     *     response="200",
     *     description="The updated Resource.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Game\GetWithAttributes")
     *   ),
     *   @SWG\Response(
     *     response="400",
     *     description="Bad Request.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\BadRequestUpdate")
     *   ),
     *   @SWG\Response(
     *     response="403",
     *     description="Forbidden.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\LockedResource")
     *   ),
     *   @SWG\Response(
     *     response="404",
     *     description="Invalid Resource ID specified.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\NotFound")
     *   )
     * )
     *
     * @Acl(
     *   scope="SYSTEM",
     *   description="Updates an existing Game."
     * )
     *
     * @Put(
     *   "/{gameId:[0-9]+}",
     *   name="api.game.update"
     * )
     */
    public function updateAction()
    {
        try {
            $id = $this->dispatcher->getParam('gameId');
            $model = $this->service->apiUpdate($id, $this->getPut(true));
            $this->dispatcher->forward(array(
                'action' => 'get',
                'params' => array(
                    'gameId' => $model->getId(),
                ),
            ));
            return null;
        } catch (\Exception $e) {
            return $this->processJsonExceptions($e);
        }
    }

    /**
     * Toggle active status of a Game
     *
     * @SWG\Put(
     *   path="/api/game/{gameId}/toggle-active",
     *   tags={"API Module","System Scope"},
     *   summary="Toggle active status of a Game",
     *   description="Enables or disables the Game in the Application. Disabled
           Games are excluded for all Merchants.",
     *   operationId="api.game.toggle-active",
     *   @SWG\Parameter(
     *     name="gameId",
     *     in="path",
     *     description="Game ID.",
     *     type="integer",
     *     format="int32",
     *     required=true
     *   ),
     *   @SWG\Parameter(
     *     name="body",
     *     in="body",
     *     description="Flag indicating the new status.",
     *     required=true,
     *     @SWG\Schema(ref="#/definitions/Api\Request\ToggleActive")
     *   ),
     *   @SWG\Response(
     *     response="200",
     *     description="The requested Resource.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Game\GetWithAttributes")
     *   ),
     *   @SWG\Response(
     *     response="400",
     *     description="Bad Request.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\BadRequestUpdate")
     *   ),
     *   @SWG\Response(
     *     response="403",
     *     description="Forbidden.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\LockedResource")
     *   ),
     *   @SWG\Response(
     *     response="404",
     *     description="Invalid Resource ID specified.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\NotFound")
     *   )
     * )
     *
     * @Acl(
     *   scope="SYSTEM",
     *   operation="enable",
     *   description="Enables or disables the Game in the Application. Disabled
     *     Games are excluded for all Merchants."
     * )
     *
     * @Put(
     *   "/{gameId:[0-9]+}/toggle-active",
     *   name="api.game.toggle-active"
     * )
     */
    public function toggleActiveAction()
    {
        try {
            $id = $this->dispatcher->getParam('gameId');
            $model = $this->service->apiToggleActive($id, $this->getPut(true));
            $this->dispatcher->forward(array(
                'action' => 'get',
                'params' => array(
                    'gameId' => $model->getId(),
                ),
            ));
            return null;
        } catch (\Exception $e) {
            return $this->processJsonExceptions($e);
        }
    }

    /**
     * Toggle locked status of a Game
     *
     * @SWG\Put(
     *   path="/api/game/{gameId}/toggle-locked",
     *   tags={"API Module","System Scope"},
     *   summary="Toggle locked status of a Game",
     *   description="Locks or unlocks the Game in the Application. Locked Games cannot be modified.",
     *   operationId="api.game.toggle-locked",
     *   @SWG\Parameter(
     *     name="gameId",
     *     in="path",
     *     description="Game ID.",
     *     type="integer",
     *     format="int32",
     *     required=true
     *   ),
     *   @SWG\Parameter(
     *     name="body",
     *     in="body",
     *     description="Flag indicating the new status.",
     *     required=true,
     *     @SWG\Schema(ref="#/definitions/Api\Request\ToggleLocked")
     *   ),
     *   @SWG\Response(
     *     response="200",
     *     description="The requested Resource.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Game\GetWithAttributes")
     *   ),
     *   @SWG\Response(
     *     response="400",
     *     description="Bad Request.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\BadRequestUpdate")
     *   ),
     *   @SWG\Response(
     *     response="404",
     *     description="Invalid Resource ID specified.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\NotFound")
     *   )
     * )
     *
     * @Acl(
     *   scope="SYSTEM",
     *   operation="lock",
     *   description="Locks or unlocks the Game in the Application. Locked Games cannot be modified."
     * )
     *
     * @Put(
     *   "/{gameId:[0-9]+}/toggle-locked",
     *   name="api.game.toggle-locked"
     * )
     */
    public function toggleLockedAction()
    {
        try {
            $id = $this->dispatcher->getParam('gameId');
            $model = $this->service->apiToggleLocked($id, $this->getPut(true));
            $this->dispatcher->forward(array(
                'action' => 'get',
                'params' => array(
                    'gameId' => $model->getId(),
                ),
            ));
            return null;
        } catch (\Exception $e) {
            return $this->processJsonExceptions($e);
        }
    }

    /**
     * Delete a Game
     *
     * @SWG\Delete(
     *   path="/api/game/{gameId}",
     *   tags={"API Module","System Scope"},
     *   summary="Delete Game",
     *   description="Deletes an existing Game.",
     *   operationId="api.game.delete",
     *   @SWG\Parameter(
     *     name="gameId",
     *     in="path",
     *     description="Game ID.",
     *     type="integer",
     *     format="int32",
     *     required=true
     *   ),
     *   @SWG\Response(
     *     response="200",
     *     description="Successful operation.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Simple")
     *   ),
     *   @SWG\Response(
     *     response="400",
     *     description="Bad Request.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\BadRequestDelete")
     *   ),
     *   @SWG\Response(
     *     response="403",
     *     description="Forbidden.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\LockedResource")
     *   ),
     *   @SWG\Response(
     *     response="404",
     *     description="Invalid Resource ID specified.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\NotFound")
     *   )
     * )
     *
     * @Acl(
     *   scope="SYSTEM",
     *   description="Deletes an existing Game."
     * )
     *
     * @Delete(
     *   "/{gameId:[0-9]+}",
     *   name="api.game.delete"
     * )
     */
    public function deleteAction()
    {
        try {
            $id = $this->dispatcher->getParam('gameId');
            $this->service->apiDelete($id);
            return $this->sendJsonContent(array());
        } catch (\Exception $e) {
            return $this->processJsonExceptions($e);
        }
    }

    /**
     * List Audits of the Game
     *
     * Contains Business logic because of the redirection. Don't try to repeat it at home.
     *
     * @SWG\Get(
     *   path="/api/game/{gameId}/audit",
     *   tags={"API Module","System Scope"},
     *   summary="List Audits of a Game",
     *   description="Returns a list of Audits belonging to a Game and matching the search criteria.",
     *   operationId="api.game.audit.list",
     *   @SWG\Parameter(
     *     name="gameId",
     *     in="path",
     *     description="Game ID.",
     *     type="integer",
     *     format="int32",
     *     required=true
     *   ),
     *   @SWG\Parameter(ref="#/parameters/Audit\id"),
     *   @SWG\Parameter(ref="#/parameters/Audit\user_id"),
     *   @SWG\Parameter(ref="#/parameters/Audit\type"),
     *   @SWG\Parameter(ref="#/parameters/Global\created_at"),
     *   @SWG\Parameter(ref="#/parameters/QP\search"),
     *   @SWG\Parameter(ref="#/parameters/QP\cols"),
     *   @SWG\Parameter(ref="#/parameters/QP\sort"),
     *   @SWG\Parameter(ref="#/parameters/QP\limit"),
     *   @SWG\Parameter(ref="#/parameters/QP\page"),
     *   @SWG\Parameter(ref="#/parameters/QP\offset"),
     *   @SWG\Response(
     *     response="200",
     *     description="Array of Resources matching the search criteria.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Audit\ListByModel")
     *   ),
     *   @SWG\Response(
     *     response="404",
     *     description="Invalid Resource ID specified.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\NotFound")
     *   )
     * )
     *
     * @Acl(
     *   scope="SYSTEM",
     *   map="api/game/get"
     * )
     *
     * @Get(
     *   "/{gameId:[0-9]+}/audit",
     *   name="api.game.audit.list"
     * )
     */
    public function listAuditAction()
    {
        try {
            if (0 === ($id = (int) $this->dispatcher->getParam('gameId'))) {
                throw new AP0021('Invalid Game specified.');
            }

            if (false === ($model = $this->getModel($id))) {
                $params = array(
                    'modelName' => 'Game',
                    'modelId' => $id,
                );
            } else {
                $params = array(
                    'model' => $model,
                );
            }
            $this->dispatcher->forward(array(
                'controller' => 'audit',
                'action' => 'list',
                'params' => $params,
            ));
            return null;
        } catch (\Exception $e) {
            return $this->processJsonExceptions($e);
        }
    }

    /**
     * Get Audit of the Game
     *
     * Contains Business logic because of the redirection. Don't try to repeat it at home.
     *
     * @SWG\Get(
     *   path="/api/game/{gameId}/audit/{auditId}",
     *   tags={"API Module","System Scope"},
     *   summary="Get Audit by ID",
     *   description="Returns an Audit belonging to the Game by ID.",
     *   operationId="api.game.audit.get",
     *   @SWG\Parameter(
     *     name="gameId",
     *     in="path",
     *     description="Game ID.",
     *     type="integer",
     *     format="int32",
     *     required=true
     *   ),
     *   @SWG\Parameter(
     *     name="auditId",
     *     in="path",
     *     description="Audit ID.",
     *     type="integer",
     *     format="int32",
     *     required=true
     *   ),
     *   @SWG\Response(
     *     response="200",
     *     description="The requested Resource.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Audit\GetByModel")
     *   ),
     *   @SWG\Response(
     *     response="404",
     *     description="Invalid Resource ID specified.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\NotFound")
     *   )
     * )
     *
     * @Acl(
     *   scope="SYSTEM",
     *   map="api/game/get"
     * )
     *
     * @Get(
     *   "/{gameId:[0-9]+}/audit/{auditId:[0-9]+}",
     *   name="api.game.audit.get"
     * )
     */
    public function getAuditAction()
    {
        try {
            if (0 === ($id = (int) $this->dispatcher->getParam('gameId'))) {
                throw new AP0021('Invalid Game specified.');
            }

            if (false === ($model = $this->getModel($id))) {
                $params = array(
                    'modelName' => 'Game',
                    'modelId' => $id,
                    'auditId' => $this->dispatcher->getParam('auditId'),
                );
            } else {
                $params = array(
                    'model' => $model,
                    'auditId' => $this->dispatcher->getParam('auditId'),
                );
            }
            $this->dispatcher->forward(array(
                'controller' => 'audit',
                'action' => 'get',
                'params' => $params,
            ));
            return null;
        } catch (\Exception $e) {
            return $this->processJsonExceptions($e);
        }
    }

    /**
     * List Attributes of the Game
     *
     * @SWG\Get(
     *   path="/api/game/{gameId}/game-attribute",
     *   tags={"API Module","System Scope","Merchant Scope"},
     *   summary="List Game Attributes of a Game",
     *   description="Returns a list of Game Attributes belonging to a Game and matching the search criteria.",
     *   operationId="api.game.game-attribute.list",
     *   @SWG\Parameter(
     *     name="gameId",
     *     in="path",
     *     description="Game ID.",
     *     type="integer",
     *     format="int32",
     *     required=true
     *   ),
     *   @SWG\Parameter(ref="#/parameters/GameAttribute\id"),
     *   @SWG\Parameter(ref="#/parameters/GameAttribute\attribute_type_id"),
     *   @SWG\Parameter(ref="#/parameters/GameAttributeType\name"),
     *   @SWG\Parameter(ref="#/parameters/GameAttributeType\nullable"),
     *   @SWG\Parameter(ref="#/parameters/GameAttributeType\mandatory"),
     *   @SWG\Parameter(ref="#/parameters/GameAttributeType\visible"),
     *   @SWG\Parameter(ref="#/parameters/Global\created_at"),
     *   @SWG\Parameter(ref="#/parameters/Global\updated_at"),
     *   @SWG\Parameter(ref="#/parameters/QP\search"),
     *   @SWG\Parameter(ref="#/parameters/QP\cols"),
     *   @SWG\Parameter(ref="#/parameters/QP\sort"),
     *   @SWG\Parameter(ref="#/parameters/QP\limit"),
     *   @SWG\Parameter(ref="#/parameters/QP\page"),
     *   @SWG\Parameter(ref="#/parameters/QP\offset"),
     *   @SWG\Response(
     *     response="200",
     *     description="Array of Resources matching the search criteria.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\GameAttribute\ListByGame")
     *   ),
     *   @SWG\Response(
     *     response="404",
     *     description="Invalid Resource ID specified.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\NotFound")
     *   )
     * )
     *
     * @Acl(
     *   map="api/game/get"
     * )
     *
     * @Get(
     *   "/{gameId:[0-9]+}/game-attribute",
     *   name="api.game.game-attribute.list"
     * )
     */
    public function listAttributeAction()
    {
        try {
            $id = $this->dispatcher->getParam('gameId');

            /** @var \CesarEngineExample\Service\GameAttribute $service */
            $service = ServiceLocator::getService('GameAttribute');
            return $this->sendJsonContent($service->apiListWithTypesByGameId($id, true));
        } catch (\Exception $e) {
            return $this->processJsonExceptions($e);
        }
    }

    /**
     * Get Attribute of the Game
     *
     * @SWG\Get(
     *   path="/api/game/{gameId}/game-attribute/{attributeId}",
     *   tags={"API Module","System Scope","Merchant Scope"},
     *   summary="Get Game Attribute by ID",
     *   description="Returns a Game Attribute belonging to the Game by ID.",
     *   operationId="api.game.game-attribute.get",
     *   @SWG\Parameter(
     *     name="gameId",
     *     in="path",
     *     description="Game ID.",
     *     type="integer",
     *     format="int32",
     *     required=true
     *   ),
     *   @SWG\Parameter(
     *     name="attributeId",
     *     in="path",
     *     description="Game Attribute ID.",
     *     type="integer",
     *     format="int32",
     *     required=true
     *   ),
     *   @SWG\Response(
     *     response="200",
     *     description="The requested Resource.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\GameAttribute\GetByGame")
     *   ),
     *   @SWG\Response(
     *     response="404",
     *     description="Invalid Resource ID specified.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\NotFound")
     *   )
     * )
     *
     * @Acl(
     *   map="api/game/get"
     * )
     *
     * @Get(
     *   "/{gameId:[0-9]+}/game-attribute/{attributeId:[0-9]+}",
     *   name="api.game.game-attribute.get"
     * )
     */
    public function getAttributeAction()
    {
        try {
            $id = $this->dispatcher->getParam('gameId');
            $rid = $this->dispatcher->getParam('attributeId');

            /** @var \CesarEngineExample\Service\GameAttribute $service */
            $service = ServiceLocator::getService('GameAttribute');
            return $this->sendJsonContent($service->apiGetByIdAndGameId($rid, $id));
        } catch (\Exception $e) {
            return $this->processJsonExceptions($e);
        }
    }

    /**
     * Free Spin options
     *
     * @SWG\Options(
     *   path="/api/game/{gameId}/freespin",
     *   tags={"API Module","System Scope","Merchant Scope"},
     *   summary="Get Free Spin options",
     *   description="Get possible operations over the Free Spin endpoints.",
     *   operationId="api.game.freespin.options",
     *   @SWG\Parameter(
     *     name="gameId",
     *     in="path",
     *     description="Game ID.",
     *     type="integer",
     *     format="int32",
     *     required=true
     *   ),
     *   @SWG\Response(
     *     response="200",
     *     description="Endpoint options."
     *   ),
     *   @SWG\Response(
     *     response="404",
     *     description="Invalid Resource ID specified.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\NotFound")
     *   )
     * )
     *
     * @Acl(
     *   map="api/freespin/create"
     * )
     *
     * @Options(
     *   "/{gameId:[0-9]+}/freespin",
     *   name="api.game.freespin.options"
     * )
     */
    public function freespinOptionsAction()
    {
        try {
            $id = $this->dispatcher->getParam('gameId');

            /** @var \CesarEngineExample\Service\FreeSpin $service */
            $service = ServiceLocator::getService('FreeSpin');

            $data = $this->view
                ->enable()
                ->getRender('game', 'freespin-options', array(
                    'attribs' => $service->apiGetParametersListByGameId($id),
                ));

            return $this->response
                ->setHeader('Allow', 'OPTIONS')
                ->setStatusCode(200, 'OK')
                ->setContentType('application/json', 'utf-8')
                ->setHeader('Content-Length', strlen($data))
                ->setContent($data);
        } catch (\Exception $e) {
            return $this->processJsonExceptions($e);
        }
    }

    /**
     * List Game Configurations of the Game
     *
     * @SWG\Get(
     *   path="/api/game/{gameId}/game-configuration",
     *   tags={"API Module","System Scope","Merchant Scope"},
     *   summary="List Game Configurations of a Game",
     *   description="Returns a list of Game Configurations belonging to a Game and matching the search criteria.",
     *   operationId="api.game.game-configuration.list",
     *   @SWG\Parameter(
     *     name="gameId",
     *     in="path",
     *     description="Game ID.",
     *     type="integer",
     *     format="int32",
     *     required=true
     *   ),
     *   @SWG\Parameter(ref="#/parameters/GameConfiguration\id"),
     *   @SWG\Parameter(ref="#/parameters/GameConfiguration\technology_id"),
     *   @SWG\Parameter(ref="#/parameters/GameConfiguration\name"),
     *   @SWG\Parameter(ref="#/parameters/GameConfiguration\active"),
     *   @SWG\Parameter(ref="#/parameters/GameConfiguration\locked"),
     *   @SWG\Parameter(ref="#/parameters/Global\created_at"),
     *   @SWG\Parameter(ref="#/parameters/Global\updated_at"),
     *   @SWG\Parameter(ref="#/parameters/QP\search"),
     *   @SWG\Parameter(ref="#/parameters/QP\cols"),
     *   @SWG\Parameter(ref="#/parameters/QP\sort"),
     *   @SWG\Parameter(ref="#/parameters/QP\limit"),
     *   @SWG\Parameter(ref="#/parameters/QP\page"),
     *   @SWG\Parameter(ref="#/parameters/QP\offset"),
     *   @SWG\Response(
     *     response="200",
     *     description="Array of Resources matching the search criteria.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\GameConfiguration\ListByGame")
     *   ),
     *   @SWG\Response(
     *     response="404",
     *     description="Invalid Resource ID specified.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\NotFound")
     *   )
     * )
     *
     * @Acl(
     *   map="api/game_configuration/get"
     * )
     *
     * @Get(
     *   "/{gameId:[0-9]+}/game-configuration",
     *   name="api.game.game-configuration.list"
     * )
     */
    public function listGameConfigurationAction()
    {
        try {
            $id = $this->dispatcher->getParam('gameId');

            /** @var \CesarEngineExample\Service\GameConfiguration $service */
            $service = ServiceLocator::getService('GameConfiguration');
            return $this->sendJsonContent($service->apiListByGameId($id));
        } catch (\Exception $e) {
            return $this->processJsonExceptions($e);
        }
    }

    /**
     * Game Configurations options
     *
     * @SWG\Options(
     *   path="/api/game/{gameId}/game-configuration",
     *   tags={"API Module","System Scope"},
     *   summary="Get endpoint options",
     *   description="Get possible operations over the endpoint.",
     *   operationId="api.game.game-configuration.list.options",
     *   @SWG\Parameter(
     *     name="gameId",
     *     in="path",
     *     description="Game ID.",
     *     type="integer",
     *     format="int32",
     *     required=true
     *   ),
     *   @SWG\Response(
     *     response="200",
     *     description="Endpoint options."
     *   ),
     *   @SWG\Response(
     *     response="404",
     *     description="Invalid Resource ID specified.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\NotFound")
     *   )
     * )
     *
     * @Acl(
     *   scope="SYSTEM",
     *   map="api/game_configuration/create"
     * )
     *
     * @Options(
     *   "/{gameId:[0-9]+}/game-configuration",
     *   name="api.game.game-configuration.list.options"
     * )
     */
    public function listGameConfigurationOptionsAction()
    {
        try {
            $id = $this->dispatcher->getParam('gameId');

            /** @var \CesarEngineExample\Service\GameAttributeType $service */
            $service = ServiceLocator::getService('GameAttributeType');

            $data = $this->view
                ->enable()
                ->getRender('game', 'list-game-configuration-options', array(
                    'attribs' => $service->apiListByGameIdAndLevel($id, 'Game Configuration')['result'],
                ));

            return $this->response
                ->setHeader('Allow', 'GET,OPTIONS,POST')
                ->setStatusCode(200, 'OK')
                ->setContentType('application/json', 'utf-8')
                ->setHeader('Content-Length', strlen($data))
                ->setContent($data);
        } catch (\Exception $e) {
            return $this->processJsonExceptions($e);
        }
    }

    /**
     * Get Game Configuration of the Game
     *
     * @SWG\Get(
     *   path="/api/game/{gameId}/game-configuration/{configurationId}",
     *   tags={"API Module","System Scope","Merchant Scope"},
     *   summary="Get Game Configuration by ID",
     *   description="Returns a Game Configuration belonging to the Game by ID.",
     *   operationId="api.game.game-configuration.get",
     *   @SWG\Parameter(
     *     name="gameId",
     *     in="path",
     *     description="Game ID.",
     *     type="integer",
     *     format="int32",
     *     required=true
     *   ),
     *   @SWG\Parameter(
     *     name="configurationId",
     *     in="path",
     *     description="Game Configuration ID.",
     *     type="integer",
     *     format="int32",
     *     required=true
     *   ),
     *   @SWG\Response(
     *     response="200",
     *     description="The requested Resource.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\GameConfiguration\GetWithAttributesByGame")
     *   ),
     *   @SWG\Response(
     *     response="404",
     *     description="Invalid Resource ID specified.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\NotFound")
     *   )
     * )
     *
     * @Acl(
     *   map="api/game_configuration/get"
     * )
     *
     * @Get(
     *   "/{gameId:[0-9]+}/game-configuration/{configurationId:[0-9]+}",
     *   name="api.game.game-configuration.get"
     * )
     */
    public function getGameConfigurationAction()
    {
        try {
            $id = $this->dispatcher->getParam('gameId');
            $rid = $this->dispatcher->getParam('configurationId');

            /** @var \CesarEngineExample\Service\GameConfiguration $service */
            $service = ServiceLocator::getService('GameConfiguration');
            return $this->sendJsonContent($service->apiGetWithAttributesByIdAndGameId($rid, $id));
        } catch (\Exception $e) {
            return $this->processJsonExceptions($e);
        }
    }

    /**
     * Create new Game Configuration of the Game
     *
     * @SWG\Post(
     *   path="/api/game/{gameId}/game-configuration",
     *   tags={"API Module","System Scope"},
     *   summary="Create new Game Configuration for a Game",
     *   description="Creates a new Game Configuration for a Game in the Application.",
     *   operationId="api.game.game-configuration.create",
     *   @SWG\Parameter(
     *     name="gameId",
     *     in="path",
     *     description="Game ID.",
     *     type="integer",
     *     format="int32",
     *     required=true
     *   ),
     *   @SWG\Parameter(
     *     name="body",
     *     in="body",
     *     description="Game Configuration that needs to be added to the Application.",
     *     required=true,
     *     @SWG\Schema(ref="#/definitions/GameConfiguration\PostForGame")
     *   ),
     *   @SWG\Response(
     *     response="201",
     *     description="The newly created Resource.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\GameConfiguration\GetWithAttributes")
     *   ),
     *   @SWG\Response(
     *     response="400",
     *     description="Bad Request.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\BadRequestCreate")
     *   ),
     *   @SWG\Response(
     *     response="403",
     *     description="Forbidden.",
     *     @SWG\Schema(ref="#/definitions/Api\Response\Error\LockedResource")
     *   )
     * )
     *
     * @Acl(
     *   scope="SYSTEM",
     *   map="api/game_configuration/create"
     * )
     *
     * @Post(
     *   "/{gameId:[0-9]+}/game-configuration",
     *   name="api.game.game-configuration.create"
     * )
     */
    public function createGameConfigurationAction()
    {
        try {
            $this->dispatcher->forward(array(
                'controller' => 'game-configuration',
                'action' => 'create',
                'params' => array(
                    'game' => $this->dispatcher->getParam('gameId'),
                ),
            ));
            return null;
        } catch (\Exception $e) {
            return $this->processJsonExceptions($e);
        }
    }

    /**
     * Get Game entity
     *
     * @param integer $id
     * @return \CesarEngineExample\Entity\Game
     */
    protected function getModel($id)
    {
        /** @var \CesarEngineExample\Repository\Game $repo */
        $repo = RepositoryLocator::getRepository('Game');
        return $repo->apiGetById($id);
    }
}