Skip to content

Commit

Permalink
0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammetsafak committed Aug 31, 2024
1 parent e396aa5 commit c5b7155
Show file tree
Hide file tree
Showing 18 changed files with 373 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
namespace App\Commands;
namespace App\Commands\Mail;

use App\Queues\MailQueue;
use App\Queues\NotificationQueue;
use PHPQueueManager\PHPQueueManager\Queue\MessageInterface;
use \Symfony\Component\Console\Input\InputInterface;
use \Symfony\Component\Console\Output\OutputInterface;
use \Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MailConsumer extends Command
{
Expand All @@ -14,17 +14,12 @@ class MailConsumer extends Command

public function execute(InputInterface $input, OutputInterface $output): int
{
$queue = new MailQueue();
$queue = new NotificationQueue();

$queue->consume(function (MessageInterface $message) {
$payload = $message->getPayload();
if (filter_var($payload['to_mail'], FILTER_VALIDATE_EMAIL)) {
sleep(2);
// The workers that will work for NotificationQueue are defined in Message.

return true;
}

return false;
return true;
});

return Command::SUCCESS;
Expand Down
34 changes: 34 additions & 0 deletions App/Commands/Mail/MailProducer.php
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("");
}

}
36 changes: 0 additions & 36 deletions App/Commands/MailProducer.php

This file was deleted.

40 changes: 40 additions & 0 deletions App/Commands/Payments/PaymentConsumer.php
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("");
}

}
31 changes: 31 additions & 0 deletions App/Commands/SMS/SMSConsumer.php
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("");
}

}
37 changes: 37 additions & 0 deletions App/Commands/SMS/SMSProducer.php
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("");
}

}
6 changes: 0 additions & 6 deletions App/Helpers/general_helper.php
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;
}
}
5 changes: 5 additions & 0 deletions App/Helpers/payment_helper.php
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;
}
40 changes: 40 additions & 0 deletions App/Messages/MailMessage.php
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,
]);
}

}
31 changes: 31 additions & 0 deletions App/Messages/SMSMessage.php
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;
}
}

}
32 changes: 32 additions & 0 deletions App/Queues/NotificationQueue.php
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';
}

}
6 changes: 3 additions & 3 deletions App/Queues/MailQueue.php → App/Queues/PaymentQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use PHPQueueManager\PHPQueueManager\Adapters\AdapterFactory;
use PHPQueueManager\PHPQueueManager\Queue\Queue;

class MailQueue extends Queue
class PaymentQueue extends Queue
{

public function __construct()
Expand All @@ -22,12 +22,12 @@ public function __construct()

public function getName(): string
{
return 'mail_queue';
return 'payment_queue';
}

public function getDLQName(): string
{
return 'mail_queue_dead_letter_queue';
return 'payment_queue_dead_letter_queue';
}

}
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ This application was created to easily create and manage your own job queues.
```
composer create-project phpqueuemanager/application
```
Check out the sample template under the "App" directory before you start developing! And then when you are ready to develop your own app you can execute the following command to **destroy everything in the "App" directory**.

```
php qmanager app:truncate
```

You will also need plugins appropriate for the AMQP system you use.

#### RabbitMQ

- [https://pecl.php.net/package/amqp](https://pecl.php.net/package/amqp)
- Include the `php-amqplib/php-amqplib` package in your project.

```
composer require php-amqplib/php-amqplib
```

#### Kafka

- [https://pecl.php.net/package/rdkafka](https://pecl.php.net/package/rdkafka)
- [php.net Documentation](https://arnaud.le-blanc.net/php-rdkafka-doc/phpdoc/rdkafka.setup.html)


## Getting Help
Expand Down
Loading

0 comments on commit c5b7155

Please sign in to comment.