From 726998c2678fef3a463f0f0378f815d00a1bb115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Kr=C3=A1l?= Date: Wed, 18 Nov 2020 21:33:15 +0100 Subject: [PATCH] resolved #31 Attachment - CRUD operations --- README.md | 17 ++++++++++ src/Client.php | 70 +++++++++++++++++++++++++++++++++++++++++ src/Http/UrlBuilder.php | 22 +++++++++++++ tests/ClientTest.php | 25 +++++++++++++++ 4 files changed, 134 insertions(+) diff --git a/README.md b/README.md index 78c37d0..52b6d74 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,23 @@ $evidenceItem = $client->findByCode($evidenceItemCode, $uriParameters); $evidenceItem = $client->findLastInEvidence($evidenceItemCode, $uriParameters); ``` +## Přílohy +Vytvoření přílohy +``` +$evidenceItem = $client->createAttachment($evidenceId, $fileName, $contentType, $contentTypeData); +``` + +Vyhledání přílohy +``` +$evidenceItem = $client->findAttachmentById($evidenceId, $attachmentId); +$evidenceItem = $client->findAttachments($evidenceId); +``` + +Smazání přílohy +``` +$evidenceItem = $client->deleteAttachment($evidenceId, $attachmentId); +``` + ## Sumace ``` $client->sumInEvidence(); diff --git a/src/Client.php b/src/Client.php index 0275869..e563423 100644 --- a/src/Client.php +++ b/src/Client.php @@ -371,6 +371,76 @@ public function allInEvidence(): array return $this->responseHydrator->convertResponseToEvidenceResults($response); } + /** + * @return array + * @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionFail + * @throws \EcomailFlexibee\Exception\EcomailFlexibeeForbidden + * @throws \EcomailFlexibee\Exception\EcomailFlexibeeInvalidAuthorization + * @throws \EcomailFlexibee\Exception\EcomailFlexibeeMethodNotAllowed + * @throws \EcomailFlexibee\Exception\EcomailFlexibeeNotAcceptableRequest + * @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestFail + */ + public function findAttachments(int $evidenceId): array + { + $response = $this->httpClient->request( + $this->queryBuilder->createAttachmentUriByEvidence($evidenceId, null), + Method::get(Method::GET), + [], + [], + [], + $this->config, + ); + + return $this->responseHydrator->convertResponseToEvidenceResults($response); + } + + public function createAttachment(int $evidenceId, string $fileName, string $contentType, string $contentTypeData): Response + { + return $this->httpClient->request( + $this->queryBuilder->createAttachmentUriByData($evidenceId, $fileName), + Method::get(Method::PUT), + ['content' => $contentTypeData], + [], + ['Content-Type' => $contentType], + $this->config, + ); + } + + public function deleteAttachment(int $evidenceId, int $attachmentId): Response + { + return $this->httpClient->request( + $this->queryBuilder->createAttachmentUriByEvidence($evidenceId, $attachmentId), + Method::get(Method::DELETE), + [], + [], + [], + $this->config, + ); + } + + /** + * @return array + * @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionFail + * @throws \EcomailFlexibee\Exception\EcomailFlexibeeForbidden + * @throws \EcomailFlexibee\Exception\EcomailFlexibeeInvalidAuthorization + * @throws \EcomailFlexibee\Exception\EcomailFlexibeeMethodNotAllowed + * @throws \EcomailFlexibee\Exception\EcomailFlexibeeNotAcceptableRequest + * @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestFail + */ + public function findAttachmentById(int $evidenceId, int $attachmentId): array + { + $response = $this->httpClient->request( + $this->queryBuilder->createAttachmentUriByEvidence($evidenceId, $attachmentId), + Method::get(Method::GET), + [], + [], + [], + $this->config, + ); + + return $this->responseHydrator->convertResponseToEvidenceResults($response); + } + public function countInEvidence(): int { $response = $this->httpClient->request( diff --git a/src/Http/UrlBuilder.php b/src/Http/UrlBuilder.php index 55635e7..d7cafce 100644 --- a/src/Http/UrlBuilder.php +++ b/src/Http/UrlBuilder.php @@ -110,6 +110,28 @@ public function createUriByEvidenceOnly(array $uriParameters): string return $this->getUrl(); } + public function createAttachmentUriByData(int $evidenceId, string $fileName): string + { + $path = new Path(sprintf('c/%s/%s/%d/prilohy/new/%s', $this->company, $this->evidence, $evidenceId, $fileName)); + $this->setPath($path); + + return $this->getUrl(); + } + + public function createAttachmentUriByEvidence(int $evidenceId, ?int $attachmentId): string + { + $path = $attachmentId === null ? new Path( + sprintf('c/%s/%s/%d/prilohy.json', $this->company, $this->evidence, $evidenceId), + ) : new Path( + sprintf('c/%s/%s/%d/prilohy/%d.json', $this->company, $this->evidence, $evidenceId, $attachmentId), + ); + + $this->setPath($path); + $this->createQueryParams([]); + + return $this->getUrl(); + } + /** * @param array $uriParameters */ diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 094fa20..1dac2a9 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -365,6 +365,31 @@ public function testLogRequest(): void unlink($logPath); } + public function testAttachments(): void + { + $logPath = 'logs/log.txt'; + @unlink($logPath); + $client = new Client( + Config::HOST, + Config::COMPANY, + Config::USERNAME, + Config::PASSWORD, + 'adresar', + false, + null, + $logPath, + ); + + $lastEvidenceItem = $client->findLastInEvidence(false); + $id = (int) $lastEvidenceItem->getData()['adresar'][0]['id']; + $attachmentName = sprintf('%s.jpg', uniqid()); + $response = $client->createAttachment($id, $attachmentName, 'image/jpeg', 'x'); + Assert::assertTrue($response->isSuccess()); + $attachmentId = (int) $client->findAttachments($id)[0]->getData()['priloha'][0]['id']; + $response = $client->deleteAttachment($id, $attachmentId); + Assert::assertTrue($response->isSuccess()); + } + private function checkResponseStructure(EvidenceResult $result): void { $requiredKeys = [