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 / Bookable / Category / AddCategoryCommandHandler.php
ameliabooking / src / Application / Commands / Bookable / Category Last commit date
AddCategoryCommand.php 1 year ago AddCategoryCommandHandler.php 6 months ago DeleteCategoryCommand.php 1 year ago DeleteCategoryCommandHandler.php 6 months ago GetCategoriesCommand.php 1 year ago GetCategoriesCommandHandler.php 6 months ago GetCategoryCommand.php 1 year ago GetCategoryCommandHandler.php 6 months ago GetCategoryDeleteEffectCommand.php 6 months ago GetCategoryDeleteEffectCommandHandler.php 3 months ago UpdateCategoriesPositionsCommand.php 1 year ago UpdateCategoriesPositionsCommandHandler.php 6 months ago UpdateCategoryCommand.php 1 year ago UpdateCategoryCommandHandler.php 6 months ago
AddCategoryCommandHandler.php
115 lines
1 <?php
2
3 namespace AmeliaBooking\Application\Commands\Bookable\Category;
4
5 use AmeliaBooking\Application\Commands\CommandHandler;
6 use AmeliaBooking\Application\Commands\CommandResult;
7 use AmeliaBooking\Domain\Collection\Collection;
8 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
9 use AmeliaBooking\Domain\Entity\Bookable\Service\Category;
10 use AmeliaBooking\Domain\Entity\Bookable\Service\Extra;
11 use AmeliaBooking\Domain\Entity\Bookable\Service\Service;
12 use AmeliaBooking\Domain\Entity\Entities;
13 use AmeliaBooking\Domain\Factory\Bookable\Service\CategoryFactory;
14 use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException;
15 use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id;
16 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
17 use AmeliaBooking\Infrastructure\Repository\Bookable\Service\CategoryRepository;
18 use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ExtraRepository;
19 use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ServiceRepository;
20
21 /**
22 * Class AddCategoryCommandHandler
23 *
24 * @package AmeliaBooking\Application\Commands\Bookable\Category
25 */
26 class AddCategoryCommandHandler extends CommandHandler
27 {
28 protected $mandatoryFields = [
29 'name'
30 ];
31
32 /**
33 * @param AddCategoryCommand $command
34 *
35 * @return CommandResult
36 * @throws \Slim\Exception\ContainerValueNotFoundException
37 * @throws AccessDeniedException
38 * @throws QueryExecutionException
39 * @throws InvalidArgumentException
40 */
41 public function handle(AddCategoryCommand $command)
42 {
43 if (!$command->getPermissionService()->currentUserCanWrite(Entities::SERVICES)) {
44 throw new AccessDeniedException('You are not allowed to add category.');
45 }
46
47 $result = new CommandResult();
48
49 $this->checkMandatoryFields($command);
50
51 $categoryArray = $command->getFields();
52
53 $categoryArray = apply_filters('amelia_before_category_added_filter', $categoryArray);
54
55 do_action('amelia_before_category_added', $categoryArray);
56
57 /** @var Category $category */
58 $category = CategoryFactory::create($categoryArray);
59
60 if (!($category instanceof Category)) {
61 $result->setResult(CommandResult::RESULT_ERROR);
62 $result->setMessage('Could not create category.');
63
64 return $result;
65 }
66
67 /** @var CategoryRepository $categoryRepository */
68 $categoryRepository = $this->container->get('domain.bookable.category.repository');
69 /** @var ServiceRepository $serviceRepository */
70 $serviceRepository = $this->container->get('domain.bookable.service.repository');
71 /** @var ExtraRepository $extraRepository */
72 $extraRepository = $this->container->get('domain.bookable.extra.repository');
73
74 $categoryRepository->beginTransaction();
75
76 $categoryId = $categoryRepository->add($category);
77
78 $category->setId(new Id($categoryId));
79
80 if ($category->getServiceList() && $category->getServiceList()->length()) {
81 foreach ($category->getServiceList()->getItems() as $service) {
82 /** @var Service $service */
83 $service->setCategoryId(new Id($categoryId));
84
85 $serviceId = $serviceRepository->add($service);
86
87 /** @var Extra $extra */
88 foreach ($service->getExtras()->getItems() as $extra) {
89 $extra->setServiceId(new Id($serviceId));
90
91 $extraId = $extraRepository->add($extra);
92
93 $extra->setId(new Id($extraId));
94 }
95
96 $service->setId(new Id($serviceId));
97 }
98 }
99
100 $categoryRepository->commit();
101
102 do_action('amelia_after_category_added', $categoryArray);
103
104 $result->setResult(CommandResult::RESULT_SUCCESS);
105 $result->setMessage('Successfully added new category.');
106 $result->setData(
107 [
108 Entities::CATEGORY => $category->toArray()
109 ]
110 );
111
112 return $result;
113 }
114 }
115