74 lines
2.0 KiB
PHP
Executable File
74 lines
2.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\EventListener;
|
|
|
|
use App\Entity\Icon;
|
|
use App\Entity\Pagewidget;
|
|
use App\Entity\Pagewidgetslide;
|
|
use App\Service\MinioService;
|
|
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
|
|
use Doctrine\ORM\Events;
|
|
use Doctrine\Persistence\Event\LifecycleEventArgs;
|
|
|
|
class PurgefileSubscriber implements EventSubscriberInterface
|
|
{
|
|
private $entity;
|
|
private $minio;
|
|
|
|
public function __construct(MinioService $minio)
|
|
{
|
|
$this->minio = $minio;
|
|
}
|
|
|
|
public function getSubscribedEvents(): array
|
|
{
|
|
return [
|
|
Events::preRemove,
|
|
];
|
|
}
|
|
|
|
public function preRemove(LifecycleEventArgs $args): void
|
|
{
|
|
$this->entity = $args->getObject();
|
|
|
|
// Sur suppression de pagewidget
|
|
if ($this->entity instanceof Pagewidget) {
|
|
try {
|
|
$files = $this->minio->listFiles('file/pagewidget/'.$this->entity->getId().'/');
|
|
if ($files) {
|
|
foreach ($files as $file) {
|
|
$this->minio->delete($file['Key']);
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
}
|
|
}
|
|
|
|
// Sur suppression pagewidgetslide
|
|
if ($this->entity instanceof Pagewidgetslide) {
|
|
try {
|
|
$files = $this->minio->listFiles($this->entity->getImage());
|
|
if ($files) {
|
|
foreach ($files as $file) {
|
|
$this->minio->delete($file['Key']);
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
}
|
|
}
|
|
|
|
// Sur suppression icon
|
|
if ($this->entity instanceof Icon) {
|
|
try {
|
|
$files = $this->minio->listFiles($this->entity->getLabel());
|
|
if ($files) {
|
|
foreach ($files as $file) {
|
|
$this->minio->delete($file['Key']);
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
}
|
|
}
|
|
}
|
|
}
|