em = $em; $this->parameterBag = $parameterBag; $this->productFamilyUtils = $productFamilyUtils; } public function duplicateEntity($entity, $flush = true) { $newEntity = clone $entity; if ($newEntity instanceof ImageInterface) { $newEntity = $this->duplicateImage($newEntity); } if ($newEntity instanceof ProductFamilyInterface) { $newEntity = $this->productFamilyUtils->processBeforePersistProductFamily($newEntity, false, true); } if(method_exists($newEntity, 'getAddress') && is_object($newEntity->getAddress())){ $address = $newEntity->getAddress(); $newAddress = $this->duplicateEntity($address); $newEntity->setAddress($newAddress); $this->em->persist($newAddress); } if($newEntity instanceof SluggableInterface){ $this->em->persist($newEntity); if($flush)$this->em->flush(); $newEntity->setSlug(null); } $this->em->persist($newEntity); if($flush)$this->em->flush(); return $newEntity; } public function duplicateEntityToOtherHub($entity, $hub){ $newEntity = $this->duplicateEntity($entity); if ($hub) { $newEntity->setMerchant($hub); } $this->em->persist($newEntity) ; $this->em->flush() ; return $newEntity; } public function duplicateImage($entity, $folder = false) { $basePath = $this->parameterBag->get('kernel.project_dir') . '/public/uploads/images/'; if ($entity->getImage() && file_exists($basePath . $entity->getImage())) { $extension = (pathinfo($basePath . $entity->getImage(), PATHINFO_EXTENSION)); if ($extension == "jpg" || $extension == "png" || $extension == "gif") { $newImage = md5(uniqid()) . '.' . $extension; if ($folder) $newImage = $folder . '/' . $newImage; copy($basePath . $entity->getImage(), $basePath . $newImage); $entity->setImage($newImage); } } else { $entity->setImage(null); } return $entity; } }