-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMailArchiveDeleteSubscriber.php
More file actions
56 lines (47 loc) · 1.84 KB
/
Copy pathMailArchiveDeleteSubscriber.php
File metadata and controls
56 lines (47 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
declare(strict_types=1);
namespace Frosh\MailArchive\Subscriber;
use Frosh\MailArchive\Content\MailArchive\MailArchiveDefinition;
use Frosh\MailArchive\Content\MailArchive\MailArchiveEntity;
use Frosh\MailArchive\Services\EmlFileManager;
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeleteEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MailArchiveDeleteSubscriber implements EventSubscriberInterface
{
/**
* @param EntityRepository<EntityCollection<MailArchiveEntity>> $froshMailArchiveRepository
*/
public function __construct(
private readonly EntityRepository $froshMailArchiveRepository,
private readonly EmlFileManager $emlFileManager,
) {}
public static function getSubscribedEvents(): array
{
return [
EntityDeleteEvent::class => 'beforeDelete',
];
}
public function beforeDelete(EntityDeleteEvent $event): void
{
/** @var array<string> $ids */
$ids = array_values($event->getIds(MailArchiveDefinition::ENTITY_NAME));
if (empty($ids)) {
return;
}
$criteria = new Criteria($ids);
$criteria->addFields(['emlPath']);
$mails = $this->froshMailArchiveRepository->search($criteria, $event->getContext())->getEntities();
foreach ($mails as $mail) {
$emlPath = $mail->get('emlPath');
if (empty($emlPath) || !\is_string($emlPath)) {
continue;
}
$event->addSuccess(function () use ($emlPath): void {
$this->emlFileManager->deleteEmlFile($emlPath);
});
}
}
}