Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

EntityComponent.php 4.0KB

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