vendor/cio-sw6/cio-data-retention/src/CioDataRetention.php line 17

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace CioDataRetention;
  4. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  9. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  10. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  11. use Shopware\Core\Framework\Plugin;
  12. use Shopware\Core\Framework\Uuid\Uuid;
  13. class CioDataRetention extends Plugin
  14. {
  15.     // CIO-AI-Driven
  16.     public const CUSTOMER_DELETION_MAIL_TEMPLATE_TYPE_NAME = 'Customer account deletion (Double-Opt-Out)';
  17.     public const CUSTOMER_DELETION_MAIL_TEMPLATE_TYPE_TECHNICAL_NAME = 'cio_data_retention_customer_deletion';
  18.     public function install(InstallContext $installContext): void
  19.     {
  20.         parent::install($installContext);
  21.         $this->ensureCustomerDeletionMailTemplateExists($installContext->getContext());
  22.     }
  23.     public function activate(ActivateContext $activateContext): void
  24.     {
  25.         parent::activate($activateContext);
  26.         // CIO-AI-Driven: ensure template exists even if plugin was already installed before this feature was added.
  27.         $this->ensureCustomerDeletionMailTemplateExists($activateContext->getContext());
  28.     }
  29.     public function uninstall(UninstallContext $uninstallContext): void
  30.     {
  31.         parent::uninstall($uninstallContext);
  32.         if ($uninstallContext->keepUserData()) {
  33.             return;
  34.         }
  35.         /** @var EntityRepositoryInterface $mailTemplateTypeRepository */
  36.         $mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
  37.         /** @var EntityRepositoryInterface $mailTemplateRepository */
  38.         $mailTemplateRepository = $this->container->get('mail_template.repository');
  39.         $mailTemplateType = $mailTemplateTypeRepository->search(
  40.             (new Criteria())->addFilter(new EqualsFilter('technicalName', self::CUSTOMER_DELETION_MAIL_TEMPLATE_TYPE_TECHNICAL_NAME)),
  41.             $uninstallContext->getContext()
  42.         )->first();
  43.         if ($mailTemplateType === null) {
  44.             return;
  45.         }
  46.         $mailTemplateIds = $mailTemplateRepository->searchIds(
  47.             (new Criteria())->addFilter(new EqualsFilter('mailTemplateTypeId', $mailTemplateType->getId())),
  48.             $uninstallContext->getContext()
  49.         )->getIds();
  50.         if ($mailTemplateIds !== []) {
  51.             $mailTemplateRepository->delete(array_map(static fn (string $id): array => ['id' => $id], $mailTemplateIds), $uninstallContext->getContext());
  52.         }
  53.         $mailTemplateTypeRepository->delete([
  54.             ['id' => $mailTemplateType->getId()],
  55.         ], $uninstallContext->getContext());
  56.     }
  57.     private function ensureCustomerDeletionMailTemplateExists(\Shopware\Core\Framework\Context $context): void
  58.     {
  59.         /** @var EntityRepositoryInterface $mailTemplateTypeRepository */
  60.         $mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
  61.         /** @var EntityRepositoryInterface $mailTemplateRepository */
  62.         $mailTemplateRepository = $this->container->get('mail_template.repository');
  63.         $existingType = $mailTemplateTypeRepository->search(
  64.             (new Criteria())->addFilter(new EqualsFilter('technicalName', self::CUSTOMER_DELETION_MAIL_TEMPLATE_TYPE_TECHNICAL_NAME)),
  65.             $context
  66.         )->first();
  67.         $mailTemplateTypeId = $existingType ? $existingType->getId() : Uuid::randomHex();
  68.         if ($existingType === null) {
  69.             try {
  70.                 $mailTemplateTypeRepository->create([
  71.                     [
  72.                         'id' => $mailTemplateTypeId,
  73.                         'name' => self::CUSTOMER_DELETION_MAIL_TEMPLATE_TYPE_NAME,
  74.                         'technicalName' => self::CUSTOMER_DELETION_MAIL_TEMPLATE_TYPE_TECHNICAL_NAME,
  75.                         'availableEntities' => [
  76.                             'salesChannel' => 'sales_channel',
  77.                         ],
  78.                     ],
  79.                 ], $context);
  80.             } catch (UniqueConstraintViolationException $exception) {
  81.                 // created concurrently / already exists
  82.             }
  83.         }
  84.         $existingTemplateId = $mailTemplateRepository->searchIds(
  85.             (new Criteria())
  86.                 ->addFilter(new EqualsFilter('mailTemplateTypeId', $mailTemplateTypeId))
  87.                 ->setLimit(1),
  88.             $context
  89.         )->firstId();
  90.         if ($existingTemplateId !== null) {
  91.             return;
  92.         }
  93.         try {
  94.             $mailTemplateRepository->create([
  95.                 [
  96.                     'id' => Uuid::randomHex(),
  97.                     'mailTemplateTypeId' => $mailTemplateTypeId,
  98.                     'subject' => [
  99.                         'de-DE' => 'Bestätige die Löschung deines Kundenkontos',
  100.                         'en-GB' => 'Confirm deletion of your customer account',
  101.                     ],
  102.                     'senderName' => [
  103.                         'de-DE' => '{{ salesChannel.name }}',
  104.                         'en-GB' => '{{ salesChannel.name }}',
  105.                     ],
  106.                     'description' => [
  107.                         'de-DE' => 'CioDataRetention - Bestätigung der Kundenkonto Löschung',
  108.                         'en-GB' => 'CioDataRetention - Customer account deletion confirmation',
  109.                     ],
  110.                     'contentPlain' => [
  111.                         'de-DE' => "Hallo,\n\nbitte bestätige die Löschung deines Kundenkontos über folgenden Link:\n\n{{ confirmUrl }}\n\nWenn du diese Anfrage nicht gestellt hast, ignoriere bitte diese E-Mail.\n",
  112.                         'en-GB' => "Hello,\n\nplease confirm deletion of your customer account via the following link:\n\n{{ confirmUrl }}\n\nIf you did not request this, please ignore this email.\n",
  113.                     ],
  114.                     'contentHtml' => [
  115.                         'de-DE' => '<p>Hallo,</p><p>bitte bestätige die Löschung deines Kundenkontos über folgenden Link:</p><p><a href="{{ confirmUrl }}">{{ confirmUrl }}</a></p><p>Wenn du diese Anfrage nicht gestellt hast, ignoriere bitte diese E-Mail.</p>',
  116.                         'en-GB' => '<p>Hello,</p><p>please confirm deletion of your customer account via the following link:</p><p><a href="{{ confirmUrl }}">{{ confirmUrl }}</a></p><p>If you did not request this, please ignore this email.</p>',
  117.                     ],
  118.                 ],
  119.             ], $context);
  120.         } catch (UniqueConstraintViolationException $exception) {
  121.             // already exists
  122.         }
  123.     }
  124. }