-
-
Notifications
You must be signed in to change notification settings - Fork 655
Expand file tree
/
Copy pathUpdateEventHandler.php
More file actions
131 lines (113 loc) · 4.55 KB
/
UpdateEventHandler.php
File metadata and controls
131 lines (113 loc) · 4.55 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
<?php
namespace HiEvents\Services\Application\Handlers\Event;
use HiEvents\DataTransferObjects\AttributesDTO;
use HiEvents\DomainObjects\EventDomainObject;
use HiEvents\DomainObjects\Status\OrderStatus;
use HiEvents\Events\Dispatcher;
use HiEvents\Events\EventUpdateEvent;
use HiEvents\Exceptions\CannotChangeCurrencyException;
use HiEvents\Helper\DateHelper;
use HiEvents\Repository\Interfaces\EventRepositoryInterface;
use HiEvents\Repository\Interfaces\OrderRepositoryInterface;
use HiEvents\Services\Application\Handlers\Event\DTO\UpdateEventDTO;
use HiEvents\Services\Infrastructure\HtmlPurifier\HtmlPurifierService;
use HiEvents\Jobs\Event\Webhook\DispatchEventWebhookJob;
use HiEvents\Services\Infrastructure\DomainEvents\Enums\DomainEventType;
use Illuminate\Database\DatabaseManager;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Throwable;
readonly class UpdateEventHandler
{
public function __construct(
private EventRepositoryInterface $eventRepository,
private Dispatcher $dispatcher,
private DatabaseManager $databaseManager,
private OrderRepositoryInterface $orderRepository,
private HtmlPurifierService $purifier,
)
{
}
/**
* @throws Throwable
*/
public function handle(UpdateEventDTO $eventData): EventDomainObject
{
return $this->databaseManager->transaction(function () use ($eventData) {
$this->updateEventAttributes($eventData);
return $this->getUpdateEvent($eventData);
});
}
private function fetchExistingEvent(UpdateEventDTO $eventData)
{
$existingEvent = $this->eventRepository->findFirstWhere([
'id' => $eventData->id,
'account_id' => $eventData->account_id,
]);
if ($existingEvent === null) {
throw new ResourceNotFoundException(
__('Event :id not found', ['id' => $eventData->id])
);
}
return $existingEvent;
}
/**
* @throws CannotChangeCurrencyException
*/
private function updateEventAttributes(UpdateEventDTO $eventData): void
{
$existingEvent = $this->fetchExistingEvent($eventData);
if ($eventData->currency !== null && $eventData->currency !== $existingEvent->getCurrency()) {
$this->checkForCompletedOrders($eventData);
}
$this->eventRepository->updateWhere(
attributes: [
'title' => $eventData->title,
'category' => $eventData->category?->value ?? $existingEvent->getCategory(),
'start_date' => DateHelper::convertToUTC($eventData->start_date, $eventData->timezone),
'end_date' => $eventData->end_date
? DateHelper::convertToUTC($eventData->end_date, $eventData->timezone)
: null,
'description' => $this->purifier->purify($eventData->description),
'timezone' => $eventData->timezone ?? $existingEvent->getTimezone(),
'currency' => $eventData->currency ?? $existingEvent->getCurrency(),
'location' => $eventData->location,
'location_details' => $eventData->location_details?->toArray(),
'attributes' => $eventData->attributes
? $eventData->attributes->map(fn(AttributesDTO $a) => $a->toArray())->all()
: $existingEvent->getAttributes(),
],
where: [
'id' => $eventData->id,
'account_id' => $eventData->account_id,
],
);
}
private function getUpdateEvent(UpdateEventDTO $eventData): EventDomainObject
{
$event = $this->eventRepository->findFirstWhere([
'id' => $eventData->id,
'account_id' => $eventData->account_id,
]);
$this->dispatcher->dispatchEvent(new EventUpdateEvent($event));
DispatchEventWebhookJob::dispatch(
$event->getId(),
DomainEventType::EVENT_UPDATED,
);
return $event;
}
/**
* @throws CannotChangeCurrencyException
*/
private function checkForCompletedOrders(UpdateEventDTO $eventData): void
{
$orders = $this->orderRepository->findWhere([
'event_id' => $eventData->id,
'status' => OrderStatus::COMPLETED->name,
]);
if (!$orders->isNotEmpty()) {
throw new CannotChangeCurrencyException(
__('You cannot change the currency of an event that has completed orders'),
);
}
}
}