Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

116 linhas
4.1KB

  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\BlameableInterface;
  6. use Lc\SovBundle\Doctrine\Extension\DevAliasInterface;
  7. use Lc\SovBundle\Doctrine\Extension\SeoInterface;
  8. use Lc\SovBundle\Doctrine\Extension\SortableInterface;
  9. use Lc\SovBundle\Doctrine\Extension\TimestampableInterface;
  10. use Lc\SovBundle\Event\EntityComponentEvent;
  11. use Lc\SovBundle\Model\File\FileInterface;
  12. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. class EntityComponent
  15. {
  16. protected EntityManagerInterface $entityManager;
  17. protected ParameterBagInterface $parameterBag;
  18. protected EventDispatcherInterface $eventDispatcher;
  19. public function __construct(
  20. EntityManagerInterface $entityManager,
  21. ParameterBagInterface $parameterBag,
  22. EventDispatcherInterface $eventDispatcher
  23. ) {
  24. $this->entityManager = $entityManager;
  25. $this->parameterBag = $parameterBag;
  26. $this->eventDispatcher = $eventDispatcher;
  27. }
  28. public function duplicateEntity($entity)
  29. {
  30. $newEntity = clone $entity;
  31. $classMetadata = $this->entityManager->getClassMetadata(get_class($newEntity));
  32. //Dupplication de l'image ou du fichier lier
  33. foreach ($classMetadata->getAssociationMappings() as $associationMapping){
  34. if(in_array(FileInterface::class, class_implements($associationMapping['targetEntity']))){
  35. $methodGet = 'get'.ucfirst($associationMapping['fieldName']);
  36. $methodSet = 'set'.ucfirst($associationMapping['fieldName']);
  37. if(method_exists($newEntity, $methodGet) && method_exists($newEntity, $methodSet)){
  38. if(!is_null($newEntity->$methodGet())){
  39. $newFile = clone $newEntity->$methodGet();
  40. $newEntity->$methodSet($newFile);
  41. }
  42. }
  43. }
  44. }
  45. if($newEntity instanceof DevAliasInterface){
  46. $newEntity->setDevAlias(null);
  47. }
  48. if($newEntity instanceof SeoInterface) {
  49. $newEntity->setOldUrls(array());
  50. }
  51. if($newEntity instanceof BlameableInterface) {
  52. $newEntity->setUpdatedBy(null);
  53. $newEntity->setCreatedBy(null);
  54. }
  55. if($newEntity instanceof TimestampableInterface) {
  56. $newEntity->setUpdatedAt(new \DateTime());
  57. $newEntity->setCreatedAt(new \DateTime());
  58. }
  59. $this->eventDispatcher->dispatch(new EntityComponentEvent($newEntity), EntityComponentEvent::DUPLICATE_EVENT);
  60. //Ne pas utiliser create ici! Sinon pour certaine entité comme ProductFamily on réajoute un orginProduct
  61. $this->entityManager->create($newEntity, false);
  62. return $newEntity;
  63. }
  64. public function duplicateEntityToOtherHub($entity, $hub)
  65. {
  66. $newEntity = $this->duplicateEntity($entity);
  67. if ($hub) {
  68. $newEntity->setMerchant($hub);
  69. }
  70. $this->entityManager->persist($newEntity);
  71. $this->entityManager->flush();
  72. return $newEntity;
  73. }
  74. public function duplicateImage($entity, $folder = false)
  75. {
  76. $basePath = $this->parameterBag->get('kernel.project_dir') . '/public/uploads/images/';
  77. if ($entity->getImage() && file_exists($basePath . $entity->getImage())) {
  78. $extension = strtolower(pathinfo($basePath . $entity->getImage(), PATHINFO_EXTENSION));
  79. if ($extension == "jpg" || $extension == "png" || $extension == "gif") {
  80. $newImage = md5(uniqid()) . '.' . $extension;
  81. if ($folder) {
  82. $newImage = $folder . '/' . $newImage;
  83. }
  84. copy($basePath . $entity->getImage(), $basePath . $newImage);
  85. $entity->setImage($newImage);
  86. } else {
  87. $entity->setImage(null);
  88. }
  89. } else {
  90. $entity->setImage(null);
  91. }
  92. return $entity;
  93. }
  94. }