src/EventListener/ShippingMethodListener.php line 23

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventListener;
  4. use App\Files\Uploader\ShippingMethodUploader;
  5. use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
  6. use Symfony\Component\EventDispatcher\GenericEvent;
  7. final class ShippingMethodListener
  8. {
  9.     private ShippingMethodUploader $shippingMethodUploader;
  10.     /**
  11.      * ShippingMethodListener constructor.
  12.      */
  13.     public function __construct(ShippingMethodUploader $shippingMethodUploader)
  14.     {
  15.         $this->shippingMethodUploader $shippingMethodUploader;
  16.     }
  17.     public function pre_create(ResourceControllerEvent $shippingMethod): void
  18.     {
  19.         $file $shippingMethod->getSubject()->getFile();
  20.         if ($file !== null) {
  21.             $pathFile $this->shippingMethodUploader->upload($file);
  22.             $shippingMethod->getSubject()->setFilePath($pathFile);
  23.         }
  24.     }
  25.     public function pre_update(ResourceControllerEvent $shippingMethod): void
  26.     {
  27.         $file $shippingMethod->getSubject()->getFile();
  28.         if ($file === null) {
  29.             return;
  30.         }
  31.         $pathFile $this->shippingMethodUploader->upload($file);
  32.         $shippingMethod->getSubject()->setFilePath($pathFile);
  33.     }
  34.     public function deleteFile(GenericEvent $event)
  35.     {
  36.         $file $event->getSubject();
  37.         if ($file) {
  38.             $oldFilePath $file->getFilePath();
  39.             if (!empty($oldFilePath)) {
  40.                 @unlink($oldFilePath);
  41.             }
  42.         }
  43.     }
  44. }