2023-04-10 16:47:59 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\EventSubscriber;
|
|
|
|
|
|
|
|
use App\Core\Entity\EntityInterface;
|
|
|
|
use App\Core\Event\EntityManager\EntityManagerEvent;
|
|
|
|
use App\Core\EventSubscriber\EntityManagerEventSubscriber;
|
|
|
|
use App\Entity\Bill;
|
|
|
|
use Symfony\Component\Filesystem\Filesystem;
|
|
|
|
use App\Core\Slugify\Slugify;
|
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
|
|
|
use App\Core\Notification\MailNotifier;
|
|
|
|
use App\Repository\UserRepositoryQuery;
|
|
|
|
|
|
|
|
class BillEventSubscriber extends EntityManagerEventSubscriber
|
|
|
|
{
|
|
|
|
protected Slugify $slugger;
|
|
|
|
protected MailNotifier $notifier;
|
|
|
|
protected UrlGeneratorInterface $urlGenerator;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
Slugify $slugger,
|
|
|
|
MailNotifier $notifier,
|
|
|
|
UserRepositoryQuery $userQuery,
|
|
|
|
UrlGeneratorInterface $urlGenerator
|
|
|
|
)
|
|
|
|
{
|
|
|
|
$this->slugger = $slugger;
|
|
|
|
$this->notifier = $notifier;
|
|
|
|
$this->userQuery = $userQuery;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getSubscribedEvents()
|
|
|
|
{
|
|
|
|
return array_merge(parent::getSubscribedEvents(), [
|
|
|
|
'bill.status_updated' => ['onStatusUpdate', self::$priority],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function supports(EntityInterface $entity)
|
|
|
|
{
|
|
|
|
return $entity instanceof Bill;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onStatusUpdate(EntityManagerEvent $event)
|
|
|
|
{
|
|
|
|
if (!$this->supports($event->getEntity())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->notifier
|
|
|
|
->setSubject('[Tinternet][Facture] '.$event->getEntity()->getHumanStatus())
|
2023-04-12 18:27:47 +02:00
|
|
|
->addRecipientsByUsers($this->userQuery->where('.isTreasurer = true')->find(),
|
2023-04-10 16:47:59 +02:00
|
|
|
true)
|
|
|
|
->notify('mail/bill/status_updated.html.twig', [
|
|
|
|
'entity' => $event->getEntity(),
|
|
|
|
'user' => $event->getParams()['user'],
|
|
|
|
'show_url' => $this->urlGenerator->generate(
|
|
|
|
'admin_bill_show',
|
|
|
|
[
|
|
|
|
'entity' => $event->getEntity()->getId(),
|
|
|
|
],
|
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL
|
|
|
|
),
|
|
|
|
])
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onPreUpdate(EntityManagerEvent $event)
|
|
|
|
{
|
|
|
|
if (!$this->supports($event->getEntity())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$entity = $event->getEntity();
|
|
|
|
|
|
|
|
$splInfo = new \SplFileInfo($event->getEntity()->getFile());
|
|
|
|
|
|
|
|
$datas = [];
|
|
|
|
|
|
|
|
if ($entity->getDate()) {
|
|
|
|
$datas[] = $entity->getDate()->format('Ymd');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($entity->getCategory()) {
|
|
|
|
$datas[] = $this->slugger->slugify($entity->getCategory()->getLabel());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($entity->getVendor()) {
|
|
|
|
$datas[] = $this->slugger->slugify($entity->getVendor()->getLabel());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($entity->getReference()) {
|
|
|
|
$datas[] = $entity->getReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
$datas[] = $entity->getId().'.'.$splInfo->getExtension();
|
|
|
|
|
2024-03-22 09:24:27 +01:00
|
|
|
$expectedFilename = str_replace('/', '-', implode('_', $datas));
|
2023-04-10 16:47:59 +02:00
|
|
|
$expectedPath = dirname($entity->getFile()).'/'.$expectedFilename;
|
|
|
|
|
|
|
|
if ($splInfo->getFilename() !== $expectedFilename) {
|
|
|
|
$fs = new Filesystem();
|
|
|
|
$fs->rename(
|
|
|
|
$event->getEntity()->getFile(),
|
|
|
|
$expectedPath
|
|
|
|
);
|
|
|
|
|
|
|
|
$event->getEntity()->setFile($expectedPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onDelete(EntityManagerEvent $event)
|
|
|
|
{
|
|
|
|
if (!$this->supports($event->getEntity())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fs = new Filesystem();
|
|
|
|
$fs->remove($event->getEntity()->getFile());
|
|
|
|
}
|
|
|
|
}
|