Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
create Notification data class, setup Rabbit MQ architecture
Browse files Browse the repository at this point in the history
Currently creates two consumers for email and web notifications
  • Loading branch information
aquibbaig committed Jul 4, 2019
1 parent 78f9175 commit 30d8eff
Show file tree
Hide file tree
Showing 6 changed files with 365 additions and 0 deletions.
14 changes: 14 additions & 0 deletions site/app/config/packages/old_sound_rabbit_mq.yml
Expand Up @@ -19,6 +19,9 @@ old_sound_rabbit_mq:
update_project_info:
connection: default
exchange_options: {name: 'update-project-info', type: fanout}
notification:
connection: default
exchange_options: {name: 'notification', type: fanout}
consumers:
update_project_info:
connection: default
Expand All @@ -30,3 +33,14 @@ old_sound_rabbit_mq:
exchange_options: {name: 'update-project-info', type: fanout}
queue_options: {name: 'update-github-metadata'}
callback: update_github_metadata_consumer
multiple_consumers:
notification:
connection: default
exchange_options: {name: 'notification', type: fanout}
queues:
web_notification:
name: 'web-notification'
callback: web_notification_consumer
email_notification:
name: 'email-notification'
callback: email_notification_consumer
2 changes: 2 additions & 0 deletions site/app/config/services.yml
Expand Up @@ -27,6 +27,8 @@ services:
# Required legacy-style aliases
update_project_info_consumer: '@App\Consumer\UpdateProjectInformation'
update_github_metadata_consumer: '@App\Consumer\UpdateGitHubMetadataConsumer'
web_notification_consumer: '@App\Consumer\WebNotificationConsumer'
email_notification_consumer: '@App\Consumer\EmailNotificationConsumer'

# Explicit configurations
# TODO: Investigate and deprecate these in favor of auto-wiring
Expand Down
89 changes: 89 additions & 0 deletions site/src/Consumer/AbstractNotificationConsumer.php
@@ -0,0 +1,89 @@
<?php

namespace App\Consumer;

use App\Util\Notification;
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
use PhpAmqpLib\Message\AMQPMessage;
use Psr\Log\LoggerInterface;
use Exception;

/**
* Class AbstractNotificationConsumer
*
* Base class for all the Notification Consumers that we have
*
* @author Aquib Baig <aquibbaig97@gmail.com>
*/
abstract class AbstractNotificationConsumer implements ConsumerInterface
{
/**
* @var LoggerInterface
*/
protected $logger;

/**
* @var Notification $notification
*/
protected $notification;

/**
* AbstractNotificationConsumer constructor.
* @param LoggerInterface $logger
* @param Notification $notification
*/
public function __construct(LoggerInterface $logger, Notification $notification)
{
$this->logger = $logger;
$this->notification = $notification;
}

/**
* @param AMQPMessage $msg
*
* @return bool
*
* @throws Exception
*/
public function execute(AMQPMessage $msg)
{
$this->notification = unserialize($msg->body);

try {
if ($this->shouldHandle()) {
return $this->handle();
}

return true; //don't requeue
} catch (Exception $e) {
// Log out exceptions if they occur, and keep Consumers running for
// next requests
$this->logger->error(
"Processing the Notification resulted in an ".get_class($e)
);
$this->logger->error("Message: ".$e->getMessage());
$this->logger->error("Trace: ".$e->getTraceAsString());

return false;
}
}

/**
* Should a Notification be handled by a the Consumer?
*
* @return bool
*/
protected function shouldHandle()
{
return true;
}

/**
* Actually processes a Notification to its sink
*
* @return mixed
*/
abstract protected function handle():bool;
}

?>
41 changes: 41 additions & 0 deletions site/src/Consumer/EmailNotificationConsumer.php
@@ -0,0 +1,41 @@
<?php

namespace App\Consumer;

use App\Util\Notification;
use Psr\Log\LoggerInterface;

