You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.0KB

  1. <?php
  2. namespace Lc\SovBundle\Component;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  5. use Lc\SovBundle\Doctrine\Extension\SluggableInterface;
  6. use Lc\SovBundle\Model\File\FileInterface;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. class EntityComponent
  9. {
  10. protected EntityManagerInterface $entityManager;
  11. protected ParameterBagInterface $parameterBag;
  12. public function __construct(
  13. EntityManagerInterface $entityManager,
  14. ParameterBagInterface $parameterBag
  15. ) {
  16. $this->entityManager = $entityManager;
  17. $this->parameterBag = $parameterBag;
  18. }
  19. public function duplicateEntity($entity, $flush = true)
  20. {
  21. $newEntity = clone $entity;
  22. if ($newEntity instanceof FileInterface) {
  23. $newEntity = $this->duplicateImage($newEntity);
  24. }
  25. if ($newEntity instanceof ProductFamilyInterface) {
  26. // @TODO : à adapter
  27. //$newEntity = $this->productFamilyUtils->processBeforePersistProductFamily($newEntity, false, true);
  28. }
  29. if (method_exists($newEntity, 'getAddress') && is_object($newEntity->getAddress())) {
  30. $address = $newEntity->getAddress();
  31. $newAddress = $this->duplicateEntity($address);
  32. $newEntity->setAddress($newAddress);
  33. $this->entityManager->persist($newAddress);
  34. }
  35. if ($newEntity instanceof SluggableInterface) {
  36. $this->entityManager->persist($newEntity);
  37. if ($flush) {
  38. $this->entityManager->flush();
  39. }
  40. $newEntity->setSlug(null);
  41. }
  42. $this->entityManager->persist($newEntity);
  43. if ($flush) {
  44. $this->entityManager->flush();
  45. }
  46. return $newEntity;
  47. }
  48. public function duplicateEntityToOtherHub($entity, $hub)
  49. {
  50. $newEntity = $this->duplicateEntity($entity);
  51. if ($hub) {
  52. $newEntity->setMerchant($hub);
  53. }
  54. $this->entityManager->persist($newEntity);
  55. $this->entityManager->flush();
  56. return $newEntity;
  57. }
  58. public function duplicateImage($entity, $folder = false)
  59. {
  60. $basePath = $this->parameterBag->get('kernel.project_dir') . '/public/uploads/images/';
  61. if ($entity->getImage() && file_exists($basePath . $entity->getImage())) {
  62. $extension = strtolower(pathinfo($basePath . $entity->getImage(), PATHINFO_EXTENSION));
  63. if ($extension == "jpg" || $extension == "png" || $extension == "gif") {
  64. $newImage = md5(uniqid()) . '.' . $extension;
  65. if ($folder) {
  66. $newImage = $folder . '/' . $newImage;
  67. }
  68. copy($basePath . $entity->getImage(), $basePath . $newImage);
  69. $entity->setImage($newImage);
  70. } else {
  71. $entity->setImage(null);
  72. }
  73. } else {
  74. $entity->setImage(null);
  75. }
  76. return $entity;
  77. }
  78. }