custom/plugins/AcrisTaxCS/src/AcrisTaxCS.php line 26

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\Tax;
  3. use Acris\Tax\Components\Service\VatIdValidationService;
  4. use Doctrine\DBAL\Connection;
  5. use Shopware\Core\Content\Rule\Aggregate\RuleCondition\RuleConditionEntity;
  6. use Shopware\Core\Content\Rule\RuleEntity;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  15. use Shopware\Core\Framework\Plugin;
  16. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  17. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  18. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  19. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  20. use Shopware\Core\System\Country\CountryEntity;
  21. use Shopware\Core\System\CustomField\CustomFieldTypes;
  22. use Shopware\Core\System\Snippet\SnippetEntity;
  23. class AcrisTaxCS extends Plugin
  24. {
  25.     const CUSTOM_FIELD_SET_NAME 'acris_tax_specific_country';
  26.     const CUSTOM_FIELD_SET_NAME_PRODUCT_EXCLUDE_TAX_FREE 'acris_tax_exclude_product_tax_free';
  27.     const DEFAULT_CUSTOM_CUSTOMER_HAS_VAT_ID_RULE_NAME 'ACRIS customer personal data has VAT Reg.No. (Shopware standard)';
  28.     const DEFAULT_CUSTOM_BILLING_ADDRESS_HAS_VAT_ID_RULE_NAME 'ACRIS billing address has VAT Reg.No.';
  29.     const DEFAULT_CUSTOM_SHIPPING_ADDRESS_HAS_VAT_ID_RULE_NAME 'ACRIS shipping address has VAT Reg.No.';
  30.     const DEFAULT_CUSTOM_CUSTOMER_HAS_VALID_VAT_ID_RULE_NAME 'ACRIS customer personal data has valid VAT Reg.No.';
  31.     const DEFAULT_CUSTOM_BILLING_ADDRESS_HAS_VALID_VAT_ID_RULE_NAME 'ACRIS billing address has valid VAT Reg.No.';
  32.     const DEFAULT_CUSTOM_SHIPPING_ADDRESS_HAS_VALID_VAT_ID_RULE_NAME 'ACRIS shipping address has valid VAT Reg.No.';
  33.     const DEFAULT_CUSTOM_EXCLUDE_PRODUCTS_FROM_TAX_FREE_RULE_NAME 'ACRIS exclude products from tax-free';
  34.     const DEFAULT_CUSTOM_SAME_COUNTRY_IN_BILLING_AND_SHIPPING_ADDRESS_NAME 'ACRIS same country in billing and shipping address';
  35.     public function update(UpdateContext $updateContext): void
  36.     {
  37.         if(version_compare($updateContext->getCurrentPluginVersion(), '2.3.0''<')
  38.             && version_compare($updateContext->getUpdatePluginVersion(), '2.3.0''>=')) {
  39.             $this->replaceExistingRuleNames($updateContext->getContext());
  40.         }
  41.         if(version_compare($updateContext->getCurrentPluginVersion(), '6.1.0''<')
  42.             && version_compare($updateContext->getUpdatePluginVersion(), '6.1.0''>=')) {
  43.             $this->assignDefaultVatIdCheckForCountries($updateContext->getContext());
  44.         }
  45.     }
  46.     public function postUpdate(UpdateContext $context): void
  47.     {
  48.         if(version_compare($context->getCurrentPluginVersion(), '6.2.0''<')
  49.             && version_compare($context->getUpdatePluginVersion(), '6.2.0''>=')) {
  50.             $this->insertDefaultValues($context->getContext());
  51.             $this->setDefaultAdvancedTaxFreeRulesSettings($context->getContext());
  52.         }
  53.         if(version_compare($context->getCurrentPluginVersion(), '6.4.0''<')
  54.             && version_compare($context->getUpdatePluginVersion(), '6.4.0''>=')) {
  55.             $this->insertDefaultValues($context->getContext());
  56.         }
  57.         if(version_compare($context->getCurrentPluginVersion(), '6.9.0''<')
  58.             && version_compare($context->getUpdatePluginVersion(), '6.9.0''>=')) {
  59.             $this->insertDefaultValues($context->getContext());
  60.         }
  61.         if(version_compare($context->getCurrentPluginVersion(), '6.14.0''<')
  62.             && version_compare($context->getUpdatePluginVersion(), '6.14.0''>=')) {
  63.             $this->addCustomFields($context->getContext());
  64.         }
  65.         if(version_compare($context->getCurrentPluginVersion(), '6.14.1''<')
  66.             && version_compare($context->getUpdatePluginVersion(), '6.14.1''>=')) {
  67.             $this->removeCustomFields($context->getContext(), [self::CUSTOM_FIELD_SET_NAME_PRODUCT_EXCLUDE_TAX_FREE]);
  68.             $this->addCustomFields($context->getContext());
  69.         }
  70.         $this->insertDefaultValues($context->getContext());
  71.     }
  72.     public function activate(ActivateContext $activateContext): void
  73.     {
  74.         $this->insertDefaultValues($activateContext->getContext());
  75.         $this->assignDefaultVatIdCheckForCountries($activateContext->getContext());
  76.         $this->setDefaultAdvancedTaxFreeRulesSettings($activateContext->getContext());
  77.     }
  78.     public function install(InstallContext $context): void
  79.     {
  80.         $this->addCustomFields($context->getContext());
  81.     }
  82.     public function uninstall(UninstallContext $context): void
  83.     {
  84.         if ($context->keepUserData()) {
  85.             return;
  86.         }
  87.         $this->removeCustomFields($context->getContext(), [self::CUSTOM_FIELD_SET_NAME_PRODUCT_EXCLUDE_TAX_FREE]);
  88.         $this->removeRules($context->getContext());
  89.         $this->cleanupDatabase();
  90.     }
  91.     private function cleanupDatabase(): void
  92.     {
  93.         $connection $this->container->get(Connection::class);
  94.         $connection->executeStatement('DROP TABLE IF EXISTS acris_tax_vat_id_validation_log');
  95.         $connection->executeStatement('DROP TABLE IF EXISTS acris_tax_customer_group_rule');
  96.         $connection->executeStatement('DROP TABLE IF EXISTS acris_tax_country_rule');
  97.         try {
  98.             $connection->executeStatement('ALTER TABLE `customer` DROP `acrisTaxVatIdValidationLogs`');
  99.             $connection->executeStatement('ALTER TABLE `customer_group` DROP `acrisRules`');
  100.             $connection->executeStatement('ALTER TABLE `country` DROP `acrisRules`');
  101.         } catch (\Exception $e) { }
  102.     }
  103.     private function replaceExistingRuleNames(Context $context)
  104.     {
  105.         $ruleConditionRepository $this->container->get('rule_condition.repository');
  106.         /** @var EntitySearchResult $existingRuleConditionSearchResult */
  107.         $existingRuleConditionSearchResult $ruleConditionRepository->search((new Criteria())->addFilter(
  108.             new MultiFilter(MultiFilter::CONNECTION_OR, [
  109.                 new EqualsFilter('type''customerHasVatIdBillingRule'),
  110.                 new EqualsFilter('type''customerHasVatIdShippingRule')
  111.             ])
  112.         ), $context);
  113.         if ($existingRuleConditionSearchResult->count() <= 0) {
  114.             return;
  115.         }
  116.         $changedRuleConditions = [];
  117.         /** @var RuleConditionEntity $ruleConditionEntity */
  118.         foreach ($existingRuleConditionSearchResult->getElements() as $ruleConditionEntity) {
  119.             $value $ruleConditionEntity->getValue();
  120.             if(!is_array($value)) {
  121.                 continue;
  122.             }
  123.             if(array_key_exists('customerHasVatIdBilling'$value)) {
  124.                 $value = ['customerHasVatId' => $value['customerHasVatIdBilling']];
  125.             } elseif(array_key_exists('customerHasVatIdShipping'$value)) {
  126.                 $value = ['customerHasVatId' => $value['customerHasVatIdShipping']];
  127.             } else {
  128.                 continue;
  129.             }
  130.             $changedRuleConditions[] = [
  131.                 'id' => $ruleConditionEntity->getId(),
  132.                 'type' => 'customerHasVatIdRule',
  133.                 'value' => $value
  134.             ];
  135.         }
  136.         if(!empty($changedRuleConditions)) {
  137.             $ruleConditionRepository->upsert($changedRuleConditions$context);
  138.         }
  139.     }
  140.     private function assignDefaultVatIdCheckForCountries(Context $context): void
  141.     {
  142.         /** @var EntityRepositoryInterface $countryRepository */
  143.         $countryRepository $this->container->get('country.repository');
  144.         $criteria = new Criteria();
  145.         $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_AND, [
  146.             new EqualsAnyFilter('iso'VatIdValidationService::DEFAULT_SPECIFIC_COUNTRIES)
  147.         ]));
  148.         /** @var EntitySearchResult $countryIds */
  149.         $countries $countryRepository->search($criteria$context);
  150.         if ($countries->count() === || !$countries->first()) return;
  151.         $updateData = [];
  152.         /** @var CountryEntity $country */
  153.         foreach ($countries->getEntities()->getElements() as $country) {
  154.             $customFields $country->getTranslation('customFields') ?? [];
  155.             if (array_key_exists('acris_tax_specific_country_validate_vat_id'$customFields)) continue;
  156.             $customFields['acris_tax_specific_country_validate_vat_id'] = true;
  157.             $updateData[] = [
  158.                 'id' => $country->getId(),
  159.                 'customFields' => $customFields
  160.             ];
  161.         }
  162.         if (!empty($updateData)) {
  163.             $countryRepository->update($updateData$context);
  164.         }
  165.     }
  166.     private function insertDefaultValues(Context $context): void
  167.     {
  168.         /** @var EntityRepositoryInterface $ruleRepository */
  169.         $ruleRepository $this->container->get('rule.repository');
  170.         $rules = [
  171.             [
  172.                 'name' => self::DEFAULT_CUSTOM_CUSTOMER_HAS_VAT_ID_RULE_NAME,
  173.                 'priority' => 100,
  174.                 'conditions' => [
  175.                     [
  176.                         'type' => 'customerHasVatIdRule',
  177.                         'value' => [
  178.                             'customerHasVatId' => true
  179.                         ]
  180.                     ]
  181.                 ]
  182.             ], [
  183.                 'name' => self::DEFAULT_CUSTOM_BILLING_ADDRESS_HAS_VAT_ID_RULE_NAME,
  184.                 'priority' => 100,
  185.                 'conditions' => [
  186.                     [
  187.                         'type' => 'billingAddressHasVatIdRule',
  188.                         'value' => [
  189.                             'billingAddressHasVatId' => true
  190.                         ]
  191.                     ]
  192.                 ]
  193.             ], [
  194.                 'name' => self::DEFAULT_CUSTOM_SHIPPING_ADDRESS_HAS_VAT_ID_RULE_NAME,
  195.                 'priority' => 100,
  196.                 'conditions' => [
  197.                     [
  198.                         'type' => 'shippingAddressHasVatIdRule',
  199.                         'value' => [
  200.                             'shippingAddressHasVatId' => true
  201.                         ]
  202.                     ]
  203.                 ]
  204.             ], [
  205.                 'name' => self::DEFAULT_CUSTOM_CUSTOMER_HAS_VALID_VAT_ID_RULE_NAME,
  206.                 'priority' => 100,
  207.                 'conditions' => [
  208.                     [
  209.                         'type' => 'customerHasVatIdValidRule',
  210.                         'value' => [
  211.                             'customerHasVatId' => true
  212.                         ]
  213.                     ]
  214.                 ]
  215.             ], [
  216.                 'name' => self::DEFAULT_CUSTOM_BILLING_ADDRESS_HAS_VALID_VAT_ID_RULE_NAME,
  217.                 'priority' => 100,
  218.                 'conditions' => [
  219.                     [
  220.                         'type' => 'billingAddressHasVatIdValidRule',
  221.                         'value' => [
  222.                             'billingAddressHasVatId' => true
  223.                         ]
  224.                     ]
  225.                 ]
  226.             ], [
  227.                 'name' => self::DEFAULT_CUSTOM_SHIPPING_ADDRESS_HAS_VALID_VAT_ID_RULE_NAME,
  228.                 'priority' => 100,
  229.                 'conditions' => [
  230.                     [
  231.                         'type' => 'shippingAddressHasVatIdValidRule',
  232.                         'value' => [
  233.                             'shippingAddressHasVatId' => true
  234.                         ]
  235.                     ]
  236.                 ]
  237.             ], [
  238.                 'name' => self::DEFAULT_CUSTOM_SAME_COUNTRY_IN_BILLING_AND_SHIPPING_ADDRESS_NAME,
  239.                 'priority' => 100,
  240.                 'conditions' => [
  241.                     [
  242.                         'type' => 'sameCountryInBillingAddressAndShippingAddressRule',
  243.                         'value' => [
  244.                             'isSameCountryInBillingAddressAndShippingAddress' => true
  245.                         ]
  246.                     ]
  247.                 ]
  248.             ]];
  249.         list($countryId$countryIds) = $this->getCountryIds($context);
  250.         if (!empty($countryId) && is_string($countryId) && !empty($countryIds) && is_array($countryIds)) {
  251.             $rules[] = [
  252.                 'name' => self::DEFAULT_CUSTOM_EXCLUDE_PRODUCTS_FROM_TAX_FREE_RULE_NAME,
  253.                 'priority' => 100,
  254.                 'conditions' => [
  255.                     [
  256.                         'type' => 'orContainer',
  257.                         'value' => [],
  258.                         'children' => [
  259.                             [
  260.                                 'type' => 'andContainer',
  261.                                 'value' => [],
  262.                                 'children' => [
  263.                                     [
  264.                                         'type' => 'productWarehouse',
  265.                                         'value' => [
  266.                                             'operator' => '=',
  267.                                             'warehouse' => 'EU'
  268.                                         ]
  269.                                     ],
  270.                                     [
  271.                                         'type' => 'customerShippingCountry',
  272.                                         'value' => [
  273.                                             'operator' => '=',
  274.                                             'countryIds' => $countryIds
  275.                                         ]
  276.                                     ]
  277.                                 ]
  278.                             ],
  279.                             [
  280.                                 'type' => 'andContainer',
  281.                                 'value' => [],
  282.                                 'children' => [
  283.                                     [
  284.                                         'type' => 'customerShippingCountry',
  285.                                         'value' => [
  286.                                             'operator' => '=',
  287.                                             'countryIds' => [$countryId]
  288.                                         ],
  289.                                     ]
  290.                                 ]
  291.                             ]
  292.                         ]
  293.                     ]
  294.                 ]
  295.             ];
  296.         }
  297.         foreach ($rules as $rule) {
  298.             try {
  299.                 $this->createIfNotExists($ruleRepository, [['name' => 'name''value' => $rule['name']]], $rule$context);
  300.             } catch (\Throwable $e) {
  301.                 // Do nothing - prevent a Shopware bug for deleting cache for rules
  302.                 // TODO: Fix missing rule id now!
  303.             }
  304.         }
  305.     }
  306.     private function createIfNotExists(EntityRepositoryInterface $repository, array $equalFields, array $dataContext $context): void
  307.     {
  308.         $filters = [];
  309.         foreach ($equalFields as $equalField) {
  310.             $filters[] = new EqualsFilter($equalField['name'], $equalField['value']);
  311.         }
  312.         if (sizeof($filters) > 1) {
  313.             $filter = new MultiFilter(MultiFilter::CONNECTION_OR$filters);
  314.         } else {
  315.             $filter array_shift($filters);
  316.         }
  317.         $searchResult $repository->search((new Criteria())->addFilter($filter), $context);
  318.         if (empty($searchResult->first())) {
  319.             $repository->create([$data], $context);
  320.         }
  321.     }
  322.     private function removeRules(Context $context): void
  323.     {
  324.         /** @var EntityRepositoryInterface $rulesRepository */
  325.         $rulesRepository $this->container->get('rule.repository');
  326.         /** @var EntitySearchResult $rulesResult */
  327.         $rulesResult $rulesRepository->search((new Criteria())->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
  328.             new EqualsFilter('name'self::DEFAULT_CUSTOM_CUSTOMER_HAS_VAT_ID_RULE_NAME),
  329.             new EqualsFilter('name'self::DEFAULT_CUSTOM_BILLING_ADDRESS_HAS_VAT_ID_RULE_NAME),
  330.             new EqualsFilter('name'self::DEFAULT_CUSTOM_SHIPPING_ADDRESS_HAS_VAT_ID_RULE_NAME),
  331.             new EqualsFilter('name'self::DEFAULT_CUSTOM_CUSTOMER_HAS_VALID_VAT_ID_RULE_NAME),
  332.             new EqualsFilter('name'self::DEFAULT_CUSTOM_BILLING_ADDRESS_HAS_VALID_VAT_ID_RULE_NAME),
  333.             new EqualsFilter('name'self::DEFAULT_CUSTOM_SHIPPING_ADDRESS_HAS_VALID_VAT_ID_RULE_NAME),
  334.             new EqualsFilter('name'self::DEFAULT_CUSTOM_SAME_COUNTRY_IN_BILLING_AND_SHIPPING_ADDRESS_NAME),
  335.             new EqualsFilter('name'self::DEFAULT_CUSTOM_EXCLUDE_PRODUCTS_FROM_TAX_FREE_RULE_NAME),
  336.         ])), $context);
  337.         if ($rulesResult->getTotal() > && $rulesResult->first()) {
  338.             $ruleIds = [];
  339.             /** @var RuleEntity $rule */
  340.             foreach ($rulesResult->getEntities()->getElements() as $rule) {
  341.                 $ruleIds[] = ['id' => $rule->getId()];
  342.             }
  343.             $rulesRepository->delete($ruleIds$context);
  344.         }
  345.     }
  346.     private function removeCustomFields(Context $context, array $setNames): void
  347.     {
  348.         /* Check for snippets if they exist for custom fields */
  349.         $this->checkForExistingCustomFieldSnippets($context);
  350.         $customFieldSet $this->container->get('custom_field_set.repository');
  351.         foreach ($setNames as $setName) {
  352.             $id $customFieldSet->searchIds((new Criteria())->addFilter(new EqualsFilter('name'$setName)), $context)->firstId();
  353.             if($id$customFieldSet->delete([['id' => $id]], $context);
  354.         }
  355.     }
  356.     private function setDefaultAdvancedTaxFreeRulesSettings(Context $context)
  357.     {
  358.         /** @var EntityRepositoryInterface $ruleRepository */
  359.         $ruleRepository $this->container->get('rule.repository');
  360.         $configService $this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService');
  361.         if($configService->get('AcrisTaxCS.config.acrisAdvancedTaxRules') === null) {
  362.             /** @var IdSearchResult $idSearchResult */
  363.             $idSearchResult $ruleRepository->searchIds((new Criteria())->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
  364.                 new EqualsFilter('name'self::DEFAULT_CUSTOM_CUSTOMER_HAS_VAT_ID_RULE_NAME),
  365.                 new EqualsFilter('name'self::DEFAULT_CUSTOM_BILLING_ADDRESS_HAS_VAT_ID_RULE_NAME),
  366.                 new EqualsFilter('name'self::DEFAULT_CUSTOM_SHIPPING_ADDRESS_HAS_VAT_ID_RULE_NAME),
  367.             ])), $context);
  368.             if ($idSearchResult->getTotal() > && !empty($idSearchResult->firstId())) {
  369.                 $configService->set('AcrisTaxCS.config.acrisAdvancedTaxRules'$idSearchResult->getIds());
  370.             }
  371.         }
  372.     }
  373.     private function getCountryIds(Context $context): array
  374.     {
  375.         $countryId null;
  376.         $countryIds = [];
  377.         $countryISOs = [
  378.             'DE',
  379.             'AT',
  380.         ];
  381.         $countryISO 'NL';
  382.         $allISOs array_merge($countryISOs, [$countryISO]);
  383.         /** @var EntityRepositoryInterface $countryRepository */
  384.         $countryRepository $this->container->get('country.repository');
  385.         $criteria = (new Criteria())->addFilter(new EqualsAnyFilter('iso'$allISOs));
  386.         $countryCollection $countryRepository->search($criteria$context)->getEntities();
  387.         if ($countryCollection->count() === 0) {
  388.             return [nullnull];
  389.         }
  390.         $valid true;
  391.         /** @var CountryEntity $country */
  392.         foreach ($countryCollection->getElements() as $country) {
  393.             if ($country->getIso() === $countryISO) {
  394.                 $countryId $country->getId();
  395.                 continue;
  396.             }
  397.             if (in_array($country->getIso(), $countryISOs)) {
  398.                 $countryIds[] = $country->getId();
  399.                 continue;
  400.             }
  401.             $valid false;
  402.         }
  403.         if ($valid !== true) {
  404.             return [nullnull];
  405.         }
  406.         return [$countryId$countryIds];
  407.     }
  408.     private function addCustomFields(Context $context): void
  409.     {
  410.         /* Check for snippets if they exist for custom fields */
  411.         $this->checkForExistingCustomFieldSnippets($context);
  412.         $customFieldSet $this->container->get('custom_field_set.repository');
  413.         if($customFieldSet->search((new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOM_FIELD_SET_NAME_PRODUCT_EXCLUDE_TAX_FREE)), $context)->count() == 0) {
  414.             $customFieldSet->create([[
  415.                 'name' => self::CUSTOM_FIELD_SET_NAME_PRODUCT_EXCLUDE_TAX_FREE,
  416.                 'config' => [
  417.                     'label' => [
  418.                         'en-GB' => 'ACRIS Tax',
  419.                         'de-DE' => 'ACRIS Steuer'
  420.                     ]
  421.                 ],
  422.                 'customFields' => [
  423.                     ['name' => 'acris_tax_exclude_product_tax_free_warehouse''type' => CustomFieldTypes::TEXT,
  424.                         'config' => [
  425.                             'componentName' => 'sw-field',
  426.                             'type' => 'text',
  427.                             'customFieldType' => 'text',
  428.                             'customFieldPosition' => 1,
  429.                             'label' => [
  430.                                 'en-GB' => 'Tax tag',
  431.                                 'de-DE' => 'Steuer-Tag'
  432.                             ],
  433.                             'helpText' => [
  434.                                 'en-GB' => 'This field will be used and checked in the "Tax tag" rule condition.',
  435.                                 'de-DE' => 'Dieses Feld wird in der Regelbedingung "Steuer-Tag" verwendet und geprüft.'
  436.                             ],
  437.                             'placeholder' => [
  438.                                 'en-GB' => 'Enter tax tag...',
  439.                                 'de-DE' => 'Steuer-Tag eingeben...'
  440.                             ]
  441.                         ]]
  442.                 ],
  443.                 'relations' => [
  444.                     [
  445.                         'entityName' => 'product'
  446.                     ]
  447.                 ]
  448.             ]], $context);
  449.         }
  450.     }
  451.     private function checkForExistingCustomFieldSnippets(Context $context)
  452.     {
  453.         /** @var EntityRepositoryInterface $snippetRepository */
  454.         $snippetRepository $this->container->get('snippet.repository');
  455.         $criteria = new Criteria();
  456.         $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
  457.             new EqualsFilter('translationKey''customFields.' 'acris_tax_exclude_product_tax_free_warehouse')
  458.         ]));
  459.         /** @var EntitySearchResult $searchResult */
  460.         $searchResult $snippetRepository->search($criteria$context);
  461.         if ($searchResult->count() > 0) {
  462.             $snippetIds = [];
  463.             /** @var SnippetEntity $snippet */
  464.             foreach ($searchResult->getEntities()->getElements() as $snippet) {
  465.                 $snippetIds[] = [
  466.                     'id' => $snippet->getId()
  467.                 ];
  468.             }
  469.             if (!empty($snippetIds)) {
  470.                 $snippetRepository->delete($snippetIds$context);
  471.             }
  472.         }
  473.     }
  474. }