/**
* Class EmailNotificationConsumer
*
* Sends out Notifications over email
*
* @author Aquib Baig <aquibbaig97@gmail.com>
*/
class EmailNotificationConsumer extends AbstractNotificationConsumer
{
/**
* EmailNotificationConsumer constructor.
* @param LoggerInterface $logger
* @param Notification $notification
*/
public function __construct(LoggerInterface $logger, Notification $notification)
{
parent::__construct($logger, $notification);
}

/**
* Handles an email Notification
*
* @return bool
*/
protected function handle(): bool
{
// TODO: Process the Notification($this->notification)
echo "Email Notification Consumer";

return true;
}
}

?>
42 changes: 42 additions & 0 deletions site/src/Consumer/WebNotificationConsumer.php
@@ -0,0 +1,42 @@
<?php

namespace App\Consumer;

use App\Util\Notification;
use Psr\Log\LoggerInterface;

/**
* Class WebNotificationConsumer
*
* This class persists Notifications to the database
* and sends them out to the UI
*
* @author Aquib Baig <aquibbaig97@gmail.com>
*/
class WebNotificationConsumer extends AbstractNotificationConsumer
{
/**
* WebNotificationConsumer constructor.
* @param LoggerInterface $logger
* @param Notification $notification
*/
public function __construct(LoggerInterface $logger, Notification $notification)
{
parent::__construct($logger, $notification);
}

/**
* Handles Notifications that will be sent to UI
*
* @return bool
*/
protected function handle(): bool
{
// TODO: Process the Notification($this->notification)
echo "Web Notification Consumer";

return true;
}
}

?>
177 changes: 177 additions & 0 deletions site/src/Util/Notification.php
@@ -0,0 +1,177 @@
<?php

namespace App\Util;

use App\Entity\User;
use DateTime;

/**
* Data class for all Notifications
*
* @author Aquib Baig <aquibbaig97@gmail.com>
*/
class Notification
{
/**
* Subject of the Notification
*
* @var string $subject
*/
protected $subject;

/**
* Actual definition of the Notification
*
* @var string $message
*/
protected $message;

/**
* User associated with the Notification
*
* @var User $recipient
*/
protected $recipient;

/**
* The type of Notification
*
* @var string $type
*/
protected $type;

/**
* When is the Notification created
*
* @var DateTime $createdAt
*/
protected $createdAt;

/**
* Notification constructor.
*/
public function __construct()
{
$this->setCreatedAt(new DateTime());
}

/**
* Gets the Notification Subject
*
* @return string
*/
public function getSubject(): string
{
return $this->subject;
}

/**
* Sets the Notification Subject
*
* @param string $subject
*
* @return $this
*/
public function setSubject(string $subject)
{
$this->subject = $subject;

return $this;
}

/**
* Get the Notification Message
*
* @return string
*/
public function getMessage(): string
{
return $this->message;
}

/**
* Set the Notification Message
*
* @param string $message
*
* @return $this
*/
public function setMessage(string $message)
{
$this->message = $message;

return $this;
}

/**
* Returns the type of the Notification
*
* @return string
*/
public function getType(): string
{
return $this->type;
}

/**
* Set the Notification type
*
* @param string $type
*
* @return $this
*/
public function setType(string $type)
{
$this->type = $type;

return $this;
}

/**
* Returns User associated with a Notification
*
* @return User
*/
public function getRecipient(): User
{
return $this->recipient;
}

/**
* Used to bind users to any Notification
*
* @param User $recipient
*
* @return $this
*/
public function setRecipient(User $recipient)
{
$this->recipient = $recipient;

return $this;
}

/**
* Gets the timestamp of a Notification
*
* @return DateTime
*/
public function getCreatedAt(): DateTime
{
return $this->createdAt;
}

/**
* Sets the timestamp of a Notification
*
* @param DateTime $createdAt
*
* @return $this
*/
public function setCreatedAt(DateTime $createdAt)
{
$this->createdAt = $createdAt;

return $this;
}
}

0 comments on commit 30d8eff

Please sign in to comment.