-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e396aa5
commit c5b7155
Showing
18 changed files
with
373 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
namespace App\Commands\Mail; | ||
use App\Messages\MailMessage; | ||
use App\Queues\NotificationQueue; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class MailProducer extends Command | ||
{ | ||
|
||
protected static $defaultName = "produce:mail"; | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$queue = new NotificationQueue(); | ||
|
||
$message = new MailMessage(); | ||
|
||
for ($i = 0; $i < 100; ++$i) { | ||
$message->mail('example' . $i . '@example.com', 'Welcome ' . $i, ''); | ||
|
||
$queue->publish($message); | ||
} | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
public function configure(): void | ||
{ | ||
$this->setDescription(""); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
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,40 @@ | ||
<?php | ||
namespace App\Commands\Payments; | ||
use App\Queues\PaymentQueue; | ||
use PHPQueueManager\PHPQueueManager\Queue\MessageInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class PaymentConsumer extends Command | ||
{ | ||
|
||
protected static $defaultName = "consume:payment"; | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$queue = new PaymentQueue(); | ||
|
||
$queue->consume(function (MessageInterface $message) { | ||
try { | ||
$payload = $message->getPayload(); | ||
|
||
return credit_card_payment($payload['credit_card_number'], | ||
$payload['credit_card_holder_name'], | ||
$payload['credit_card_expiry'], | ||
$payload['credit_card_cvv'], $payload['amount']); | ||
|
||
} catch (\Throwable $e) { | ||
return false; | ||
} | ||
}); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
public function configure(): void | ||
{ | ||
$this->setDescription(""); | ||
} | ||
|
||
} |
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,31 @@ | ||
<?php | ||
namespace App\Commands\SMS; | ||
use App\Queues\NotificationQueue; | ||
use PHPQueueManager\PHPQueueManager\Queue\MessageInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class SMSConsumer extends Command | ||
{ | ||
|
||
protected static $defaultName = "consume:sms"; | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$queue = new NotificationQueue(); | ||
|
||
$queue->consume(function (MessageInterface $message) { | ||
// The workers that will work for NotificationQueue are defined in Message. | ||
|
||
return true; | ||
}); | ||
return Command::SUCCESS; | ||
} | ||
|
||
public function configure(): void | ||
{ | ||
$this->setDescription(""); | ||
} | ||
|
||
} |
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,37 @@ | ||
<?php | ||
namespace App\Commands\SMS; | ||
use App\Messages\SMSMessage; | ||
use App\Queues\NotificationQueue; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class SMSProducer extends Command | ||
{ | ||
|
||
protected static $defaultName = "produce:sms"; | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
|
||
$queue = new NotificationQueue(); | ||
|
||
$message = new SMSMessage(); | ||
|
||
for ($i = 10; $i < 100; ++$i) { | ||
$message->setPayload([ | ||
'phone_number' => '+9011111111' . $i | ||
]); | ||
|
||
$queue->publish($message); | ||
} | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
public function configure(): void | ||
{ | ||
$this->setDescription(""); | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -1,7 +1 @@ | ||
<?php | ||
if (!function_exists('env')) { | ||
function env(string $name, mixed $default = null): mixed | ||
{ | ||
return $_ENV[$name] ?? $default; | ||
} | ||
} |
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,5 @@ | ||
<?php | ||
function credit_card_payment(string $number, string $holderName, string $expiry, int $cvv, float $amount): 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,40 @@ | ||
<?php | ||
|
||
namespace App\Messages; | ||
|
||
use PHPQueueManager\PHPQueueManager\Queue\JobMessageInterface; | ||
use PHPQueueManager\PHPQueueManager\Queue\Message; | ||
use PHPQueueManager\PHPQueueManager\Queue\MessageInterface; | ||
|
||
class MailMessage extends Message implements JobMessageInterface | ||
{ | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function worker(\PHPQueueManager\PHPQueueManager\Queue\MessageInterface $message): bool | ||
{ | ||
try { | ||
$payload = $message->getPayload(); | ||
if (filter_var($payload['to_mail'], FILTER_VALIDATE_EMAIL)) { | ||
sleep(2); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} catch (\Throwable $e) { | ||
return false; | ||
} | ||
} | ||
|
||
public function mail(string $toMail, string $subject, string $body) | ||
{ | ||
$this->setPayload([ | ||
'to_mail' => $toMail, | ||
'subject' => $subject, | ||
'body' => $body, | ||
]); | ||
} | ||
|
||
} |
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,31 @@ | ||
<?php | ||
|
||
namespace App\Messages; | ||
|
||
use PHPQueueManager\PHPQueueManager\Queue\JobMessageInterface; | ||
use PHPQueueManager\PHPQueueManager\Queue\Message; | ||
use PHPQueueManager\PHPQueueManager\Queue\MessageInterface; | ||
|
||
class SMSMessage extends Message implements JobMessageInterface | ||
{ | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function worker(\PHPQueueManager\PHPQueueManager\Queue\MessageInterface $message): bool | ||
{ | ||
try { | ||
$payload = $message->getPayload(); | ||
if (!empty($payload['phone_number'])) { | ||
sleep(2); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} catch (\Throwable $e) { | ||
return false; | ||
} | ||
} | ||
|
||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace App\Queues; | ||
|
||
use PHPQueueManager\PHPQueueManager\Adapters\AdapterFactory; | ||
use PHPQueueManager\PHPQueueManager\Queue\Queue; | ||
|
||
class NotificationQueue extends Queue | ||
{ | ||
public function __construct() | ||
{ | ||
$adapter = AdapterFactory::create('rabbitmq', [ | ||
'host' => env("RABBITMQ_HOST", "localhost"), | ||
'port' => env("RABBITMQ_PORT", 5672), | ||
'username' => env("RABBITMQ_USER", "guest"), | ||
'password' => env("RABBITMQ_PASS", "guest"), | ||
]); | ||
|
||
parent::__construct($adapter); | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'notification_queue'; | ||
} | ||
|
||
public function getDLQName(): string | ||
{ | ||
return 'notification_queue_dead_letter_queue'; | ||
} | ||
|
||
} |
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
Oops, something went wrong.