Skip to content

Commit

Permalink
resolved #31 Attachment - CRUD operations
Browse files Browse the repository at this point in the history
  • Loading branch information
pekral committed Nov 18, 2020
1 parent 13cf46d commit 726998c
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
70 changes: 70 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,76 @@ public function allInEvidence(): array
return $this->responseHydrator->convertResponseToEvidenceResults($response);
}

/**
* @return array<mixed>
* @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<mixed>
* @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(
Expand Down
22 changes: 22 additions & 0 deletions src/Http/UrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed> $uriParameters
*/
Expand Down
25 changes: 25 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit 726998c

Please sign in to comment.