Skip to content

Instantly share code, notes, and snippets.

@kevinmlong
Last active June 1, 2017 14:15
Show Gist options
  • Save kevinmlong/439bb65f259c2d13ae2c55f03a416c1b to your computer and use it in GitHub Desktop.
Save kevinmlong/439bb65f259c2d13ae2c55f03a416c1b to your computer and use it in GitHub Desktop.
AbstractCarrier Class for PHP Event Bus Using RabbitMQ
<?php
abstract class AbstractCarrier {
protected $connection;
protected $channel;
protected $queueName;
public function __construct($queueName, $host = 'localhost', $port = 5672, $username = 'guest', $password = 'guest') {
$this->queueName = $queueName;
$this->connection = new AMQPStreamConnection($host, $port, $username, $password);
$this->channel = $this->connection->channel();
$this->channel->queue_declare($queueName, false, false, false, false);
}
/**
* This method is called by the script that implements the carrier. It is blocking.
* The call to basic_consume calls the 'receivedParcel' method which is implmented by the subclasses
*/
public function waitForMail() {
$this->channel->basic_consume($this->queueName, $this->queueName, false,false,false,false, [$this, 'receivedParcel']);
while (count($this->channel->callbacks)) {
$this->channel->wait();
}
$this->channel->close();
$this->connection->close();
}
/* Stub to be implemented by sub classes */
protected function receivedParcel($msg){}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment