diff --git a/backend/src/Infrastructure/External/Api/BinaryStringFileResult.php b/backend/src/Infrastructure/External/Api/BinaryStringFileResult.php index 6331c2c..363ef74 100644 --- a/backend/src/Infrastructure/External/Api/BinaryStringFileResult.php +++ b/backend/src/Infrastructure/External/Api/BinaryStringFileResult.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace App\Infrastructure\External\Api; +use RuntimeException; + /** * Враппер бинарных фалов, при создании класса содает новый временный файл, * при необходимости сохраняет файл под новым именем @@ -23,4 +25,14 @@ class BinaryStringFileResult $this->tempFileName = sprintf('%s/%s_%s', sys_get_temp_dir(), 'Document', time()); file_put_contents($this->tempFileName, $content); } + + public function remove(): bool + { + if (file_exists($this->tempFileName)) { + unlink($this->tempFileName); + return true; + } + + throw new RuntimeException('Temp file not found'); + } } diff --git a/backend/src/SignDocument/Api/Response/DigitalSignatureResponse.php b/backend/src/SignDocument/Api/Response/DigitalSignatureResponse.php new file mode 100644 index 0000000..7392f76 --- /dev/null +++ b/backend/src/SignDocument/Api/Response/DigitalSignatureResponse.php @@ -0,0 +1,18 @@ +server->get('HTTP_AUTHORIZATION'); - return $this->createJsonResponse([ - 'hash' => $this->digitalSignatureService->getSignature($digitalSignatureRequest, $token), - ]); + return $this->createJsonResponseFromObject( + $this->digitalSignatureService->getSignature($digitalSignatureRequest, $token) + ); } } \ No newline at end of file diff --git a/backend/src/SignDocument/Services/DigitalSignatureService.php b/backend/src/SignDocument/Services/DigitalSignatureService.php index 6c9468b..7ff187e 100644 --- a/backend/src/SignDocument/Services/DigitalSignatureService.php +++ b/backend/src/SignDocument/Services/DigitalSignatureService.php @@ -4,22 +4,31 @@ declare(strict_types=1); namespace App\SignDocument\Services; +use App\Infrastructure\External\Api\BinaryStringFileResult; use App\SignDocument\Api\Api; use App\SignDocument\Api\ApiParams; use App\SignDocument\Api\Request\DigitalSignatureRequest; +use App\SignDocument\Api\Response\DigitalSignatureResponse; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Exception; use RuntimeException; class DigitalSignatureService { + private BinaryStringFileResult $document; + public function __construct( private readonly Api $api, private readonly ApiParams $apiParams, - private readonly RemoveExistingDocumentService $removeExistingDocumentService, ){ } - public function getSignature(DigitalSignatureRequest $request, string $token): string + + public function __destruct() + { + $this->document->remove(); + } + + public function getSignature(DigitalSignatureRequest $request, string $token): DigitalSignatureResponse { if ($_ENV['API_TOKEN'] !== $request->apiToken) { throw new AccessDeniedHttpException('Доступ запрещен'); @@ -28,16 +37,17 @@ class DigitalSignatureService $this->api->apiParams = $this->apiParams; try { - $document = $this->api->download($request->url, $token); + $this->document = $this->api->download($request->url, $token); - exec(sprintf('cp %s %s.pdf', $document->tempFileName, $document->tempFileName)); - exec(sprintf('cryptcp -sign -detached -der %s', $document->tempFileName . '.pdf')); + exec(sprintf('cp %s %s.pdf', $this->document->tempFileName, $this->document->tempFileName)); + exec(sprintf('cryptcp -sign -detached -der %s.pdf', $this->document->tempFileName)); - $response = base64_encode(file_get_contents($document->tempFileName . '.pdf.sgn')); + $response = base64_encode(file_get_contents($this->document->tempFileName . '.pdf.sgn')); - $this->removeExistingDocumentService->removeExistingDocument($document); - - return $response; + return new DigitalSignatureResponse( + hash: $response, + content: file_get_contents(sprintf('%s.pdf', $this->document->tempFileName)) + ); } catch (Exception $e) { throw new RuntimeException($e->getMessage()); }