-
-
Notifications
You must be signed in to change notification settings - Fork 635
Expand file tree
/
Copy pathCreateOrderHandler.php
More file actions
157 lines (133 loc) · 6.26 KB
/
CreateOrderHandler.php
File metadata and controls
157 lines (133 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
declare(strict_types=1);
namespace HiEvents\Services\Application\Handlers\Order;
use HiEvents\DomainObjects\AffiliateDomainObject;
use HiEvents\DomainObjects\EventDomainObject;
use HiEvents\DomainObjects\EventSettingDomainObject;
use HiEvents\DomainObjects\Generated\AffiliateDomainObjectAbstract;
use HiEvents\DomainObjects\Generated\PromoCodeDomainObjectAbstract;
use HiEvents\DomainObjects\OrderDomainObject;
use HiEvents\DomainObjects\PromoCodeDomainObject;
use HiEvents\DomainObjects\Status\AffiliateStatus;
use HiEvents\DomainObjects\Status\EventStatus;
use HiEvents\Repository\Interfaces\AffiliateRepositoryInterface;
use HiEvents\Repository\Interfaces\EventRepositoryInterface;
use HiEvents\Repository\Interfaces\PromoCodeRepositoryInterface;
use HiEvents\Services\Application\Handlers\Order\DTO\CreateOrderPublicDTO;
use HiEvents\Services\Domain\Order\OrderItemProcessingService;
use HiEvents\Services\Domain\Order\OrderManagementService;
use HiEvents\Services\Domain\Product\AvailableProductQuantitiesFetchService;
use Illuminate\Database\DatabaseManager;
use Illuminate\Validation\UnauthorizedException;
use Illuminate\Validation\ValidationException;
use Throwable;
class CreateOrderHandler
{
public function __construct(
private readonly EventRepositoryInterface $eventRepository,
private readonly PromoCodeRepositoryInterface $promoCodeRepository,
private readonly AffiliateRepositoryInterface $affiliateRepository,
private readonly OrderManagementService $orderManagementService,
private readonly OrderItemProcessingService $orderItemProcessingService,
private readonly AvailableProductQuantitiesFetchService $availableProductQuantitiesFetchService,
private readonly DatabaseManager $databaseManager,
)
{
}
/**
* @throws Throwable
*/
public function handle(
int $eventId,
CreateOrderPublicDTO $createOrderPublicDTO,
bool $deleteExistingOrdersForSession = true
): OrderDomainObject
{
return $this->databaseManager->transaction(function () use ($eventId, $createOrderPublicDTO, $deleteExistingOrdersForSession) {
$this->databaseManager->statement('SELECT pg_advisory_xact_lock(?)', [$eventId]);
$event = $this->eventRepository
->loadRelation(EventSettingDomainObject::class)
->findById($eventId);
$this->validateEventStatus($event, $createOrderPublicDTO);
$promoCode = $this->getPromoCode($createOrderPublicDTO, $eventId);
$affiliate = $this->getAffiliate($createOrderPublicDTO, $eventId);
if ($deleteExistingOrdersForSession) {
$this->orderManagementService->deleteExistingOrders($eventId, $createOrderPublicDTO->session_identifier);
}
$this->validateProductAvailability($eventId, $createOrderPublicDTO);
$order = $this->orderManagementService->createNewOrder(
eventId: $eventId,
event: $event,
timeOutMinutes: $event->getEventSettings()?->getOrderTimeoutInMinutes(),
locale: $createOrderPublicDTO->order_locale,
promoCode: $promoCode,
affiliate: $affiliate,
sessionId: $createOrderPublicDTO->session_identifier,
);
$orderItems = $this->orderItemProcessingService->process(
order: $order,
productsOrderDetails: $createOrderPublicDTO->products,
event: $event,
promoCode: $promoCode,
);
return $this->orderManagementService->updateOrderTotals($order, $orderItems);
});
}
private function getPromoCode(CreateOrderPublicDTO $createOrderPublicDTO, int $eventId): ?PromoCodeDomainObject
{
if ($createOrderPublicDTO->promo_code === null) {
return null;
}
$promoCode = $this->promoCodeRepository->findFirstWhere([
PromoCodeDomainObjectAbstract::CODE => strtolower(trim($createOrderPublicDTO->promo_code)),
PromoCodeDomainObjectAbstract::EVENT_ID => $eventId,
]);
if ($promoCode?->isValid()) {
return $promoCode;
}
return null;
}
private function getAffiliate(CreateOrderPublicDTO $createOrderPublicDTO, int $eventId): ?AffiliateDomainObject
{
if ($createOrderPublicDTO->affiliate_code === null) {
return null;
}
return $this->affiliateRepository->findFirstWhere([
AffiliateDomainObjectAbstract::CODE => strtoupper(trim($createOrderPublicDTO->affiliate_code)),
AffiliateDomainObjectAbstract::EVENT_ID => $eventId,
AffiliateDomainObjectAbstract::STATUS => AffiliateStatus::ACTIVE->value,
]);
}
public function validateEventStatus(EventDomainObject $event, CreateOrderPublicDTO $createOrderPublicDTO): void
{
if (!$createOrderPublicDTO->is_user_authenticated && $event->getStatus() !== EventStatus::LIVE->name) {
throw new UnauthorizedException(
__('This event is not live.')
);
}
}
/**
* @throws ValidationException
*/
private function validateProductAvailability(int $eventId, CreateOrderPublicDTO $createOrderPublicDTO): void
{
$availability = $this->availableProductQuantitiesFetchService
->getAvailableProductQuantities($eventId, ignoreCache: true);
foreach ($createOrderPublicDTO->products as $product) {
foreach ($product->quantities as $priceQuantity) {
if ($priceQuantity->quantity <= 0) {
continue;
}
$available = $availability->productQuantities
->where('product_id', $product->product_id)
->where('price_id', $priceQuantity->price_id)
->first()?->quantity_available ?? 0;
if ($priceQuantity->quantity > $available) {
throw ValidationException::withMessages([
'products' => __('Not enough products available. Please try again.'),
]);
}
}
}
}
}