src/EventSubscriber/Order/AddCoverPhotoToOrderSubscriber.php line 39

  1. <?php
  2. /*
  3.  * This file is part of the Pellipop project.
  4.  *
  5.  * (c) Mobizel
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\EventSubscriber\Order;
  12. use App\Converter\FullPathFileConverterInterface;
  13. use App\Entity\Order\Order;
  14. use App\Entity\Order\OrderItemPhoto;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Symfony\Component\Config\FileLocatorInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\Filesystem\Filesystem;
  19. use Symfony\Component\HttpFoundation\File\UploadedFile;
  20. use Symfony\Component\Workflow\Event\Event;
  21. use Webmozart\Assert\Assert;
  22. class AddCoverPhotoToOrderSubscriber implements EventSubscriberInterface
  23. {
  24.     public function __construct(
  25.         private FullPathFileConverterInterface $fullPathFileConverter,
  26.         private FileLocatorInterface $fileLocator,
  27.         private Filesystem $filesystem,
  28.         private string $imageCoverFullPath,
  29.         private EntityManagerInterface $entityManager,
  30.         private bool $addCoverImageToOrder,
  31.     ) {
  32.     }
  33.     public function onEnter(Event $event)
  34.     {
  35.         if (false === $this->addCoverImageToOrder) {
  36.             return;
  37.         }
  38.         /** @var Order $order */
  39.         $order $event->getSubject();
  40.         Assert::isInstanceOf($orderOrder::class);
  41.         foreach ($order->getOrderItems() as $orderItem) {
  42.             $photoCover = new OrderItemPhoto();
  43.             $targetPath $this->fullPathFileConverter->convert($photoCover'file'basename($this->imageCoverFullPath));
  44.             $this->filesystem->copy($this->imageCoverFullPath$targetPath);
  45.             $file = new UploadedFile(path$targetPathoriginalNamebasename($targetPath), testtrue);
  46.             $photoCover->setFile($file);
  47.             $orderItem->addPhoto($photoCover);
  48.             $this->entityManager->persist($photoCover);
  49.             $this->entityManager->flush();
  50.         }
  51.     }
  52.     public static function getSubscribedEvents()
  53.     {
  54.         return [
  55.             'workflow.order_production.enter.uploaded' => ['onEnter'100],
  56.         ];
  57.     }
  58. }