Skip to content

Commit ebb8803

Browse files
committed
Add task object and related enums
1 parent f3837f0 commit ebb8803

File tree

3 files changed

+205
-0
lines changed

3 files changed

+205
-0
lines changed

src/Contracts/Task.php

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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+
}

src/Contracts/TaskStatus.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MeiliSearch\Contracts;
6+
7+
enum TaskStatus: string
8+
{
9+
case Canceled = 'canceled';
10+
case Enqueued = 'enqueued';
11+
case Failed = 'failed';
12+
case Succeeded = 'succeeded';
13+
case Processing = 'processing';
14+
}

src/Contracts/TaskType.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MeiliSearch\Contracts;
6+
7+
enum TaskType: string
8+
{
9+
case IndexCreation = 'indexCreation';
10+
case IndexUpdate = 'indexUpdate';
11+
case IndexDeletion = 'indexDeletion';
12+
case IndexSwap = 'indexSwap';
13+
case DocumentAdditionOrUpdate = 'documentAdditionOrUpdate';
14+
case DocumentDeletion = 'documentDeletion';
15+
case DocumentEdition = 'documentEdition';
16+
case SettingsUpdate = 'settingsUpdate';
17+
case DumpCreation = 'dumpCreation';
18+
case TaskCancelation = 'taskCancelation';
19+
case TaskDeletion = 'taskDeletion';
20+
case SnapshotCreation = 'snapshotCreation';
21+
}

0 commit comments

Comments
 (0)