Skip to content

02.Adapter

Muhammet ŞAFAK edited this page Aug 31, 2024 · 1 revision

Adapter

Adapters are abstraction layers that provide standard communication with a message broker service. Each adapter must implement the \PHPQueueManager\PHPQueueManager\Adapters\AdapterInterface interface.

You can see an example adapter class below. Writing an adapter is pretty straightforward, but you still need to make sure you know what you're doing.

namespace App\Adapters;

use \PHPQueueManager\PHPQueueManager\Adapters\{AbstractAdapter, AdapterInterface};
use \PHPQueueManager\PHPQueueManager\Exceptions\{DeadLetterQueueException, 
    ReTryQueueException};
use \PHPQueueManager\PHPQueueManager\Queue\{JobMessageInterface,
    Message,
    MessageInterface,
    QueueInterface};

class MyQueueAdapter extends AbstractAdapter implements AdapterInterface
{

    /**
     * @inheritDoc
     */
    public function connect(): bool
    {
        try {
            // TODO : Open Connection
            
            return true;
        } catch (\Throwable $e) {
            return false;
        }
    }

    /**
     * @inheritDoc
     */
    public function queueDeclare(QueueInterface $queue): self
    {
        // TODO : Queue Declare
        
        return $this;
    }

    /**
     * @inheritDoc
     */
    public function publish(MessageInterface $message): bool
    {
        try {
            // TODO : Add a New Job to the Queue!

            return true;
        } catch (\Throwable $e) {
            return false;
        }
    }

    /**
     * @inheritDoc
     */
    public function consume(\Closure $worker)
    {
        try {
            // I strongly recommend that you review previous adapters while writing this method.
            // TODO : Consume Queued Messages!
        } catch (\Throwable $e) {
        }
    }

    /**
     * @inheritDoc
     */
    public function close(): bool
    {
        // TODO : Close Connection
        
        return true;
    }

    /**
     * @inheritDoc
     */
    public function retry(MessageInterface $message): void
    {
        $message->retryNotification();
        // TODO : Set Message to Try Again
    }

    /**
     * @inheritDoc
     */
    public function addDeadLetterQueue(MessageInterface $message): void
    {
        try {
            $message->deadLetterNotification();
            // TODO : Write to Death Letter Queue.
        } catch (\Throwable $e) {
        }
    }

}
Clone this wiki locally