From da1232c9286b40d87fcbe02a255c51534fa5fc39 Mon Sep 17 00:00:00 2001 From: cherednik Date: Thu, 9 Jan 2025 19:29:31 +0400 Subject: [PATCH] =?UTF-8?q?DD-3602:=20[BACK].=20[=D0=94=D0=BE=D0=B2=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BD=D0=BE=D1=81=D1=82=D1=8C=20=D0=B2=D0=BE?= =?UTF-8?q?=D0=B4=D0=B8=D1=82=D0=B5=D0=BB=D1=8E=20=D1=81=20=D0=9A=D0=AD?= =?UTF-8?q?=D0=9F]=20-=20[=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D1=82=D1=8C=20=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2=D0=BA?= =?UTF-8?q?=D1=83=20=D0=B4=D0=BE=D0=BA=D1=83=D0=BC=D0=B5=D0=BD=D1=82=D0=B0?= =?UTF-8?q?=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20=D0=90=D0=9F=D0=98=20=D0=94?= =?UTF-8?q?=D0=B8=D0=B0=D0=B4=D0=BE=D0=BA]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/public/index.php | 1 - .../Infrastructure/Traits/ApiHelperTrait.php | 51 +++++++++++++++++++ .../Controller/DigitalSignatureController.php | 14 +++-- 3 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 backend/src/Infrastructure/Traits/ApiHelperTrait.php diff --git a/backend/public/index.php b/backend/public/index.php index db42f9e..9982c21 100755 --- a/backend/public/index.php +++ b/backend/public/index.php @@ -1,5 +1,4 @@ $message, + 'body' => $body, + ], $code); + } + + public function createPaginateJsonResponse(array $items, int $limit, int $totalCount): JsonResponse + { + return $this->createJsonResponse( + [ + 'items' => $items, + 'pages' => ceil($totalCount / $limit), + 'totalCount' => $totalCount, + ] + ); + } + + public function createJsonResponseFromObject(AbstractResponse $body, string $message = 'OK', int $code = 200): JsonResponse + { + return new JsonResponse([ + 'message' => $message, + 'body' => $this->toArray($body), + ], $code); + } + + private function toArray(AbstractResponse $body): array + { + $reflect = new ReflectionClass($body); + $props = $reflect->getProperties(); + $array = []; + foreach ($props as $prop) { + $prop->setAccessible(true); + $array[$prop->getName()] = $prop->getValue($body); + $prop->setAccessible(false); + } + + return $array; + } +} diff --git a/backend/src/SignDocument/Controller/DigitalSignatureController.php b/backend/src/SignDocument/Controller/DigitalSignatureController.php index c3bc6c0..419514f 100644 --- a/backend/src/SignDocument/Controller/DigitalSignatureController.php +++ b/backend/src/SignDocument/Controller/DigitalSignatureController.php @@ -5,17 +5,18 @@ declare(strict_types=1); namespace App\SignDocument\Controller; use App\DigitalSignature\Controller\SignService; +use App\Infrastructure\Traits\ApiHelperTrait; use App\SignDocument\Api\Request\DigitalSignatureRequest; -use App\SignDocument\Api\Request\SignRequest; use App\SignDocument\Services\DigitalSignatureService; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; +use Exception; class DigitalSignatureController extends AbstractController { + use ApiHelperTrait; public function __construct( private readonly DigitalSignatureService $digitalSignatureService ){ @@ -26,7 +27,12 @@ class DigitalSignatureController extends AbstractController { $token = $request->server->get('HTTP_AUTHORIZATION'); - return new JsonResponse($this->digitalSignatureService->getSignature($digitalSignatureRequest, $token)); + try { + return $this->createJsonResponse([ + 'base64' => $this->digitalSignatureService->getSignature($digitalSignatureRequest, $token), + ]); + } catch (Exception $exception) { + return $this->createJsonResponse([], $exception->getMessage(), 400); + } } - } \ No newline at end of file