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 |