PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.20
Booking for Appointments and Events Calendar – Amelia v1.2.20
2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Application / Commands / Notification / UpdateNotificationCommandHandler.php
ameliabooking / src / Application / Commands / Notification Last commit date
GetNotificationsCommand.php 7 years ago GetNotificationsCommandHandler.php 2 years ago GetSMSNotificationsHistoryCommand.php 7 years ago GetSMSNotificationsHistoryCommandHandler.php 2 years ago SendAmeliaSmsApiRequestCommand.php 7 years ago SendAmeliaSmsApiRequestCommandHandler.php 2 years ago SendTestEmailCommand.php 7 years ago SendTestEmailCommandHandler.php 2 years ago SendUndeliveredNotificationsCommand.php 4 years ago SendUndeliveredNotificationsCommandHandler.php 2 years ago UpdateNotificationCommand.php 7 years ago UpdateNotificationCommandHandler.php 2 years ago UpdateNotificationStatusCommand.php 7 years ago UpdateNotificationStatusCommandHandler.php 2 years ago UpdateSMSNotificationHistoryCommand.php 7 years ago UpdateSMSNotificationHistoryCommandHandler.php 1 year ago
UpdateNotificationCommandHandler.php
157 lines
1 <?php
2
3 namespace AmeliaBooking\Application\Commands\Notification;
4
5 use AmeliaBooking\Application\Commands\CommandHandler;
6 use AmeliaBooking\Application\Commands\CommandResult;
7 use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException;
8 use AmeliaBooking\Application\Services\Entity\EntityApplicationService;
9 use AmeliaBooking\Application\Services\Notification\NotificationHelperService;
10 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
11 use AmeliaBooking\Domain\Entity\Entities;
12 use AmeliaBooking\Domain\Entity\Notification\Notification;
13 use AmeliaBooking\Domain\Factory\Notification\NotificationFactory;
14 use AmeliaBooking\Domain\ValueObjects\Json;
15 use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException;
16 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
17 use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventRepository;
18 use AmeliaBooking\Infrastructure\Repository\Notification\NotificationRepository;
19 use AmeliaBooking\Infrastructure\Repository\Notification\NotificationsToEntitiesRepository;
20 use \Interop\Container\Exception\ContainerException;
21 use Slim\Exception\ContainerValueNotFoundException;
22
23 /**
24 * Class UpdateNotificationCommandHandler
25 *
26 * @package AmeliaBooking\Application\Commands\Notification
27 */
28 class UpdateNotificationCommandHandler extends CommandHandler
29 {
30 public $mandatoryFields = [
31 'subject',
32 'content'
33 ];
34
35 /**
36 * @param UpdateNotificationCommand $command
37 *
38 * @return CommandResult
39 * @throws ContainerValueNotFoundException
40 * @throws QueryExecutionException
41 * @throws NotFoundException
42 * @throws InvalidArgumentException
43 * @throws AccessDeniedException
44 * @throws ContainerException
45 */
46 public function handle(UpdateNotificationCommand $command)
47 {
48 if (!$command->getPermissionService()->currentUserCanWrite(Entities::NOTIFICATIONS)) {
49 throw new AccessDeniedException('You are not allowed to update notification');
50 }
51
52 $notificationId = (int)$command->getArg('id');
53
54 $result = new CommandResult();
55
56 $this->checkMandatoryFields($command);
57
58 $notificationData = $command->getFields();
59
60 if (empty($notificationData['entityIds'])) {
61 $notificationData['entityIds'] = [];
62 }
63
64 /** @var EntityApplicationService $entityService */
65 $entityService = $this->container->get('application.entity.service');
66
67 $entityService->removeMissingEntitiesForNotification($notificationData);
68
69 /** @var NotificationRepository $notificationRepo */
70 $notificationRepo = $this->container->get('domain.notification.repository');
71 /** @var NotificationsToEntitiesRepository $notificationEntitiesRepo */
72 $notificationEntitiesRepo = $this->container->get('domain.notificationEntities.repository');
73 /** @var EventRepository $eventRepo */
74 $eventRepo = $this->container->get('domain.booking.event.repository');
75 /** @var NotificationHelperService $notificationHelper */
76 $notificationHelper = $this->container->get('application.notificationHelper.service');
77
78 /** @var Notification $currentNotification */
79 $currentNotification = $notificationRepo->getById($notificationId);
80 $currentEntityList = $notificationEntitiesRepo->getEntities($notificationId);
81
82 $content = $command->getField('content');
83
84 if ($command->getField('type') === 'email') {
85 $content = preg_replace("/\r|\n/", "", $content);
86 }
87
88 if ($command->getField('type') !== 'whatsapp') {
89 $contentRes = $notificationHelper->parseAndReplace($content);
90 $parsedContent = $contentRes[0];
91 $content = $contentRes[1];
92 }
93
94 $isCustom = $command->getField('customName') !== null ;
95
96 $notificationData['id'] = $notificationId;
97 $notificationData['name'] = $isCustom ? $command->getField('name') : $currentNotification->getName()->getValue();
98 $notificationData['status'] = $command->getField('status') ?: $currentNotification->getStatus()->getValue();
99 $notificationData['type'] = $currentNotification->getType()->getValue();
100 $notificationData['sendTo'] = $currentNotification->getSendTo()->getValue();
101 $notificationData['content'] = $content;
102
103 $notificationData = apply_filters('amelia_before_notification_updated_filter', $notificationData);
104
105 do_action('amelia_before_notification_updated', $notificationData);
106
107 /** @var Notification $notification */
108 $notification = NotificationFactory::create($notificationData);
109
110 $minimumTime = $command->getField('minimumTimeBeforeBooking');
111 if (!empty($minimumTime) && json_encode($minimumTime)) {
112 $notification->setMinimumTimeBeforeBooking(new Json(json_encode($minimumTime)));
113 }
114
115 if (!$notification instanceof Notification) {
116 $result->setResult(CommandResult::RESULT_ERROR);
117 $result->setMessage('Could not update notification entity.');
118
119 return $result;
120 }
121
122 if ($notificationRepo->update($notificationId, $notification)) {
123 $result->setResult(CommandResult::RESULT_SUCCESS);
124 $result->setMessage('Successfully updated notification.');
125 $result->setData(
126 [
127 Entities::NOTIFICATION => $notification->toArray(),
128 'update' => !empty($parsedContent)
129 ]
130 );
131 }
132
133 if ($notification->getCustomName()) {
134 $removeEntities = array_diff($currentEntityList, $notification->getEntityIds());
135 $addEntities = array_diff($notification->getEntityIds(), $currentEntityList);
136
137 foreach ($removeEntities as $removeEntity) {
138 $notificationEntitiesRepo->removeEntity($notificationId, $removeEntity, $notification->getEntity()->getValue());
139 }
140 foreach ($addEntities as $addEntity) {
141 $recurringMain = null;
142 if ($notification->getEntity()->getValue() === Entities::EVENT) {
143 $recurring = $eventRepo->isRecurring($addEntity);
144 if ($recurring['event_recurringOrder'] !== null) {
145 $recurringMain = $recurring['event_recurringOrder'] === 1 ? $addEntity : $recurring['event_parentId'];
146 }
147 }
148 $notificationEntitiesRepo->addEntity($notificationId, $recurringMain ?: $addEntity, $notification->getEntity()->getValue());
149 }
150 }
151
152 do_action('amelia_after_notification_updated', $notification->toArray());
153
154 return $result;
155 }
156 }
157