PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / trunk
Booking for Appointments and Events Calendar – Amelia vtrunk
2.4.4 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 / Booking / Event / AddEventCommandHandler.php
ameliabooking / src / Application / Commands / Booking / Event Last commit date
Tag 2 months ago AddEventCommand.php 1 year ago AddEventCommandHandler.php 2 weeks ago DeleteEventBookingCommand.php 7 years ago DeleteEventBookingCommandHandler.php 3 months ago DeleteEventCommand.php 1 year ago DeleteEventCommandHandler.php 6 months ago DeleteEventsCommand.php 6 months ago DeleteEventsCommandHandler.php 3 months ago GetCalendarEventsCommand.php 1 year ago GetCalendarEventsCommandHandler.php 1 year ago GetEventBookingCommand.php 6 months ago GetEventBookingCommandHandler.php 2 weeks ago GetEventBookingsCommand.php 1 year ago GetEventBookingsCommandHandler.php 3 months ago GetEventCommand.php 7 years ago GetEventCommandHandler.php 3 months ago GetEventsCommand.php 1 year ago GetEventsCommandHandler.php 2 weeks ago UpdateEventBookingCommand.php 7 years ago UpdateEventBookingCommandHandler.php 6 months ago UpdateEventCommand.php 1 year ago UpdateEventCommandHandler.php 3 months ago UpdateEventStatusCommand.php 1 year ago UpdateEventStatusCommandHandler.php 6 months ago UpdateEventVisibilityCommand.php 6 months ago UpdateEventVisibilityCommandHandler.php 6 months ago
AddEventCommandHandler.php
138 lines
1 <?php
2
3 namespace AmeliaBooking\Application\Commands\Booking\Event;
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\Booking\EventApplicationService;
9 use AmeliaBooking\Application\Services\Entity\EntityApplicationService;
10 use AmeliaBooking\Application\Services\User\UserApplicationService;
11 use AmeliaBooking\Domain\Collection\Collection;
12 use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException;
13 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
14 use AmeliaBooking\Domain\Entity\Booking\Event\Event;
15 use AmeliaBooking\Domain\Entity\Entities;
16 use AmeliaBooking\Domain\Entity\User\AbstractUser;
17 use AmeliaBooking\Domain\Services\Settings\SettingsService;
18 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
19 use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventRepository;
20 use Exception;
21 use Interop\Container\Exception\ContainerException;
22 use Slim\Exception\ContainerValueNotFoundException;
23
24 /**
25 * Class AddEventCommandHandler
26 *
27 * @package AmeliaBooking\Application\Commands\Booking\Event
28 */
29 class AddEventCommandHandler extends CommandHandler
30 {
31 /**
32 * @var array
33 */
34 public $mandatoryFields = [
35 'name',
36 'periods'
37 ];
38
39 /**
40 * @param AddEventCommand $command
41 *
42 * @return CommandResult
43 * @throws QueryExecutionException
44 * @throws ContainerValueNotFoundException
45 * @throws InvalidArgumentException
46 * @throws ContainerException
47 * @throws Exception
48 */
49 public function handle(AddEventCommand $command)
50 {
51 $result = new CommandResult();
52
53 $this->checkMandatoryFields($command);
54
55 $eventData = $command->getFields();
56
57 /** @var EventRepository $eventRepository */
58 $eventRepository = $this->container->get('domain.booking.event.repository');
59 /** @var EventApplicationService $eventApplicationService */
60 $eventApplicationService = $this->container->get('application.booking.event.service');
61 /** @var UserApplicationService $userAS */
62 $userAS = $this->getContainer()->get('application.user.service');
63 /** @var SettingsService $settingsDS */
64 $settingsDS = $this->container->get('domain.settings.service');
65 /** @var EntityApplicationService $entityService */
66 $entityService = $this->container->get('application.entity.service');
67
68 try {
69 /** @var AbstractUser $user */
70 $user = $command->getUserApplicationService()->authorization(
71 $command->getPage() === 'cabinet' ? $command->getToken() : null,
72 $command->getCabinetType()
73 );
74 } catch (AuthorizationException $e) {
75 $result->setResult(CommandResult::RESULT_ERROR);
76 $result->setData(
77 ['reauthorize' => true]
78 );
79
80 return $result;
81 }
82
83 if (
84 $userAS->isCustomer($user) ||
85 ($userAS->isProvider($user) && !$settingsDS->getSetting('roles', 'allowWriteEvents'))
86 ) {
87 throw new AccessDeniedException('You are not allowed to add an event');
88 }
89
90 if ($userAS->isProvider($user) && $userAS->isAmeliaUser($user) && empty($eventData['providers'])) {
91 $eventData['providers'] = [
92 ['id' => $user->getId()->getValue()],
93 ];
94 }
95
96 $entityService->removeMissingEntitiesForEvent($eventData);
97
98 $eventRepository->beginTransaction();
99
100 $eventData = apply_filters('amelia_before_event_added_filter', $eventData);
101
102 do_action('amelia_before_event_added', $eventData);
103
104 try {
105 /** @var Event $event */
106 $event = $eventApplicationService->build($eventData);
107 } catch (Exception $e) {
108 $result->setResult(CommandResult::RESULT_ERROR);
109 $result->setMessage($e->getMessage());
110 $result->setData(['message' => $e->getMessage()]);
111 return $result;
112 }
113
114
115 try {
116 /** @var Collection $events */
117 $events = $eventApplicationService->add($event);
118 } catch (QueryExecutionException $e) {
119 $eventRepository->rollback();
120 throw $e;
121 }
122
123 $eventRepository->commit();
124
125 do_action('amelia_after_event_added', $event ? $event->toArray() : null);
126
127 $result->setResult(CommandResult::RESULT_SUCCESS);
128 $result->setMessage('Successfully added new event.');
129 $result->setData(
130 [
131 Entities::EVENTS => $events->toArray(),
132 ]
133 );
134
135 return $result;
136 }
137 }
138