|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Apify\Client\Model; |
| 6 | + |
| 7 | +/** |
| 8 | + * A batch of requests from the head of a request queue, locked for exclusive processing. Returned by |
| 9 | + * {@see \Apify\Client\Resource\RequestQueueClient::listAndLockHead()}. |
| 10 | + */ |
| 11 | +final class LockedRequestQueueHead |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @param list<RequestQueueRequest> $items |
| 15 | + */ |
| 16 | + public function __construct( |
| 17 | + private array $items, |
| 18 | + private int $limit, |
| 19 | + private bool $hadMultipleClients, |
| 20 | + private int $lockSecs, |
| 21 | + private ?bool $queueHasLockedRequests, |
| 22 | + private ?string $clientKey, |
| 23 | + private ?string $queueModifiedAt = null, |
| 24 | + ) { |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @param mixed $data the decoded locked-head object |
| 29 | + */ |
| 30 | + public static function fromData(mixed $data): self |
| 31 | + { |
| 32 | + $data = is_array($data) ? $data : []; |
| 33 | + $rawItems = (isset($data['items']) && is_array($data['items'])) ? array_values($data['items']) : []; |
| 34 | + $items = array_map( |
| 35 | + static fn ($item) => RequestQueueRequest::fromArray(is_array($item) ? $item : []), |
| 36 | + $rawItems |
| 37 | + ); |
| 38 | + |
| 39 | + return new self( |
| 40 | + $items, |
| 41 | + (int) ($data['limit'] ?? count($items)), |
| 42 | + (bool) ($data['hadMultipleClients'] ?? false), |
| 43 | + (int) ($data['lockSecs'] ?? 0), |
| 44 | + isset($data['queueHasLockedRequests']) ? (bool) $data['queueHasLockedRequests'] : null, |
| 45 | + isset($data['clientKey']) ? (string) $data['clientKey'] : null, |
| 46 | + isset($data['queueModifiedAt']) ? (string) $data['queueModifiedAt'] : null, |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * The locked requests from the head of the queue. Each item carries its own |
| 52 | + * {@see RequestQueueRequest} lock-expiry field as reported by the API. |
| 53 | + * |
| 54 | + * @return list<RequestQueueRequest> |
| 55 | + */ |
| 56 | + public function getItems(): array |
| 57 | + { |
| 58 | + return $this->items; |
| 59 | + } |
| 60 | + |
| 61 | + /** The maximum number of requests requested. */ |
| 62 | + public function getLimit(): int |
| 63 | + { |
| 64 | + return $this->limit; |
| 65 | + } |
| 66 | + |
| 67 | + /** Whether multiple clients have accessed the queue. */ |
| 68 | + public function hadMultipleClients(): bool |
| 69 | + { |
| 70 | + return $this->hadMultipleClients; |
| 71 | + } |
| 72 | + |
| 73 | + /** The lock duration applied to every returned request, in seconds. */ |
| 74 | + public function getLockSecs(): int |
| 75 | + { |
| 76 | + return $this->lockSecs; |
| 77 | + } |
| 78 | + |
| 79 | + /** Whether the queue has any requests locked by any client (this one or another). */ |
| 80 | + public function queueHasLockedRequests(): ?bool |
| 81 | + { |
| 82 | + return $this->queueHasLockedRequests; |
| 83 | + } |
| 84 | + |
| 85 | + /** The client key used to acquire the locks. */ |
| 86 | + public function getClientKey(): ?string |
| 87 | + { |
| 88 | + return $this->clientKey; |
| 89 | + } |
| 90 | + |
| 91 | + /** ISO 8601 timestamp of the last modification to the queue. */ |
| 92 | + public function getQueueModifiedAt(): ?string |
| 93 | + { |
| 94 | + return $this->queueModifiedAt; |
| 95 | + } |
| 96 | +} |
0 commit comments