<?php
declare(strict_types=1);
namespace App\EventListener;
use App\Files\Uploader\ShippingMethodUploader;
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
use Symfony\Component\EventDispatcher\GenericEvent;
final class ShippingMethodListener
{
private ShippingMethodUploader $shippingMethodUploader;
/**
* ShippingMethodListener constructor.
*/
public function __construct(ShippingMethodUploader $shippingMethodUploader)
{
$this->shippingMethodUploader = $shippingMethodUploader;
}
public function pre_create(ResourceControllerEvent $shippingMethod): void
{
$file = $shippingMethod->getSubject()->getFile();
if ($file !== null) {
$pathFile = $this->shippingMethodUploader->upload($file);
$shippingMethod->getSubject()->setFilePath($pathFile);
}
}
public function pre_update(ResourceControllerEvent $shippingMethod): void
{
$file = $shippingMethod->getSubject()->getFile();
if ($file === null) {
return;
}
$pathFile = $this->shippingMethodUploader->upload($file);
$shippingMethod->getSubject()->setFilePath($pathFile);
}
public function deleteFile(GenericEvent $event)
{
$file = $event->getSubject();
if ($file) {
$oldFilePath = $file->getFilePath();
if (!empty($oldFilePath)) {
@unlink($oldFilePath);
}
}
}
}