custom/plugins/AlbisLease/src/AlbisLease.php line 28

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Albis\AlbisLease;
  3. use Albis\AlbisLease\Service\AlbisPayment;
  4. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  5. use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
  6. use Shopware\Core\Content\Rule\Aggregate\RuleCondition\RuleConditionCollection;
  7. use Shopware\Core\Content\Rule\Aggregate\RuleCondition\RuleConditionEntity;
  8. use Shopware\Core\Content\Rule\RuleEntity;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Core\Framework\Event\BusinessEventInterface;
  14. use Shopware\Core\Framework\Event\BusinessEventRegistry;
  15. use Shopware\Core\Framework\Event\EventAction\Aggregate\EventActionRule\EventActionRuleDefinition;
  16. use Shopware\Core\Framework\Event\EventAction\EventActionCollection;
  17. use Shopware\Core\Framework\Event\EventAction\EventActionEntity;
  18. use Shopware\Core\Framework\Plugin;
  19. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  20. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  21. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  22. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  23. use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
  24. use Shopware\Core\Framework\Uuid\Uuid;
  25. class AlbisLease extends Plugin
  26. {
  27.     private $paymentMethodId null;
  28.     private $mailTemplateTypeId null;
  29.     private $mailTemplateId null;
  30.     private $ruleId null;
  31.     public function install(InstallContext $installContext): void
  32.     {
  33.         parent::install($installContext);
  34.         $this->paymentMethodId Uuid::randomHex();
  35.         $this->mailTemplateTypeId Uuid::randomHex();
  36.         $this->mailTemplateId Uuid::randomHex();
  37.         $this->ruleId Uuid::randomHex();
  38.         $this->createAlbisProductGroups($installContext->getContext());
  39.         $this->createAlbisPaymentMethod($installContext->getContext());
  40.         $this->createAlbisMailTemplate($installContext->getContext());
  41.         $this->createAlbisPaymentRule($installContext->getContext());
  42.         $this->createAlbisBusinessEvent($installContext->getContext());
  43.     }
  44.     public function uninstall(UninstallContext $context): void
  45.     {
  46.         // Only set the payment method to inactive when uninstalling. Removing the payment method would
  47.         // cause data consistency issues, since the payment method might have been used in several orders
  48.         $this->setPaymentMethodIsActive(false$context->getContext());
  49.         //Keep UserData? Then do nothing here
  50.         if ($context->keepUserData()) {
  51.             return;
  52.         }
  53.         $this->deleteAlbisPaymentMethod($context);
  54.         $this->deleteAlbisMailTemplate($context);
  55.         $this->deleteAlbisPaymentRule($context);
  56.         $this->deleteAlbisBusinessEvent($context);
  57.     }
  58.     public function activate(ActivateContext $context): void
  59.     {
  60.         $this->setPaymentMethodIsActive(true$context->getContext());
  61.         parent::activate($context);
  62.     }
  63.     public function deactivate(DeactivateContext $context): void
  64.     {
  65.         $this->setPaymentMethodIsActive(false$context->getContext());
  66.         parent::deactivate($context);
  67.     }
  68.     private function setPaymentMethodIsActive(bool $activeContext $context): void
  69.     {
  70.         /** @var EntityRepositoryInterface $paymentRepository */
  71.         $paymentRepository $this->container->get('payment_method.repository');
  72.         $paymentMethodId $this->getPaymentMethodId();
  73.         // Payment does not even exist, so nothing to (de-)activate here
  74.         if (!$paymentMethodId) {
  75.             return;
  76.         }
  77.         $paymentMethod = [
  78.             'id' => $paymentMethodId,
  79.             'active' => $active,
  80.         ];
  81.         $paymentRepository->update([$paymentMethod], $context);
  82.     }
  83.     private function getPaymentMethodId(): ?string
  84.     {
  85.         /** @var EntityRepositoryInterface $paymentRepository */
  86.         $paymentRepository $this->container->get('payment_method.repository');
  87.         // Fetch ID for update
  88.         $paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('handlerIdentifier'AlbisPayment::class));
  89.         $paymentIds $paymentRepository->searchIds($paymentCriteriaContext::createDefaultContext());
  90.         if ($paymentIds->getTotal() === 0) {
  91.             return null;
  92.         }
  93.         return $paymentIds->getIds()[0];
  94.     }
  95.     private function createAlbisProductGroups(Context $context):void
  96.     {
  97.     }
  98.     private function createAlbisPaymentMethod(Context $context): void
  99.     {
  100.         $paymentMethodExists $this->getPaymentMethodId();
  101.         // Payment method exists already, no need to continue here
  102.         if ($paymentMethodExists) {
  103.             return;
  104.         }
  105.         /** @var PluginIdProvider $pluginIdProvider */
  106.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  107.         $pluginId $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
  108.         /** @var EntityRepositoryInterface $paymentRepository */
  109.         $paymentRepository $this->container->get('payment_method.repository');
  110.         $paymentRepository->create([[
  111.             'id' => $this->paymentMethodId,
  112.             'handlerIdentifier' => AlbisPayment::class,
  113.             'name' => 'Albis Leasing',
  114.             'description' => 'Leasing über Albis lease',
  115.             'pluginId' => $pluginId,
  116.             'active' => true,
  117.             'afterOrderEnabled' => false
  118.         ]], $context);
  119.     }
  120.     private function createAlbisMailTemplate(Context $context): void
  121.     {
  122.         /** @var EntityRepositoryInterface $mailTemplateTypeRepository */
  123.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  124.         /** @var EntityRepositoryInterface $mailTemplateRepository */
  125.         $mailTemplateRepository $this->container->get('mail_template.repository');
  126.         $mailTemplateType = [
  127.             [
  128.                 'id' => $this->mailTemplateTypeId,
  129.                 'name' => 'Albis Leasingantrag übermittelt',
  130.                 'technicalName' => 'albis_contract_submit',
  131.                 'availableEntities' => [
  132.                     'product' => 'product',
  133.                     'salesChannel' => 'sales_channel',
  134.                     'order' => 'order'
  135.                 ]
  136.             ]
  137.         ];
  138.         $mailTemplate = [
  139.             [
  140.                 'id' => $this->mailTemplateId,
  141.                 'mailTemplateTypeId' => $this->mailTemplateTypeId,
  142.                 'subject' => [
  143.                     'en-GB' => 'We have received your Leasing application',
  144.                     'de-DE' => 'Wir haben Ihren Leasingantrag erhalten'
  145.                 ],
  146.                 'description' => 'Leasingantrag wurde nach einer Bestellung eingereicht',
  147.                 'senderName' => '{{ salesChannel.name }}',
  148.                 'contentPlain' => file_get_contents(__DIR__  '/Resources/snippet/mail/contract_submit/plain.txt'),
  149.                 'contentHtml' => file_get_contents(__DIR__  '/Resources/snippet/mail/contract_submit/html.txt')
  150.             ]
  151.         ];
  152.         try {
  153.             $mailTemplateTypeRepository->create($mailTemplateType$context);
  154.             $mailTemplateRepository->create($mailTemplate$context);
  155.         } catch (UniqueConstraintViolationException $exception) {
  156.             // No, we've already installed the fields, it's fine.
  157.         }
  158.     }
  159.     private function createAlbisPaymentRule(Context $context): void
  160.     {
  161.         $ruleRepository $this->container->get('rule.repository');
  162.         $ruleRepository->create([[
  163.             'id' => $this->ruleId,
  164.             'name' => 'Bestellungen mit der Albis Leasing Zahlungsart',
  165.             'priority' => 0,
  166.             'description' => 'Alle Bestellungen, die mit der Albis Leasing Zahhlungsart übermittelt wurden. 2',
  167.             'conditions' => [
  168.                 [
  169.                     'type' => 'paymentMethod',
  170.                     'value' => [
  171.                         'operator' => '=',
  172.                         'paymentMethodIds' => [
  173.                             '60c601c37b0040b0ada60d3e4a168e46'
  174.                         ]
  175.                     ]
  176.                 ]
  177.             ]
  178.         ]], $context);
  179.     }
  180.     private function createAlbisBusinessEvent(Context $context): void
  181.     {
  182.         $eventActionRepository $this->container->get('event_action.repository');
  183.         $eventActionRepository->create([[
  184.             'eventName' => 'albis.leasing.application.placed',
  185.             'actionName' => 'action.mail.send',
  186.             'title' => 'Albis Leasingantrag wurde eingereicht',
  187.             'description' => 'Albis',
  188.             'active' => true,
  189.             'config' => [
  190.                 'mail_template_id' => $this->mailTemplateId,
  191.                 'mail_template_type_id' => $this->mailTemplateTypeId
  192.             ],
  193. //            'rules' => [
  194. //                [
  195. //                    'id' => $this->ruleId
  196. //                ],
  197. //            ],
  198.         ]], $context);
  199.     }
  200.     private function deleteAlbisPaymentMethod(UninstallContext $uninstallContext)
  201.     {
  202.         $paymentMethodId $this->getPaymentMethodId();
  203.         // Payment method doesnt exists, no need to continue here
  204.         if (!$paymentMethodId) {
  205.             return;
  206.         }
  207.         // @Bug-961215 - Wait for shopware 6 plugin payment method deletion by api
  208. //        /** @var EntityRepositoryInterface $paymentRepository */
  209. //        $paymentRepository = $this->container->get('payment_method.repository');
  210. //
  211. //        $paymentRepository->delete([
  212. //            [
  213. //                'id' => $paymentMethodId
  214. //            ]
  215. //        ], $uninstallContext->getContext());
  216.     }
  217.     private function deleteAlbisMailTemplate(UninstallContext $uninstallContext)
  218.     {
  219.         /** @var EntityRepositoryInterface $mailTemplateTypeRepository */
  220.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  221.         /** @var EntityRepositoryInterface $mailTemplateRepository */
  222.         $mailTemplateRepository $this->container->get('mail_template.repository');
  223.         /** @var MailTemplateTypeEntity $myCustomMailTemplateType */
  224.         $myCustomMailTemplateType $mailTemplateTypeRepository->search(
  225.             (new Criteria())->addFilter(new EqualsFilter('technicalName''albis_contract_submit')),
  226.             $uninstallContext->getContext()
  227.         )->first();
  228.         $mailTemplateIds $mailTemplateRepository->searchIds(
  229.             (new Criteria())->addFilter(new EqualsFilter('mailTemplateTypeId'$myCustomMailTemplateType->getId())),
  230.             $uninstallContext->getContext()
  231.         )->getIds();
  232.         //Get the Ids from the fetched Entities
  233.         $ids array_map(static function ($id) {
  234.             return ['id' => $id];
  235.         }, $mailTemplateIds);
  236.         $mailTemplateRepository->delete($ids$uninstallContext->getContext());
  237.         $mailTemplateTypeRepository->delete([
  238.             [
  239.                 'id' => $myCustomMailTemplateType->getId()
  240.             ]
  241.         ], $uninstallContext->getContext());
  242.     }
  243.     private function deleteAlbisPaymentRule(UninstallContext $context): void
  244.     {
  245.         $ruleRepository $this->container->get('rule.repository');
  246.         /** @var RuleEntity $paymentRule */
  247.         $paymentRule $ruleRepository->search(
  248.             (new Criteria())->addFilter(new EqualsFilter('name''Bestellungen mit der Albis Leasing Zahlungsart')),
  249.             $context->getContext()
  250.         )->first();
  251.         // Rule doesnt exists, no need to continue here
  252.         if(!$paymentRule) {
  253.             return;
  254.         }
  255.         $ruleRepository->delete([
  256.             [
  257.                 'id' => $paymentRule->getId()
  258.             ]
  259.         ], $context->getContext());
  260.     }
  261.     private function deleteAlbisBusinessEvent(UninstallContext $context): void
  262.     {
  263.         $eventActionRepository $this->container->get('event_action.repository');
  264.         /** @var EventActionEntity $paymentRule */
  265.         $eventAction $eventActionRepository->search(
  266.             (new Criteria())->addFilter(new EqualsFilter('title''Albis Leasingantrag wurde eingereicht')),
  267.             $context->getContext()
  268.         )->first();
  269.         // Rule doesnt exists, no need to continue here
  270.         if(!$eventAction) {
  271.             return;
  272.         }
  273.         $eventActionRepository->delete([
  274.             [
  275.                 'id' => $eventAction->getId()
  276.             ]
  277.         ], $context->getContext());
  278.     }
  279. }