Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 22754d3

Browse files
committed
pull request
2 parents 4f27b4d + 78d68ee commit 22754d3

File tree

7 files changed

+71
-43
lines changed

7 files changed

+71
-43
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,3 @@ composer.lock
199199
mysql
200200

201201
symfony.lock
202-
203-
php8-sf6
204-

config/packages/messenger.yaml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@ framework:
22
messenger:
33
# Uncomment this (and the failed transport below) to send failed messages to this transport for later handling.
44
# failure_transport: failed
5+
default_bus: command.bus
6+
7+
buses:
8+
command.bus: ~
9+
event.bus: ~
510

611
transports:
712
# https://symfony.com/doc/current/messenger.html#transport-configuration
813
async: '%env(MESSENGER_TRANSPORT_DSN)%'
914
# failed: 'doctrine://default?queue_name=failed'
10-
# sync: 'sync://'
15+
sync: 'sync://'
1116

1217
routing:
1318
# Route your messages to the transports
14-
'App\Message\PurchaseConfirmationNotification': async
19+
# 'App\Message\PurchaseConfirmationNotification': async
20+
'App\Message\Command\SaveOrder': sync
21+
'App\Message\Event\OrderSavedEvent': async
1522

1623
# when@test:
1724
# framework:

src/Controller/StockTransactionController.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Controller;
44

5+
use App\Message\Command\SaveOrder;
56
use App\Message\PurchaseConfirmationNotification;
67
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
78
use Symfony\Component\HttpFoundation\Response;
@@ -15,25 +16,25 @@ class StockTransactionController extends AbstractController
1516
public function buy(MessageBusInterface $bus): Response
1617
{
1718
// $notification->getOrder()->getBuyer()->getEmail()
18-
$order = new class {
19-
public function getId()
20-
{
21-
return 1;
22-
}
23-
public function getBuyer(): object
24-
{
25-
return new class {
26-
public function getEmail(): string
27-
{
28-
29-
}
30-
};
31-
}
32-
};
19+
//$order = new class {
20+
// public function getId()
21+
// {
22+
// return 1;
23+
// }
24+
// public function getBuyer(): object
25+
// {
26+
// return new class {
27+
// public function getEmail(): string
28+
// {
29+
// return '[email protected]';
30+
// }
31+
// };
32+
// }
33+
//};
3334

3435

3536
// 1. Dispatch confirmation message
36-
$bus->dispatch(new PurchaseConfirmationNotification($order->getId()));
37+
$bus->dispatch(new SaveOrder());
3738

3839
// 2. Display confirmation to the user
3940
return $this->render('stocks/example.html.twig');

src/Message/Command/SaveOrder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace App\Message\Command;
4+
5+
class SaveOrder
6+
{
7+
8+
}

src/Message/PurchaseConfirmationNotification.php renamed to src/Message/Event/OrderSavedEvent.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace App\Message;
3+
namespace App\Message\Event;
44

5-
class PurchaseConfirmationNotification
5+
class OrderSavedEvent
66
{
77
public function __construct(private int|string $orderId)
88
{
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\MessageHandler\Command;
4+
5+
use App\Message\Command\SaveOrder;
6+
use App\Message\Event\OrderSavedEvent;
7+
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
8+
use Symfony\Component\Messenger\MessageBusInterface;
9+
10+
class SaveOrderHandler implements MessageHandlerInterface
11+
{
12+
public function __construct(private MessageBusInterface $eventBus)
13+
{
14+
}
15+
16+
public function __invoke(SaveOrder $saveOrder)
17+
{
18+
// Save an order to the database
19+
$orderId = 123;
20+
21+
echo 'Order being saved' . PHP_EOL;
22+
23+
// Dispatch an event message on an event bus
24+
$this->eventBus->dispatch(new OrderSavedEvent($orderId));
25+
}
26+
}
Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,39 @@
11
<?php
22

3+
namespace App\MessageHandler\Event;
34

4-
namespace App\MessageHandler;
5-
5+
use App\Message\Event\OrderSavedEvent;
66
use Mpdf\Mpdf;
7-
use Symfony\Component\Mime\Email;
7+
use Mpdf\Output\Destination;
88
use Symfony\Component\Mailer\MailerInterface;
9-
use App\Message\PurchaseConfirmationNotification;
10-
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
119
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
10+
use Symfony\Component\Mime\Email;
1211

13-
14-
class PurchaseConfirmationNotificationHandler implements MessageHandlerInterface
12+
class OrderSavedEventHandler implements MessageHandlerInterface
1513
{
1614
public function __construct(private MailerInterface $mailer)
1715
{
1816
}
1917

20-
public function __invoke(PurchaseConfirmationNotification $notification)
18+
public function __invoke(OrderSavedEvent $event)
2119
{
2220
// 1. Create a PDF contract note
2321
$mpdf = new Mpdf();
24-
$content = "<h1>Contract Note For Order {$notification->getOrderId()}</h1>";
22+
$content = "<h1>Contract Note For Order {$event->getOrderId()}</h1>";
2523
$content .= '<p>Total: <b>$1898.75</b></p>';
2624

2725
$mpdf->writeHtml($content);
28-
$contractNotePdf = $mpdf->output('', 'S');
26+
$contractNotePdf = $mpdf->output('', Destination::STRING_RETURN);
2927

3028
// 2. Email the contract note to the buyer
3129

3230
$email = (new Email())
3331
3432
35-
->subject('Contract note for order ' . $notification->getOrderId())
33+
->subject('Contract note for order ' . $event->getOrderId())
3634
->text('Here is your contract note')
3735
->attach($contractNotePdf, 'contract-note.pdf');
3836

3937
$this->mailer->send($email);
40-
41-
42-
43-
44-
45-
46-
47-
48-
4938
}
5039
}

0 commit comments

Comments
 (0)