tenweb-speed-optimizer
/
vendor
/
10web-utils
/
10web-queue
/
src
/
TenWebQueue
/
Abstracts
/
QueueProducerAbstract.php
tenweb-speed-optimizer
/
vendor
/
10web-utils
/
10web-queue
/
src
/
TenWebQueue
/
Abstracts
Last commit date
QueueConsumerAbstract.php
3 years ago
QueueProducerAbstract.php
4 years ago
QueueProducerAbstract.php
74 lines
| 1 | <?php |
| 2 | namespace TenWebQueue\Abstracts; |
| 3 | |
| 4 | use Interop\Queue\Exception; |
| 5 | use Interop\Queue\Exception\InvalidDestinationException; |
| 6 | use Interop\Queue\Exception\InvalidMessageException; |
| 7 | use Interop\Queue\Producer; |
| 8 | use TenWebQueue\Interfaces\QueueProducerInterface; |
| 9 | use TenWebQueue\QueueContext; |
| 10 | |
| 11 | abstract class QueueProducerAbstract implements QueueProducerInterface |
| 12 | { |
| 13 | /** |
| 14 | * @var QueueContext |
| 15 | */ |
| 16 | private $queueContext; |
| 17 | |
| 18 | /** |
| 19 | * @var Producer |
| 20 | */ |
| 21 | private $producer; |
| 22 | |
| 23 | |
| 24 | /** |
| 25 | * @param QueueContext $context |
| 26 | */ |
| 27 | public function __construct($context) |
| 28 | { |
| 29 | $this->queueContext = $context; |
| 30 | $this->producer = $this->queueContext->getContext()->createProducer(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @param array $data |
| 35 | * |
| 36 | * @return void |
| 37 | * @throws Exception |
| 38 | * @throws InvalidDestinationException |
| 39 | * @throws InvalidMessageException |
| 40 | */ |
| 41 | public function enqueueOne($data = array()) |
| 42 | { |
| 43 | $this->enqueue(json_encode($data)); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param array $data |
| 48 | * |
| 49 | * @return void |
| 50 | * @throws Exception |
| 51 | * @throws InvalidDestinationException |
| 52 | * @throws InvalidMessageException |
| 53 | */ |
| 54 | public function enqueueMany($data = array()) |
| 55 | { |
| 56 | foreach ($data as $item) { |
| 57 | $this->enqueue(json_encode($item)); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @param $data |
| 63 | * |
| 64 | * @return void |
| 65 | * @throws Exception |
| 66 | * @throws InvalidDestinationException |
| 67 | * @throws InvalidMessageException |
| 68 | */ |
| 69 | public function enqueue($data) |
| 70 | { |
| 71 | $message = $this->queueContext->getContext()->createMessage(json_encode($data)); |
| 72 | $this->producer->send($this->queueContext->getQueue(), $message); |
| 73 | } |
| 74 | } |