DDB-1753 #5
|
|
@ -1,5 +1,4 @@
|
||||||
<?php
|
<?php
|
||||||
const E_STRICT = 1;
|
|
||||||
|
|
||||||
use App\Kernel;
|
use App\Kernel;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,51 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Infrastructure\Traits;
|
||||||
|
|
||||||
|
use App\Infrastructure\Api\Response\AbstractResponse;
|
||||||
|
use ReflectionClass;
|
||||||
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
|
|
||||||
|
trait ApiHelperTrait
|
||||||
|
{
|
||||||
|
public function createJsonResponse(array $body, string $message = 'OK', int $code = 200): JsonResponse
|
||||||
|
{
|
||||||
|
return new JsonResponse([
|
||||||
|
'message' => $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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,17 +5,18 @@ declare(strict_types=1);
|
||||||
namespace App\SignDocument\Controller;
|
namespace App\SignDocument\Controller;
|
||||||
|
|
||||||
use App\DigitalSignature\Controller\SignService;
|
use App\DigitalSignature\Controller\SignService;
|
||||||
|
use App\Infrastructure\Traits\ApiHelperTrait;
|
||||||
use App\SignDocument\Api\Request\DigitalSignatureRequest;
|
use App\SignDocument\Api\Request\DigitalSignatureRequest;
|
||||||
use App\SignDocument\Api\Request\SignRequest;
|
|
||||||
use App\SignDocument\Services\DigitalSignatureService;
|
use App\SignDocument\Services\DigitalSignatureService;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\Routing\Annotation\Route;
|
use Symfony\Component\Routing\Annotation\Route;
|
||||||
|
use Exception;
|
||||||
|
|
||||||
class DigitalSignatureController extends AbstractController
|
class DigitalSignatureController extends AbstractController
|
||||||
{
|
{
|
||||||
|
use ApiHelperTrait;
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly DigitalSignatureService $digitalSignatureService
|
private readonly DigitalSignatureService $digitalSignatureService
|
||||||
){
|
){
|
||||||
|
|
@ -26,7 +27,12 @@ class DigitalSignatureController extends AbstractController
|
||||||
{
|
{
|
||||||
$token = $request->server->get('HTTP_AUTHORIZATION');
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue