DDB-1753 #5

Merged
dcherednik merged 21 commits from DDB-1753 into master 2025-03-21 13:37:34 +03:00
4 changed files with 52 additions and 12 deletions
Showing only changes of commit 52d40c6249 - Show all commits

View File

@ -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');
}
}

View File

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\SignDocument\Api\Response;
use App\Infrastructure\Api\Response\AbstractResponse;
class DigitalSignatureResponse extends AbstractResponse
{
public function __construct(
public string $hash,
public string $content,
)
{
}
}

View File

@ -25,8 +25,8 @@ class DigitalSignatureController extends AbstractController
{
$token = $request->server->get('HTTP_AUTHORIZATION');
return $this->createJsonResponse([
'hash' => $this->digitalSignatureService->getSignature($digitalSignatureRequest, $token),
]);
return $this->createJsonResponseFromObject(
$this->digitalSignatureService->getSignature($digitalSignatureRequest, $token)
);
}
}

View File

@ -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());
}