Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

94 lines
3.3KB

  1. <?php
  2. namespace Lc\SovBundle\Component;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\SovBundle\Event\EntityComponentEvent;
  5. use Lc\SovBundle\Model\File\FileInterface;
  6. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  7. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  8. class EntityComponent
  9. {
  10. protected EntityManagerInterface $entityManager;
  11. protected ParameterBagInterface $parameterBag;
  12. protected EventDispatcherInterface $eventDispatcher;
  13. public function __construct(
  14. EntityManagerInterface $entityManager,
  15. ParameterBagInterface $parameterBag,
  16. EventDispatcherInterface $eventDispatcher
  17. ) {
  18. $this->entityManager = $entityManager;
  19. $this->parameterBag = $parameterBag;
  20. $this->eventDispatcher = $eventDispatcher;
  21. }
  22. public function duplicateEntity($entity)
  23. {
  24. $newEntity = clone $entity;
  25. $classMetadata = $this->entityManager->getClassMetadata(get_class($newEntity));
  26. foreach ($classMetadata->getAssociationMappings() as $associationMapping){
  27. if(in_array(FileInterface::class, class_implements($associationMapping['targetEntity']))){
  28. $methodGet = 'get'.ucfirst($associationMapping['fieldName']);
  29. $methodSet = 'set'.ucfirst($associationMapping['fieldName']);
  30. if(method_exists($newEntity, $methodGet) && method_exists($newEntity, $methodSet)){
  31. if(!is_null($newEntity->$methodGet())){
  32. $newFile = clone $newEntity->$methodGet();
  33. $newEntity->$methodSet($newFile);
  34. }
  35. }
  36. }
  37. }
  38. //TODO dispatch event duplicate
  39. /* if ($newEntity instanceof ProductFamilyInterface) {
  40. // @TODO : à adapter
  41. //$newEntity = $this->productFamilyUtils->processBeforePersistProductFamily($newEntity, false, true);
  42. }
  43. */
  44. $this->entityManager->create($newEntity);
  45. $this->eventDispatcher->dispatch(new EntityComponentEvent($newEntity), EntityComponentEvent::DUPLICATE_EVENT);
  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. }