src/EventSubscriber/Order/AddCoverPhotoToOrderSubscriber.php line 39
<?php/** This file is part of the Pellipop project.** (c) Mobizel** For the full copyright and license information, please view the LICENSE* file that was distributed with this source code.*/declare(strict_types=1);namespace App\EventSubscriber\Order;use App\Converter\FullPathFileConverterInterface;use App\Entity\Order\Order;use App\Entity\Order\OrderItemPhoto;use Doctrine\ORM\EntityManagerInterface;use Symfony\Component\Config\FileLocatorInterface;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\Filesystem\Filesystem;use Symfony\Component\HttpFoundation\File\UploadedFile;use Symfony\Component\Workflow\Event\Event;use Webmozart\Assert\Assert;class AddCoverPhotoToOrderSubscriber implements EventSubscriberInterface{public function __construct(private FullPathFileConverterInterface $fullPathFileConverter,private FileLocatorInterface $fileLocator,private Filesystem $filesystem,private string $imageCoverFullPath,private EntityManagerInterface $entityManager,private bool $addCoverImageToOrder,) {}public function onEnter(Event $event){if (false === $this->addCoverImageToOrder) {return;}/** @var Order $order */$order = $event->getSubject();Assert::isInstanceOf($order, Order::class);foreach ($order->getOrderItems() as $orderItem) {$photoCover = new OrderItemPhoto();$targetPath = $this->fullPathFileConverter->convert($photoCover, 'file', basename($this->imageCoverFullPath));$this->filesystem->copy($this->imageCoverFullPath, $targetPath);$file = new UploadedFile(path: $targetPath, originalName: basename($targetPath), test: true);$photoCover->setFile($file);$orderItem->addPhoto($photoCover);$this->entityManager->persist($photoCover);$this->entityManager->flush();}}public static function getSubscribedEvents(){return ['workflow.order_production.enter.uploaded' => ['onEnter', 100],];}}