container = $container; } /** * @param Collection $entityGallery * @param int $entityId * * @throws \Slim\Exception\ContainerValueNotFoundException * @throws QueryExecutionException * @throws \Interop\Container\Exception\ContainerException */ public function manageGalleryForEntityAdd($entityGallery, $entityId) { /** @var GalleryRepository $galleryRepository */ $galleryRepository = $this->container->get('domain.galleries.repository'); /** @var GalleryImage $image */ foreach ($entityGallery->getItems() as $image) { $image->setEntityId(new Id($entityId)); if (!($imageId = $galleryRepository->add($image))) { $galleryRepository->rollback(); } $image->setId(new Id($imageId)); } } /** * @param Collection $entityGallery * @param int $entityId * @param string $entityType * * @throws \Slim\Exception\ContainerValueNotFoundException * @throws QueryExecutionException * @throws \Interop\Container\Exception\ContainerException */ public function manageGalleryForEntityUpdate($entityGallery, $entityId, $entityType) { /** @var GalleryRepository $galleryRepository */ $galleryRepository = $this->container->get('domain.galleries.repository'); $imagesIds = []; /** @var GalleryImage $image */ foreach ($entityGallery->getItems() as $image) { if ($image->getId()) { $imagesIds[] = $image->getId()->getValue(); } } if (!$galleryRepository->deleteAllNotInImagesArray( $imagesIds, $entityId, $entityType )) { $galleryRepository->rollback(); } /** @var GalleryImage $image */ foreach ($entityGallery->getItems() as $image) { if (!$image->getId()) { $galleryRepository->add($image); } else { $galleryRepository->update($image->getId()->getValue(), $image); } } } /** * @param Collection $entityGallery * * @throws \Slim\Exception\ContainerValueNotFoundException * @throws QueryExecutionException * @throws \Interop\Container\Exception\ContainerException */ public function manageGalleryForEntityDelete($entityGallery) { /** @var GalleryRepository $galleryRepository */ $galleryRepository = $this->container->get('domain.galleries.repository'); /** @var GalleryImage $image */ foreach ($entityGallery->getItems() as $image) { if (!$galleryRepository->delete($image->getId()->getValue())) { $galleryRepository->rollback(); } } } }