DDB-1753 #5
|
|
@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\Infrastructure\External\Api;
|
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());
|
$this->tempFileName = sprintf('%s/%s_%s', sys_get_temp_dir(), 'Document', time());
|
||||||
file_put_contents($this->tempFileName, $content);
|
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');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -25,8 +25,8 @@ class DigitalSignatureController extends AbstractController
|
||||||
{
|
{
|
||||||
$token = $request->server->get('HTTP_AUTHORIZATION');
|
$token = $request->server->get('HTTP_AUTHORIZATION');
|
||||||
|
|
||||||
return $this->createJsonResponse([
|
return $this->createJsonResponseFromObject(
|
||||||
'hash' => $this->digitalSignatureService->getSignature($digitalSignatureRequest, $token),
|
$this->digitalSignatureService->getSignature($digitalSignatureRequest, $token)
|
||||||
]);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -4,22 +4,31 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\SignDocument\Services;
|
namespace App\SignDocument\Services;
|
||||||
|
|
||||||
|
use App\Infrastructure\External\Api\BinaryStringFileResult;
|
||||||
use App\SignDocument\Api\Api;
|
use App\SignDocument\Api\Api;
|
||||||
use App\SignDocument\Api\ApiParams;
|
use App\SignDocument\Api\ApiParams;
|
||||||
use App\SignDocument\Api\Request\DigitalSignatureRequest;
|
use App\SignDocument\Api\Request\DigitalSignatureRequest;
|
||||||
|
use App\SignDocument\Api\Response\DigitalSignatureResponse;
|
||||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||||
use Exception;
|
use Exception;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
|
|
||||||
class DigitalSignatureService
|
class DigitalSignatureService
|
||||||
{
|
{
|
||||||
|
private BinaryStringFileResult $document;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly Api $api,
|
private readonly Api $api,
|
||||||
private readonly ApiParams $apiParams,
|
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) {
|
if ($_ENV['API_TOKEN'] !== $request->apiToken) {
|
||||||
throw new AccessDeniedHttpException('Доступ запрещен');
|
throw new AccessDeniedHttpException('Доступ запрещен');
|
||||||
|
|
@ -28,16 +37,17 @@ class DigitalSignatureService
|
||||||
$this->api->apiParams = $this->apiParams;
|
$this->api->apiParams = $this->apiParams;
|
||||||
|
|
||||||
try {
|
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('cp %s %s.pdf', $this->document->tempFileName, $this->document->tempFileName));
|
||||||
exec(sprintf('cryptcp -sign -detached -der %s', $document->tempFileName . '.pdf'));
|
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 new DigitalSignatureResponse(
|
||||||
|
hash: $response,
|
||||||
return $response;
|
content: file_get_contents(sprintf('%s.pdf', $this->document->tempFileName))
|
||||||
|
);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
throw new RuntimeException($e->getMessage());
|
throw new RuntimeException($e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue