Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
38 / 38 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
| ErrorPayloadBuilder | |
100.00% |
38 / 38 |
|
100.00% |
9 / 9 |
13 | |
100.00% |
1 / 1 |
| fromException | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| setMessage | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setCode | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setFileId | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setSignRequestId | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setSignRequestUuid | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| addFileError | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| clearFileErrors | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| build | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | /** |
| 5 | * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 6 | * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | */ |
| 8 | |
| 9 | namespace OCA\Libresign\Service\SignRequest\Error; |
| 10 | |
| 11 | use DateTime; |
| 12 | use DateTimeInterface; |
| 13 | |
| 14 | class ErrorPayloadBuilder { |
| 15 | private string $message = 'An error occurred'; |
| 16 | private int $code = 0; |
| 17 | private ?int $fileId = null; |
| 18 | private ?int $signRequestId = null; |
| 19 | private ?string $signRequestUuid = null; |
| 20 | /** @var array<int, array{message: string, code: int}> */ |
| 21 | private array $fileErrors = []; |
| 22 | |
| 23 | public static function fromException(\Throwable $e, ?int $fileId = null, ?int $signRequestId = null, ?string $signRequestUuid = null): self { |
| 24 | $builder = new self(); |
| 25 | return $builder |
| 26 | ->setMessage($e->getMessage()) |
| 27 | ->setCode($e->getCode()) |
| 28 | ->setFileId($fileId) |
| 29 | ->setSignRequestId($signRequestId) |
| 30 | ->setSignRequestUuid($signRequestUuid); |
| 31 | } |
| 32 | |
| 33 | public function setMessage(string $message): self { |
| 34 | $this->message = $message; |
| 35 | return $this; |
| 36 | } |
| 37 | |
| 38 | public function setCode(int $code): self { |
| 39 | $this->code = $code; |
| 40 | return $this; |
| 41 | } |
| 42 | |
| 43 | public function setFileId(?int $fileId): self { |
| 44 | $this->fileId = $fileId; |
| 45 | return $this; |
| 46 | } |
| 47 | |
| 48 | public function setSignRequestId(?int $signRequestId): self { |
| 49 | $this->signRequestId = $signRequestId; |
| 50 | return $this; |
| 51 | } |
| 52 | |
| 53 | public function setSignRequestUuid(?string $signRequestUuid): self { |
| 54 | $this->signRequestUuid = $signRequestUuid; |
| 55 | return $this; |
| 56 | } |
| 57 | |
| 58 | public function addFileError(int $fileId, \Throwable $e): self { |
| 59 | $this->fileErrors[$fileId] = [ |
| 60 | 'message' => $e->getMessage(), |
| 61 | 'code' => $e->getCode(), |
| 62 | ]; |
| 63 | return $this; |
| 64 | } |
| 65 | |
| 66 | public function clearFileErrors(): self { |
| 67 | $this->fileErrors = []; |
| 68 | return $this; |
| 69 | } |
| 70 | |
| 71 | public function build(): array { |
| 72 | $payload = [ |
| 73 | 'message' => $this->message, |
| 74 | 'code' => $this->code, |
| 75 | 'timestamp' => (new DateTime())->format(DateTimeInterface::ATOM), |
| 76 | ]; |
| 77 | |
| 78 | if ($this->fileId !== null) { |
| 79 | $payload['fileId'] = $this->fileId; |
| 80 | } |
| 81 | |
| 82 | if ($this->signRequestId !== null) { |
| 83 | $payload['signRequestId'] = $this->signRequestId; |
| 84 | } |
| 85 | |
| 86 | if ($this->signRequestUuid !== null) { |
| 87 | $payload['signRequestUuid'] = $this->signRequestUuid; |
| 88 | } |
| 89 | |
| 90 | if (!empty($this->fileErrors)) { |
| 91 | $payload['fileErrors'] = $this->fileErrors; |
| 92 | } |
| 93 | |
| 94 | return $payload; |
| 95 | } |
| 96 | } |