Skip to content

Commit

Permalink
create purchase domain event
Browse files Browse the repository at this point in the history
  • Loading branch information
wheesnoza committed Apr 20, 2022
1 parent b1f1a00 commit e521e2f
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 4 deletions.
18 changes: 18 additions & 0 deletions app/Frontend/Purchase/Exceptions/PurchaseFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Frontend\Purchase\Exceptions;

use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;

class PurchaseFailedException extends Exception
{
public function render(): JsonResponse
{
return response()
->json([
'message' => 'The purchase could not be completed. Please wait and try again.'
], Response::HTTP_EXPECTATION_FAILED);
}
}
30 changes: 30 additions & 0 deletions app/Frontend/Purchase/Mail/OrderReceivedNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);

namespace App\Frontend\Purchase\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Src\Frontend\Customer\Domain\Customer;
use Src\Frontend\Purchase\Domain\Purchase;

final class OrderReceivedNotification extends Mailable
{
use Queueable;
use SerializesModels;

public Purchase $purchase;

public function __construct(Purchase $purchase)
{
$this->purchase = $purchase;
}

public function build(): self
{
return $this->view('frontend.purchase.emails.order_received')
->with([
'purchase' => $this->purchase,
]);
}
}
17 changes: 17 additions & 0 deletions app/Frontend/Purchase/Providers/PurchaseEventServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Frontend\Purchase\Providers;


use Illuminate\Foundation\Support\Providers\EventServiceProvider;
use Src\Frontend\Purchase\Domain\PurchaseCreatedDomainEvent;
use Src\Frontend\Purchase\Infrastructure\Mailer\LaravelMailerSendOrderReceivedNotification;

class PurchaseEventServiceProvider extends EventServiceProvider
{
protected $listen = [
PurchaseCreatedDomainEvent::class => [
LaravelMailerSendOrderReceivedNotification::class,
]
];
}
1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
*/
\App\Frontend\Auth\Providers\AuthEventServiceProvider::class,
\App\Frontend\Product\Providers\ProductEventServiceProvider::class,
\App\Frontend\Purchase\Providers\PurchaseEventServiceProvider::class,

],

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
We have received your order for {{ $purchase->uuid() }}
9 changes: 6 additions & 3 deletions src/Frontend/Purchase/Application/Create/PurchaseCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Src\Frontend\Auth\Domain\CustomerAuthRepository;
use Src\Frontend\Customer\Domain\CustomerUuid;
use Src\Frontend\Purchase\Domain\Purchase;
use Src\Frontend\Purchase\Domain\PurchasePriority;
use Src\Frontend\Purchase\Domain\PurchaseQuantity;
Expand All @@ -28,7 +29,7 @@ public function __construct(
PurchaseRepository $purchaseRepository,
EventBus $eventBus,
EnsureVariantStockRepository $ensureVariantStockRepository,
TransactionRepository $transactionRepository
TransactionRepository $transactionRepository,
) {
$this->customerAuthRepository = $customerAuthRepository;
$this->purchaseRepository = $purchaseRepository;
Expand All @@ -37,14 +38,14 @@ public function __construct(
$this->transactionRepository = $transactionRepository;
}

public function __invoke(array $attributes): Purchase
public function __invoke(array $attributes): ?Purchase
{
$variantUuid = new VariantUuid($attributes['variant_uuid']);
$quantity = new PurchaseQuantity($attributes['quantity']);

$purchase = Purchase::create(
PurchaseUuid::generate(),
$this->customerAuthRepository->user()->uuid(),
new CustomerUuid('b5ac9ecb-7173-3f0f-8273-f09bcc0c9846'),
$variantUuid,
PurchaseState::Reserved,
PurchasePriority::from($attributes['priority']),
Expand All @@ -65,6 +66,8 @@ public function __invoke(array $attributes): Purchase
$this->transactionRepository->commit();
} catch (Exception $exception) {
$this->transactionRepository->rollback();

return null;
}

$this->eventBus->publish(...$purchase->pullDomainEvents());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Src\Frontend\Purchase\Application\Listener;

use Src\Frontend\Purchase\Domain\PurchaseCreatedDomainEvent;

interface SendOrderReceivedNotification
{
public function handle(PurchaseCreatedDomainEvent $event): void;
}
6 changes: 5 additions & 1 deletion src/Frontend/Purchase/Domain/Purchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@ public static function create(
PurchasePriority $priority,
PurchaseQuantity $quantity
): self {
return new self(
$purchase = new self(
$uuid,
$customerUuid,
$variantUuid,
$state,
$priority,
$quantity
);

$purchase->record(new PurchaseCreatedDomainEvent($purchase));

return $purchase;
}

public function uuid(): PurchaseUuid
Expand Down
20 changes: 20 additions & 0 deletions src/Frontend/Purchase/Domain/PurchaseCreatedDomainEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

namespace Src\Frontend\Purchase\Domain;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Src\Shared\Domain\Bus\Event\DomainEvent;

final class PurchaseCreatedDomainEvent implements DomainEvent
{
use Dispatchable;
use InteractsWithSockets;

public Purchase $purchase;

public function __construct(Purchase $purchase)
{
$this->purchase = $purchase;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Src\Frontend\Purchase\Infrastructure\Mailer;

use App\Frontend\Purchase\Mail\OrderReceivedNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Mail;
use Src\Frontend\Customer\Domain\CustomerRepository;
use Src\Frontend\Purchase\Application\Listener\SendOrderReceivedNotification;
use Src\Frontend\Purchase\Domain\PurchaseCreatedDomainEvent;

class LaravelMailerSendOrderReceivedNotification implements SendOrderReceivedNotification, ShouldQueue
{
private CustomerRepository $customerRepository;

public function __construct(CustomerRepository $customerRepository)
{
$this->customerRepository = $customerRepository;
}

public function handle(PurchaseCreatedDomainEvent $event): void
{
Mail::to(
$this->customerRepository
->find($event->purchase->customerUuid())
->email()
->value()
)->send(new OrderReceivedNotification($event->purchase));
}
}
8 changes: 8 additions & 0 deletions src/Shared/Domain/Logger/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Src\Shared\Domain\Logger;

interface Logger
{
public function critical(string $message, array $context);
}
14 changes: 14 additions & 0 deletions src/Shared/Infrastructure/Logger/LaravelLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Src\Shared\Infrastructure\Logger;

use Illuminate\Support\Facades\Log;
use Src\Shared\Domain\Logger\Logger;

class LaravelLogger implements Logger
{
public function critical(string $message, array $context)
{
Log::critical($message, $context);
}
}

0 comments on commit e521e2f

Please sign in to comment.