This repository has been archived by the owner on Jan 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
setup Rabbit MQ architecture with respective consumers
- Loading branch information
Showing
7 changed files
with
232 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace App\Consumer; | ||
|
||
use FOS\UserBundle\Model\UserManagerInterface; | ||
use App\Util\NotificationHydrator; | ||
|
||
/** | ||
* Class AbstractNotificationConsumer | ||
* | ||
* Base class for all the Notification Consumers that we have | ||
* | ||
* @author Aquib Baig <aquibbaig97@gmail.com> | ||
*/ | ||
abstract class AbstractNotificationConsumer | ||
{ | ||
/** | ||
* @var UserManagerInterface | ||
*/ | ||
protected $userManager; | ||
|
||
/** | ||
* AbstractNotificationConsumer constructor. | ||
* @param UserManagerInterface $userManager | ||
*/ | ||
public function __construct(UserManagerInterface $userManager) | ||
{ | ||
$this->userManager = $userManager; | ||
} | ||
|
||
/** | ||
* Decides if a notification should be handled by a particular Consumer | ||
* or not by returning a boolean value | ||
* | ||
* @param NotificationHydrator $notification | ||
* | ||
* @return bool | ||
*/ | ||
abstract protected function shouldHandle(NotificationHydrator $notification): bool; | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace App\Consumer; | ||
|
||
use FOS\UserBundle\Model\UserManagerInterface; | ||
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface; | ||
use PhpAmqpLib\Message\AMQPMessage; | ||
use App\Util\NotificationHydrator; | ||
|
||
/** | ||
* Class EmailNotificationConsumer | ||
* | ||
* This class is responsible for sending out Notifications | ||
* to the user's email | ||
* | ||
* @author Aquib Baig <aquibbaig97@gmail.com> | ||
*/ | ||
class EmailNotificationConsumer extends AbstractNotificationConsumer implements ConsumerInterface | ||
{ | ||
/** | ||
* EmailNotificationConsumer constructor. | ||
* @param UserManagerInterface $userManager | ||
*/ | ||
public function __construct(UserManagerInterface $userManager) | ||
{ | ||
parent::__construct($userManager); | ||
} | ||
|
||
/** | ||
* Find the user that created the notification | ||
* and persist it in the database | ||
* | ||
* @param AMQPMessage $msg | ||
* | ||
* @throws \Exception | ||
*/ | ||
public function execute(AMQPMessage $msg) | ||
{ | ||
echo "\nEmail Notification Consumer\n"; | ||
} | ||
|
||
/** | ||
* @param NotificationHydrator $notification | ||
* | ||
* @return bool | ||
*/ | ||
protected function shouldHandle(NotificationHydrator $notification): bool | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace App\Consumer; | ||
|
||
use App\Util\NotificationHydrator; | ||
use FOS\UserBundle\Model\UserManagerInterface; | ||
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface; | ||
use PhpAmqpLib\Message\AMQPMessage; | ||
|
||
/** | ||
* Class WebNotificationConsumer | ||
* | ||
* This class stores Notifications to the database | ||
* and sends them out to the UI | ||
* | ||
* @author Aquib Baig <aquibbaig97@gmail.com> | ||
*/ | ||
class WebNotificationConsumer extends AbstractNotificationConsumer implements ConsumerInterface | ||
{ | ||
/** | ||
* WebNotificationConsumer constructor. | ||
* @param UserManagerInterface $userManager | ||
*/ | ||
public function __construct(UserManagerInterface $userManager) | ||
{ | ||
parent::__construct($userManager); | ||
} | ||
|
||
/** | ||
* Find the user that created the notification | ||
* and persist it in the database | ||
* | ||
* @param AMQPMessage $msg | ||
* | ||
* @throws \Exception | ||
*/ | ||
public function execute(AMQPMessage $msg) | ||
{ | ||
echo "\nWeb Notification Consumer\n"; | ||
} | ||
|
||
/** | ||
* @param NotificationHydrator $notification | ||
* | ||
* @return bool | ||
*/ | ||
protected function shouldHandle(NotificationHydrator $notification): bool | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace App\Service; | ||
|
||
use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface; | ||
use App\Util\NotificationHydrator; | ||
|
||
/** | ||
* Class NotificationProducer | ||
* | ||
* This class publishes a Notification to the RabbitMQ queue | ||
* which is then taken up by the Consumers | ||
* | ||
* @author Aquib Baig <aquibbaig97@gmail.com> | ||
*/ | ||
class NotificationProducerService | ||
{ | ||
/** | ||
* Holds the notificationProducer service | ||
* | ||
* @var ProducerInterface | ||
*/ | ||
private $notificationProducer; | ||
|
||
/** | ||
* NotificationService constructor. | ||
* | ||
* @param ProducerInterface $notificationProducer | ||
*/ | ||
public function __construct(ProducerInterface $notificationProducer) | ||
{ | ||
$this->notificationProducer = $notificationProducer; | ||
} | ||
|
||
/** | ||
* Publishes a Notification to the RabbitMQ Queue | ||
* | ||
* @param NotificationHydrator $notification | ||
*/ | ||
public function publishNotification(NotificationHydrator $notification) | ||
{ | ||
$this->notificationProducer->publish((serialize($notification))); | ||
} | ||
} | ||
|
||
?> |