GalleryApplicationService.php
123 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 | |
| 20 | private $container; |
| 21 | |
| 22 | /** |
| 23 | * GalleryApplicationService constructor. |
| 24 | * |
| 25 | * @param Container $container |
| 26 | */ |
| 27 | public function __construct(Container $container) |
| 28 | { |
| 29 | $this->container = $container; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @param Collection $entityGallery |
| 34 | * @param int $entityId |
| 35 | * |
| 36 | * @throws \Slim\Exception\ContainerValueNotFoundException |
| 37 | * @throws QueryExecutionException |
| 38 | * @throws \Interop\Container\Exception\ContainerException |
| 39 | */ |
| 40 | public function manageGalleryForEntityAdd($entityGallery, $entityId) |
| 41 | { |
| 42 | /** @var GalleryRepository $galleryRepository */ |
| 43 | $galleryRepository = $this->container->get('domain.galleries.repository'); |
| 44 | |
| 45 | /** @var GalleryImage $image */ |
| 46 | foreach ($entityGallery->getItems() as $image) { |
| 47 | $image->setEntityId(new Id($entityId)); |
| 48 | |
| 49 | if (!($imageId = $galleryRepository->add($image))) { |
| 50 | $galleryRepository->rollback(); |
| 51 | } |
| 52 | |
| 53 | $image->setId(new Id($imageId)); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @param Collection $entityGallery |
| 59 | * @param int $entityId |
| 60 | * @param string $entityType |
| 61 | * |
| 62 | * @throws \Slim\Exception\ContainerValueNotFoundException |
| 63 | * @throws QueryExecutionException |
| 64 | * @throws \Interop\Container\Exception\ContainerException |
| 65 | */ |
| 66 | public function manageGalleryForEntityUpdate($entityGallery, $entityId, $entityType) |
| 67 | { |
| 68 | /** @var GalleryRepository $galleryRepository */ |
| 69 | $galleryRepository = $this->container->get('domain.galleries.repository'); |
| 70 | |
| 71 | $imagesIds = []; |
| 72 | |
| 73 | /** @var GalleryImage $image */ |
| 74 | foreach ($entityGallery->getItems() as $image) { |
| 75 | if ($image->getId()) { |
| 76 | $imagesIds[] = $image->getId()->getValue(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if (!$galleryRepository->deleteAllNotInImagesArray( |
| 81 | $imagesIds, |
| 82 | $entityId, |
| 83 | $entityType |
| 84 | )) { |
| 85 | $galleryRepository->rollback(); |
| 86 | } |
| 87 | |
| 88 | /** @var GalleryImage $image */ |
| 89 | foreach ($entityGallery->getItems() as $image) { |
| 90 | if (!$image->getId()) { |
| 91 | $galleryRepository->add($image); |
| 92 | } else { |
| 93 | $galleryRepository->update($image->getId()->getValue(), $image); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @param Collection $entityGallery |
| 100 | * |
| 101 | * @return boolean |
| 102 | * |
| 103 | * @throws \Slim\Exception\ContainerValueNotFoundException |
| 104 | * @throws QueryExecutionException |
| 105 | * @throws \Interop\Container\Exception\ContainerException |
| 106 | */ |
| 107 | public function manageGalleryForEntityDelete($entityGallery) |
| 108 | { |
| 109 | /** @var GalleryRepository $galleryRepository */ |
| 110 | $galleryRepository = $this->container->get('domain.galleries.repository'); |
| 111 | |
| 112 | /** @var GalleryImage $image */ |
| 113 | foreach ($entityGallery->getItems() as $image) { |
| 114 | if (!$galleryRepository->delete($image->getId()->getValue())) { |
| 115 | $galleryRepository->rollback(); |
| 116 | return false; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return true; |
| 121 | } |
| 122 | } |
| 123 |