PluginProbe
Booking for Appointments and Events Calendar – Amelia / 2.4.2
Booking for Appointments and Events Calendar – Amelia v2.4.2
2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 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 All 59 releases
ameliabooking / src / Application / Commands / User / Customer / UpdateCustomerNoteCommandHandler.php

UpdateCustomerNoteCommandHandler.php in Booking for Appointments and Events Calendar – Amelia 2.4.2, at src/Application/Commands/User/Customer/UpdateCustomerNoteCommandHandler.php

102 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AmeliaBooking\Application\Commands\User\Customer;
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\User\UserApplicationService;
9 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
10 use AmeliaBooking\Domain\Entity\Entities;
11 use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException;
12 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
13 use AmeliaBooking\Infrastructure\Repository\User\UserRepository;
14
15 /**
16 * Class UpdateCustomerNoteCommandHandler
17 *
18 * @package AmeliaBooking\Application\Commands\User\Customer
19 */
20 class UpdateCustomerNoteCommandHandler extends CommandHandler
21 {
22 /**
23 * @param UpdateCustomerNoteCommand $command
24 *
25 * @return CommandResult
26 *
27 * @throws InvalidArgumentException
28 * @throws NotFoundException
29 * @throws QueryExecutionException
30 * @throws AccessDeniedException
31 */
32 public function handle(UpdateCustomerNoteCommand $command)
33 {
34 $result = new CommandResult();
35
36 /** @var UserApplicationService $userAS */
37 $userAS = $this->getContainer()->get('application.user.service');
38
39 /** @var UserRepository $userRepository */
40 $userRepository = $this->getContainer()->get('domain.users.repository');
41
42 if (!$command->getPermissionService()->currentUserCanWrite(Entities::CUSTOMERS)) {
43 if ($command->getToken()) {
44 $provider = $command->getCabinetType() === 'provider'
45 ? $userAS->getAuthenticatedUser($command->getToken(), false, 'providerCabinet')
46 : null;
47
48 $oldUser = $provider === null
49 ? $userAS->getAuthenticatedUser($command->getToken(), false, 'customerCabinet')
50 : $userRepository->getById($command->getArg('id'));
51
52 if (
53 $provider === null &&
54 ($oldUser === null || $oldUser->getId()->getValue() !== intval($command->getArg('id')))
55 ) {
56 $result->setResult(CommandResult::RESULT_ERROR);
57 $result->setMessage('Could not retrieve user');
58 $result->setData(['reauthorize' => true]);
59
60 return $result;
61 }
62
63 if ($provider !== null && $oldUser === null) {
64 $result->setResult(CommandResult::RESULT_ERROR);
65 $result->setMessage('Could not retrieve user');
66
67 return $result;
68 }
69 } else {
70 throw new AccessDeniedException('You are not allowed to perform this action!');
71 }
72 } else {
73 $oldUser = $userRepository->getById($command->getArg('id'));
74 if ($oldUser === null) {
75 $result->setResult(CommandResult::RESULT_ERROR);
76 $result->setMessage('Could not retrieve user');
77 return $result;
78 }
79 }
80
81 $customerId = (int)$command->getArg('id');
82 $note = $command->getField('note');
83
84 $userRepository->beginTransaction();
85
86 try {
87 $userRepository->updateFieldById($customerId, $note, 'note');
88 } catch (QueryExecutionException $e) {
89 $userRepository->rollback();
90 throw $e;
91 }
92
93 $userRepository->commit();
94
95 $result->setResult(CommandResult::RESULT_SUCCESS);
96 $result->setMessage('Successfully updated customer note');
97 $result->setData([]);
98
99 return $result;
100 }
101 }
102