This repository has been archived by the owner on Jan 16, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create Notification data class, setup Rabbit MQ architecture
Currently creates two consumers for email and web notifications
- Loading branch information
Showing
6 changed files
with
365 additions
and
0 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,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; | ||
} | ||
|
||
?> |
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,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; | ||
} | ||
} | ||
|
||
?> |
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 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; | ||
} | ||
} | ||
|
||
?> |
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,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; | ||
} | ||
} |