PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / trunk
Booking for Appointments and Events Calendar – Amelia vtrunk
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 / Services / Gallery / GalleryApplicationService.php
ameliabooking / src / Application / Services / Gallery Last commit date
GalleryApplicationService.php 6 months ago
GalleryApplicationService.php
121 lines
1 <?php
2
3 namespace AmeliaBooking\Application\Services\Gallery;
4
5 use AmeliaBooking\Domain\Collection\Collection;
6 use AmeliaBooking\Domain\Entity\Gallery\GalleryImage;
7 use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id;
8 use AmeliaBooking\Infrastructure\Common\Container;
9 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
10 use AmeliaBooking\Infrastructure\Repository\Gallery\GalleryRepository;
11
12 /**
13 * Class GalleryApplicationService
14 *
15 * @package AmeliaBooking\Application\Services\Gallery
16 */
17 class GalleryApplicationService
18 {
19 private $container;
20
21 /**
22 * GalleryApplicationService constructor.
23 *
24 * @param Container $container
25 */
26 public function __construct(Container $container)
27 {
28 $this->container = $container;
29 }
30
31 /**
32 * @param Collection $entityGallery
33 * @param int $entityId
34 *
35 * @throws \Slim\Exception\ContainerValueNotFoundException
36 * @throws QueryExecutionException
37 */
38 public function manageGalleryForEntityAdd($entityGallery, $entityId)
39 {
40 /** @var GalleryRepository $galleryRepository */
41 $galleryRepository = $this->container->get('domain.galleries.repository');
42
43 /** @var GalleryImage $image */
44 foreach ($entityGallery->getItems() as $image) {
45 $image->setEntityId(new Id($entityId));
46
47 if (!($imageId = $galleryRepository->add($image))) {
48 $galleryRepository->rollback();
49 }
50
51 $image->setId(new Id($imageId));
52 }
53 }
54
55 /**
56 * @param Collection $entityGallery
57 * @param int $entityId
58 * @param string $entityType
59 *
60 * @throws \Slim\Exception\ContainerValueNotFoundException
61 * @throws QueryExecutionException
62 */
63 public function manageGalleryForEntityUpdate($entityGallery, $entityId, $entityType)
64 {
65 /** @var GalleryRepository $galleryRepository */
66 $galleryRepository = $this->container->get('domain.galleries.repository');
67
68 $imagesIds = [];
69
70 /** @var GalleryImage $image */
71 foreach ($entityGallery->getItems() as $image) {
72 if ($image->getId()) {
73 $imagesIds[] = $image->getId()->getValue();
74 }
75 }
76
77 if (
78 !$galleryRepository->deleteAllNotInImagesArray(
79 $imagesIds,
80 $entityId,
81 $entityType
82 )
83 ) {
84 $galleryRepository->rollback();
85 }
86
87 /** @var GalleryImage $image */
88 foreach ($entityGallery->getItems() as $image) {
89 if (!$image->getId()) {
90 $galleryRepository->add($image);
91 } else {
92 $galleryRepository->update($image->getId()->getValue(), $image);
93 }
94 }
95 }
96
97 /**
98 * @param Collection $entityGallery
99 *
100 * @return boolean
101 *
102 * @throws \Slim\Exception\ContainerValueNotFoundException
103 * @throws QueryExecutionException
104 */
105 public function manageGalleryForEntityDelete($entityGallery)
106 {
107 /** @var GalleryRepository $galleryRepository */
108 $galleryRepository = $this->container->get('domain.galleries.repository');
109
110 /** @var GalleryImage $image */
111 foreach ($entityGallery->getItems() as $image) {
112 if (!$galleryRepository->delete($image->getId()->getValue())) {
113 $galleryRepository->rollback();
114 return false;
115 }
116 }
117
118 return true;
119 }
120 }
121