|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace MeiliSearch\Contracts; |
| 6 | + |
| 7 | +final class Task implements \ArrayAccess |
| 8 | +{ |
| 9 | + /** |
| 10 | + * @param non-negative-int $taskUid |
| 11 | + * @param non-empty-string|null $indexUid |
| 12 | + * @param array<mixed> $data Raw data |
| 13 | + */ |
| 14 | + public function __construct( |
| 15 | + private readonly int $taskUid, |
| 16 | + private readonly ?string $indexUid, |
| 17 | + private readonly TaskStatus $status, |
| 18 | + private readonly TaskType $type, |
| 19 | + private readonly \DateTimeImmutable $enqueuedAt, |
| 20 | + private readonly ?\DateTimeImmutable $startedAt = null, |
| 21 | + private readonly ?\DateTimeImmutable $finishedAt = null, |
| 22 | + private readonly ?string $duration = null, |
| 23 | + private readonly ?int $canceledBy = null, |
| 24 | + private readonly ?int $batchUid = null, |
| 25 | + private readonly ?array $details = null, |
| 26 | + private readonly ?array $error = null, |
| 27 | + private readonly array $data = [], |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @return non-negative-int |
| 33 | + */ |
| 34 | + public function getTaskUid(): int |
| 35 | + { |
| 36 | + return $this->taskUid; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @return non-empty-string|null |
| 41 | + */ |
| 42 | + public function getIndexUid(): ?string |
| 43 | + { |
| 44 | + return $this->indexUid; |
| 45 | + } |
| 46 | + |
| 47 | + public function getStatus(): TaskStatus |
| 48 | + { |
| 49 | + return $this->status; |
| 50 | + } |
| 51 | + |
| 52 | + public function getType(): TaskType |
| 53 | + { |
| 54 | + return $this->type; |
| 55 | + } |
| 56 | + |
| 57 | + public function getEnqueuedAt(): \DateTimeImmutable |
| 58 | + { |
| 59 | + return $this->enqueuedAt; |
| 60 | + } |
| 61 | + |
| 62 | + public function getStartedAt(): ?\DateTimeImmutable |
| 63 | + { |
| 64 | + return $this->startedAt; |
| 65 | + } |
| 66 | + |
| 67 | + public function getFinishedAt(): ?\DateTimeImmutable |
| 68 | + { |
| 69 | + return $this->finishedAt; |
| 70 | + } |
| 71 | + |
| 72 | + public function getDuration(): ?string |
| 73 | + { |
| 74 | + return $this->duration; |
| 75 | + } |
| 76 | + |
| 77 | + public function getCanceledBy(): ?int |
| 78 | + { |
| 79 | + return $this->canceledBy; |
| 80 | + } |
| 81 | + |
| 82 | + public function getBatchUid(): ?int |
| 83 | + { |
| 84 | + return $this->batchUid; |
| 85 | + } |
| 86 | + |
| 87 | + public function getDetails(): ?array |
| 88 | + { |
| 89 | + return $this->details; |
| 90 | + } |
| 91 | + |
| 92 | + public function getError(): ?array |
| 93 | + { |
| 94 | + return $this->error; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @return array<mixed> |
| 99 | + */ |
| 100 | + public function getData(): array |
| 101 | + { |
| 102 | + return $this->data; |
| 103 | + } |
| 104 | + |
| 105 | + // @todo: deprecate |
| 106 | + public function offsetExists(mixed $offset): bool |
| 107 | + { |
| 108 | + return \array_key_exists($offset, $this->data); |
| 109 | + } |
| 110 | + |
| 111 | + // @todo: deprecate |
| 112 | + public function offsetGet(mixed $offset): mixed |
| 113 | + { |
| 114 | + return $this->data[$offset] ?? null; |
| 115 | + } |
| 116 | + |
| 117 | + // @todo: deprecate |
| 118 | + public function offsetSet(mixed $offset, mixed $value): void |
| 119 | + { |
| 120 | + throw new \LogicException(\sprintf('Setting data on "%s::%s" is not supported.', get_debug_type($this), $offset)); |
| 121 | + } |
| 122 | + |
| 123 | + // @todo: deprecate |
| 124 | + public function offsetUnset(mixed $offset): void |
| 125 | + { |
| 126 | + throw new \LogicException(\sprintf('Unsetting data on "%s::%s" is not supported.', get_debug_type($this), $offset)); |
| 127 | + } |
| 128 | + |
| 129 | + public function isFinished(): bool |
| 130 | + { |
| 131 | + return TaskStatus::Enqueued !== $this->status && TaskStatus::Processing !== $this->status; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @param array{ |
| 136 | + * taskUid?: int, |
| 137 | + * uid?: int, |
| 138 | + * indexUid: non-empty-string, |
| 139 | + * status: non-empty-string, |
| 140 | + * type: non-empty-string, |
| 141 | + * enqueuedAt: non-empty-string, |
| 142 | + * startedAt?: non-empty-string, |
| 143 | + * finishedAt?: non-empty-string, |
| 144 | + * duration?: non-empty-string, |
| 145 | + * canceledBy?: int, |
| 146 | + * batchUid?: int, |
| 147 | + * details?: array<mixed>, |
| 148 | + * error?: array<mixed>, |
| 149 | + * data: array<mixed> |
| 150 | + * } $data |
| 151 | + */ |
| 152 | + public static function fromArray(array $data): Task |
| 153 | + { |
| 154 | + return new self( |
| 155 | + $data['taskUid'] ?? $data['uid'], |
| 156 | + $data['indexUid'], |
| 157 | + TaskStatus::from($data['status']), |
| 158 | + TaskType::from($data['type']), |
| 159 | + new \DateTimeImmutable($data['enqueuedAt']), |
| 160 | + isset($data['startedAt']) ? new \DateTimeImmutable($data['startedAt']) : null, |
| 161 | + isset($data['finishedAt']) ? new \DateTimeImmutable($data['finishedAt']) : null, |
| 162 | + $data['duration'] ?? null, |
| 163 | + $data['canceledBy'] ?? null, |
| 164 | + $data['batchUid'] ?? null, |
| 165 | + $data['details'] ?? null, |
| 166 | + $data['error'] ?? null, |
| 167 | + $data, |
| 168 | + ); |
| 169 | + } |
| 170 | +} |
0 commit comments