Skip to content

Commit ab83f31

Browse files
author
Denis Korolev
committed
добавил метод для скачивания документов
1 parent cfb06fb commit ab83f31

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed

src/DocumentApi.php

+26
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace MagDv\Sbis;
66

7+
use MagDv\Sbis\Entities\Document\Download\Request\DownloadRequest;
8+
use MagDv\Sbis\Entities\Document\Download\Response\DownloadResponse;
79
use MagDv\Sbis\Entities\Document\ListOfChanges\Request\ListOfChangesRequest;
810
use MagDv\Sbis\Entities\Document\ListOfChanges\Response\ListOfChangesResponse;
911
use MagDv\Sbis\Entities\Document\MakeAction\Request\MakeActionRequest;
@@ -79,4 +81,28 @@ public function listOfChanges(ListOfChangesRequest $listOfChangesRequest): ListO
7981

8082
return $body;
8183
}
84+
85+
public function downloadDocument(DownloadRequest $downloadRequest): DownloadResponse
86+
{
87+
$req = new Request(
88+
'GET',
89+
$downloadRequest->url
90+
);
91+
92+
$response = $this->send($req);
93+
$data = $this->prepareResponse($response);
94+
95+
if ($response->getStatusCode() === 500) {
96+
/** @var DownloadResponse $body */
97+
$body = $this->serializer->deserialize($data, DownloadResponse::class, 'json');
98+
$body->statusCode = $response->getStatusCode();
99+
} else {
100+
/** @var DownloadResponse $body */
101+
$body = new DownloadResponse();
102+
$body->content = $data;
103+
$body->statusCode = $response->getStatusCode();
104+
}
105+
106+
return $body;
107+
}
82108
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MagDv\Sbis\Entities\Document\Download\Request;
6+
7+
final class DownloadRequest
8+
{
9+
public ?string $url = null;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MagDv\Sbis\Entities\Document\Download\Response;
6+
7+
use MagDv\Sbis\BaseResponse;
8+
9+
final class DownloadResponse extends BaseResponse
10+
{
11+
public ?string $content = null;
12+
}

src/Interfaces/DocumentApiInterface.php

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace MagDv\Sbis\Interfaces;
66

7+
use MagDv\Sbis\Entities\Document\Download\Request\DownloadRequest;
8+
use MagDv\Sbis\Entities\Document\Download\Response\DownloadResponse;
79
use MagDv\Sbis\Entities\Document\ListOfChanges\Request\ListOfChangesRequest;
810
use MagDv\Sbis\Entities\Document\ListOfChanges\Response\ListOfChangesResponse;
911
use MagDv\Sbis\Entities\Document\MakeAction\Request\MakeActionRequest;
@@ -16,4 +18,5 @@ interface DocumentApiInterface
1618
public function sendDocument(SendDocumentRequest $request): SendDocumentResponse;
1719
public function makeAction(MakeActionRequest $request): MakeActionResponse;
1820
public function listOfChanges(ListOfChangesRequest $listOfChangesRequest): ListOfChangesResponse;
21+
public function downloadDocument(DownloadRequest $downloadRequest): DownloadResponse;
1922
}

tests/Functional/DocumentTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Test\Functional;
66

7+
use MagDv\Sbis\Entities\Document\Download\Request\DownloadRequest;
78
use MagDv\Sbis\Entities\Document\ListOfChanges\Request\ListOfChangesFilter;
89
use MagDv\Sbis\Entities\Document\ListOfChanges\Request\ListOfChangesParams;
910
use MagDv\Sbis\Entities\Document\ListOfChanges\Request\ListOfChangesRequest;
@@ -109,5 +110,12 @@ public function testSendDocument(): void
109110
$changes = $document->listOfChanges($changesRequest);
110111

111112
$this->assertNotEmpty($changes->result->document);
113+
114+
// урл заранее был создан и через какое - то время он может исчезнуть и тесты будут падать. Надо переделать тесты так, чтобы урл брался из ответов $changes
115+
// $downloadRequest = new DownloadRequest();
116+
// $downloadRequest->url = 'https://fix-online.sbis.ru/pdfservicepublic/service/?method=%D0%A1%D0%B5%D1%80%D0%B2%D0%B8%D1%81PDF.%D0%A1%D0%B8%D0%BD%D1%85%D1%80%D0%BE%D0%BD%D0%BD%D0%B0%D1%8F%D0%9F%D0%B5%D1%87%D0%B0%D1%82%D1%8C&params=eyJEb2N1bWVudHMiOnsicyI6W3sibiI6ItCY0LTQniIsInQiOiLQp9C40YHQu9C%2BINGG0LXQ%0Au9C%2B0LUifSx7Im4iOiLQoNCw0LfQtNC10LsiLCJ0Ijoi0KfQuNGB0LvQviDRhtC10LvQvtC1%0AIn0seyJ0Ijp7Im4iOiLQnNCw0YHRgdC40LIiLCJ0Ijoi0KLQtdC60YHRgiJ9LCJuIjoi0KHQ%0Av9C40YHQvtC60JLQktCUIn1dLCJkIjpbWzE5LG51bGwsWzcxXV1dfSwiT2JqZWN0TmFtZSI6%0AIkRvY1ByaW50IiwiTWV0aG9kTmFtZSI6Ik1hc3NMaXN0IiwiUGFyYW1zIjp7InMiOlt7Im4i%0AOiJPdXRwdXRGaWxlTmFtZSIsInQiOiLQodGC0YDQvtC60LAifSx7Im4iOiJSZXFJZCIsInQi%0AOiLQodGC0YDQvtC60LAifSx7Im4iOiJkc2wiLCJ0Ijoi0KfQuNGB0LvQviDRhtC10LvQvtC1%0AIn0seyJuIjoiVW5zdXBwb3J0ZWRGaWxlSW5mbyIsInQiOiLQm9C%2B0LPQuNGH0LXRgdC60L7Q%0AtSJ9XSwiZCI6WyJPTl9UUk5BQ0xHUk9UXzJCRTk0NzUzZDQzMzRmNDRiZTA5ZTkxOWEwY2E3%0AM2E4ZjYyXzJCRTk2MGQwYzY2MTYzMDExZTM4NGM0MDA1MDU2YjcwZDkwXzJCRWJiMWU4YTli%0AYzdkMTQ1MGJiZGIyMDdjNjgwOGY0MDZhXzBfMjAyMzAzMDdfZDhlNTBhZDQtNjdhNy00YTdk%0ALWI0ZGQtODA4NjhmNDBlMTA3LnBkZiIsIjExMDY5MjcxNTEyNTYyMjUyNzgyIiwiMSIsZmFs%0Ac2VdfX0%3D&protocol=3&id=0&srv=1';
117+
//
118+
// $response = $document->downloadDocument($downloadRequest);
119+
// $this->assertNotEmpty($response->content);
112120
}
113121
}

0 commit comments

Comments
 (0)