DDB-1753 #5

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

View File

@ -13,3 +13,14 @@ DATABASE_URL="postgresql://app:!ChangeMe!@127.0.0.1:5432/app?serverVersion=16&ch
DOT_DOT_URL='http://dot-dot.local'
API_TOKEN='secret'
###> KONTUR_DIADOC###
API_KONTUR_DIADOC_END_POINT_URL=https://diadoc-api.kontur.ru
API_KONTUR_DIADOC_API_TOKEN=API-3701ccdb-7bdf-4f29-bbe0-3a0278eee913
API_KONTUR_DIADOC_LOGIN=LOGIN
API_KONTUR_DIADOC_PASSWORD=PASSWORD
###> KONTUR_DIADOC_POST_MESSAGE_DIRECTION###
API_KONTUR_DIADOC_PMD_BOX_ID=c1a0d340-a481-49b2-ba21-d51dea59df55
API_KONTUR_DIADOC_PMD_FROM_DEPT_ID=00000000-0000-0000-0000-000000000000
API_KONTUR_DIADOC_PMD_TO_DEPT_ID=da0f3b33-ef70-41e9-8e9e-fb431a891ef4
###< KONTUR_DIADOC###

View File

@ -1,5 +1,5 @@
controllers:
controllers_sign_document:
resource:
path: ../src/Controller/
namespace: App\Controller
path: ../src/SignDocument/Controller/
namespace: App\SignDocument\Controller
type: attribute

View File

@ -28,6 +28,6 @@ services:
GuzzleHttp\Client: '@guzzle.http_client'
App\Api\ApiParams:
App\SignDocument\Api\ApiParams:
arguments:
$endPointUrl: '%env(DOT_DOT_URL)%'

View File

View File

View File

View File

@ -2,11 +2,10 @@
declare(strict_types=1);
namespace App\Api;
namespace App\SignDocument\Api;
use App\Infrastructure\External\Api\AbstractApi;
use App\Infrastructure\External\Api\BinaryStringFileResult;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\RequestOptions;
class Api extends AbstractApi

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace App\Api;
namespace App\SignDocument\Api;
class ApiParams
{

View File

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace App\SignDocument\Api\Request;
use App\Infrastructure\Http\RequestDtoInterface;
class DigitalSignatureRequest implements RequestDtoInterface
{
public string $url;
public string $apiToken;
}

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace App\Api\Request;
namespace App\SignDocument\Api\Request;
use App\Infrastructure\Http\RequestDtoInterface;

View File

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace App\SignDocument\Controller;
use App\DigitalSignature\Controller\SignService;
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;
class DigitalSignatureController extends AbstractController
{
public function __construct(
private readonly DigitalSignatureService $digitalSignatureService
){
}
#[Route(path: '/digital/sign', name: 'app.digital.sign', methods: ['POST'])]
public function __invoke(Request $request, DigitalSignatureRequest $digitalSignatureRequest): Response
{
$token = $request->server->get('HTTP_AUTHORIZATION');
return new JsonResponse($this->digitalSignatureService->getSignature($digitalSignatureRequest, $token));
}
}

View File

@ -2,15 +2,15 @@
declare(strict_types=1);
namespace App\Controller;
namespace App\SignDocument\Controller;
use App\Api\Request\SignRequest;
use App\SignService;
use App\SignDocument\Api\Request\SignRequest;
use App\SignDocument\Services\SignService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class SignController extends AbstractController
{

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace App;
namespace App\SignDocument\Services;
class DevSignService
{

View File

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace App\SignDocument\Services;
use App\SignDocument\Api\Api;
use App\SignDocument\Api\ApiParams;
use App\SignDocument\Api\Request\DigitalSignatureRequest;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Exception;
use RuntimeException;
class DigitalSignatureService
{
public function __construct(
private readonly Api $api,
private readonly ApiParams $apiParams,
private readonly RemoveExistingDocumentService $removeExistingDocumentService,
){
}
public function getSignature(DigitalSignatureRequest $request, string $token): string
{
if ($_ENV['API_TOKEN'] !== $request->apiToken) {
throw new AccessDeniedHttpException('Доступ запрещен');
}
$this->api->apiParams = $this->apiParams;
try {
$document = $this->api->download($request->url, $token);
exec(sprintf('cp %s %s.pdf', $document->tempFileName, $document->tempFileName));
$signature = exec(sprintf('cryptcp -sign -detached -der %s', $document->tempFileName));
$response = base64_encode($signature);
$this->removeExistingDocumentService->removeExistingDocument($document);
return $response;
} catch (Exception $e) {
throw new RuntimeException($e->getMessage());
}
}
}

View File

@ -3,7 +3,7 @@
declare(strict_types=1);
namespace App;
namespace App\SignDocument\Services;
class ProdSignService

View File

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\SignDocument\Services;
use App\Infrastructure\External\Api\BinaryStringFileResult;
class RemoveExistingDocumentService
{
public function removeExistingDocument(BinaryStringFileResult $document): void
{
if (file_exists($document->tempFileName)) {
unlink($document->tempFileName);
}
}
}

View File

@ -2,27 +2,24 @@
declare(strict_types=1);
namespace App;
namespace App\SignDocument\Services;
use App\Api\Api;
use App\Api\ApiParams;
use App\Api\Request\SignRequest;
use App\Infrastructure\External\Api\BinaryStringFileResult;
use App\SignDocument\Api\Api;
use App\SignDocument\Api\ApiParams;
use App\SignDocument\Api\Request\SignRequest;
use Exception;
use RuntimeException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class SignService
{
private DevSignService $devSignService;
private ProdSignService $prodSignService;
public function __construct(
private Api $api,
private ApiParams $apiParams
private readonly Api $api,
private readonly ApiParams $apiParams,
private readonly DevSignService $devSignService,
private readonly ProdSignService $prodSignService,
private readonly RemoveExistingDocumentService $removeExistingDocumentService,
){
$this->devSignService = new DevSignService();
$this->prodSignService = new ProdSignService();
}
public function signDocument(SignRequest $request,string $token): array
{
@ -39,7 +36,7 @@ class SignService
$response = $this->api->send($token, $document->tempFileName . '_sign.pdf', $request->batch);
$this->removeExistingDocument($document);
$this->removeExistingDocumentService->removeExistingDocument($document);
return $response;
} catch (Exception $e) {
@ -54,11 +51,4 @@ class SignService
'prod' => $this->prodSignService->sign($documentUrl),
};
}
private function removeExistingDocument(BinaryStringFileResult $document): void
{
if (file_exists($document->tempFileName)) {
unlink($document->tempFileName);
}
}
}