|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Psl\Channel\Internal; |
| 6 | + |
| 7 | +use Psl\Channel\ChannelInterface; |
| 8 | +use Psl\Channel\Exception; |
| 9 | +use Psl\DataStructure\Queue; |
| 10 | +use Psl\DataStructure\QueueInterface; |
| 11 | + |
| 12 | +/** |
| 13 | + * @template T |
| 14 | + * |
| 15 | + * @implements ChannelInterface<T> |
| 16 | + * |
| 17 | + * @internal |
| 18 | + */ |
| 19 | +final class ChannelState implements ChannelInterface |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var QueueInterface<T> |
| 23 | + */ |
| 24 | + private QueueInterface $messages; |
| 25 | + |
| 26 | + private bool $closed = false; |
| 27 | + |
| 28 | + public function __construct( |
| 29 | + private ?int $capacity = null, |
| 30 | + ) { |
| 31 | + $this->messages = new Queue(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * {@inheritDoc} |
| 36 | + */ |
| 37 | + public function getCapacity(): ?int |
| 38 | + { |
| 39 | + return $this->capacity; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * {@inheritDoc} |
| 44 | + */ |
| 45 | + public function close(): void |
| 46 | + { |
| 47 | + $this->closed = true; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * {@inheritDoc} |
| 52 | + */ |
| 53 | + public function isClosed(): bool |
| 54 | + { |
| 55 | + return $this->closed; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * {@inheritDoc} |
| 60 | + */ |
| 61 | + public function count(): int |
| 62 | + { |
| 63 | + return $this->messages->count(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * {@inheritDoc} |
| 68 | + */ |
| 69 | + public function isFull(): bool |
| 70 | + { |
| 71 | + if (null === $this->capacity) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + return $this->capacity === $this->count(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * {@inheritDoc} |
| 80 | + */ |
| 81 | + public function isEmpty(): bool |
| 82 | + { |
| 83 | + return 0 === $this->messages->count(); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @param T $message |
| 88 | + * |
| 89 | + * @throws Exception\ClosedChannelException If the channel is closed. |
| 90 | + * @throws Exception\FullChannelException If the channel is full. |
| 91 | + */ |
| 92 | + public function send(mixed $message): void |
| 93 | + { |
| 94 | + if ($this->closed) { |
| 95 | + throw Exception\ClosedChannelException::forSending(); |
| 96 | + } |
| 97 | + |
| 98 | + if (null === $this->capacity || $this->capacity > $this->count()) { |
| 99 | + $this->messages->enqueue($message); |
| 100 | + |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + throw Exception\FullChannelException::ofCapacity($this->capacity); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @throws Exception\ClosedChannelException If the channel is closed, and there's no more messages to receive. |
| 109 | + * @throws Exception\EmptyChannelException If the channel is empty. |
| 110 | + * |
| 111 | + * @return T |
| 112 | + */ |
| 113 | + public function receive(): mixed |
| 114 | + { |
| 115 | + $empty = 0 === $this->count(); |
| 116 | + $closed = $this->closed; |
| 117 | + if ($closed && $empty) { |
| 118 | + throw Exception\ClosedChannelException::forReceiving(); |
| 119 | + } |
| 120 | + |
| 121 | + if ($empty) { |
| 122 | + throw Exception\EmptyChannelException::create(); |
| 123 | + } |
| 124 | + |
| 125 | + /** @psalm-suppress MissingThrowsDocblock */ |
| 126 | + return $this->messages->dequeue(); |
| 127 | + } |
| 128 | +} |
0 commit comments