Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

112 lines
3.9KB

  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->entityManager->create($newEntity);
  59. $this->eventDispatcher->dispatch(new EntityComponentEvent($newEntity), EntityComponentEvent::DUPLICATE_EVENT);
  60. return $newEntity;
  61. }
  62. public function duplicateEntityToOtherHub($entity, $hub)
  63. {
  64. $newEntity = $this->duplicateEntity($entity);
  65. if ($hub) {
  66. $newEntity->setMerchant($hub);
  67. }
  68. $this->entityManager->persist($newEntity);
  69. $this->entityManager->flush();
  70. return $newEntity;
  71. }
  72. public function duplicateImage($entity, $folder = false)
  73. {
  74. $basePath = $this->parameterBag->get('kernel.project_dir') . '/public/uploads/images/';
  75. if ($entity->getImage() && file_exists($basePath . $entity->getImage())) {
  76. $extension = strtolower(pathinfo($basePath . $entity->getImage(), PATHINFO_EXTENSION));
  77. if ($extension == "jpg" || $extension == "png" || $extension == "gif") {
  78. $newImage = md5(uniqid()) . '.' . $extension;
  79. if ($folder) {
  80. $newImage = $folder . '/' . $newImage;
  81. }
  82. copy($basePath . $entity->getImage(), $basePath . $newImage);
  83. $entity->setImage($newImage);
  84. } else {
  85. $entity->setImage(null);
  86. }
  87. } else {
  88. $entity->setImage(null);
  89. }
  90. return $entity;
  91. }
  92. }