. */ namespace SP\Modules\Api\Controllers; use Exception; use Klein\Klein; use League\Fractal\Manager; use SP\Core\Application; use SP\Core\Bootstrap\BootstrapBase; use SP\Domain\Api\Dtos\ApiResponse; use SP\Domain\Api\Ports\ApiService; use SP\Domain\Api\Services\JsonRpcResponse; use SP\Domain\Common\Services\ServiceException; use SP\Domain\Config\Ports\ConfigDataInterface; use SP\Domain\Core\Acl\AclInterface; use SP\Domain\Core\Context\Context; use SP\Domain\Core\Events\EventDispatcherInterface; use SP\Domain\Core\Exceptions\SPException; use SP\Domain\Http\Services\JsonResponse; /** * Class ControllerBase * * @package SP\Modules\Api\Controllers */ abstract class ControllerBase { protected const SEARCH_COUNT_ITEMS = 25; protected string $controllerName; protected Context $context; protected EventDispatcherInterface $eventDispatcher; protected ConfigDataInterface $configData; protected Manager $fractal; protected string $actionName; private bool $isAuthenticated = false; public function __construct( Application $application, protected readonly Klein $router, protected readonly ApiService $apiService, protected readonly AclInterface $acl ) { $this->context = $application->getContext(); $this->configData = $application->getConfig()->getConfigData(); $this->eventDispatcher = $application->getEventDispatcher(); $this->fractal = new Manager(); $this->controllerName = $this->getControllerName(); $this->actionName = $this->context->getTrasientKey(BootstrapBase::CONTEXT_ACTION_NAME); } final protected function getControllerName(): string { $class = static::class; return substr($class, strrpos($class, '\\') + 1, -strlen('Controller')) ?: ''; } /** * @throws SPException * @throws ServiceException */ final protected function setupApi(int $actionId): void { $this->apiService->setup($actionId); $this->isAuthenticated = true; } /** * Devuelve una respuesta en formato JSON con el estado y el mensaje. * * {"jsonrpc": "2.0", "result": 19, "id": 3} */ final protected function returnResponse(ApiResponse $apiResponse): void { try { if ($this->isAuthenticated === false) { throw new SPException(__u('Unauthorized access')); } $this->sendJsonResponse(JsonRpcResponse::getResponse($apiResponse, $this->apiService->getRequestId())); } catch (SPException $e) { processException($e); $this->returnResponseException($e); } } /** * Returns a JSON response back to the browser */ private function sendJsonResponse(string $response): void { JsonResponse::factory($this->router->response())->sendRaw($response); } final protected function returnResponseException(Exception $e): void { $this->sendJsonResponse(JsonRpcResponse::getResponseException($e, $this->apiService->getRequestId())); } }