vendor/wns/security-compliance-suite/src/WnsSecurityComplianceSuite.php line 20

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WnsSecurityComplianceSuite;
  4. use Shopware\Core\Checkout\Customer\CustomerDefinition;
  5. use Shopware\Core\Checkout\Customer\CustomerEntity;
  6. use Shopware\Core\Checkout\Order\OrderDefinition;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\Plugin;
  12. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  13. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  14. use Shopware\Core\Framework\Uuid\Uuid;
  15. use WnsSecurityComplianceSuite\SanctionList\Constants\ValidationStatus;
  16. class WnsSecurityComplianceSuite extends Plugin
  17. {
  18. public const CONFIG_PLUGIN_CHECK_BILLING_ADDRESS = 'WnsSecurityComplianceSuite.config.checkBillingAddress';
  19. public const CONFIG_PLUGIN_CHECK_SHIPPING_ADDRESS = 'WnsSecurityComplianceSuite.config.checkShippingAddress';
  20. public const ORDER_SANCTION_INVALID_FLAG_CUSTOMFIELD = 'order_sanction_validation_flag';
  21. public const ORDER_SANCTION_TECHNICAL_NAME = 'order_sanction_validation';
  22. public const CUSTOMER_SANCTION_INVALID_FLAG_CUSTOMFIELD = 'customer_sanction_validation_flag';
  23. public const CUSTOMER_SANCTION_TECHNICAL_NAME = 'customer_sanction_validation';
  24. public const API_ACCOUNT_TYPE_ARR = [
  25. CustomerEntity::ACCOUNT_TYPE_BUSINESS => 'company',
  26. CustomerEntity::ACCOUNT_TYPE_PRIVATE => 'individual',
  27. ];
  28. public function install(InstallContext $installContext): void
  29. {
  30. parent::install($installContext);
  31. $this->createCustomFields($installContext->getContext());
  32. }
  33. public function activate(ActivateContext $activateContext): void
  34. {
  35. parent::activate($activateContext);
  36. $this->createCustomFields($activateContext->getContext());
  37. }
  38. private function createCustomFields(Context $context): void
  39. {
  40. /** @var EntityRepository $customFieldSetRepo */
  41. $customFieldSetRepo = $this->container->get('custom_field_set.repository'); // @phpstan-ignore-line
  42. $this->createCustomFieldSetIfNotExists(
  43. $customFieldSetRepo,
  44. $context,
  45. self::CUSTOMER_SANCTION_TECHNICAL_NAME,
  46. self::CUSTOMER_SANCTION_INVALID_FLAG_CUSTOMFIELD,
  47. [
  48. 'en-GB' => 'Sanction Validation - Customer',
  49. 'de-DE' => 'Sanktionsprüfung - Kunde',
  50. ],
  51. CustomerDefinition::ENTITY_NAME
  52. );
  53. $this->createCustomFieldSetIfNotExists(
  54. $customFieldSetRepo,
  55. $context,
  56. self::ORDER_SANCTION_TECHNICAL_NAME,
  57. self::ORDER_SANCTION_INVALID_FLAG_CUSTOMFIELD,
  58. [
  59. 'en-GB' => 'Sanction Validation - Order',
  60. 'de-DE' => 'Sanktionsprüfung - Bestellung',
  61. ],
  62. OrderDefinition::ENTITY_NAME
  63. );
  64. }
  65. private function createCustomFieldSetIfNotExists(
  66. EntityRepository $customFieldSetRepo,
  67. Context $context,
  68. string $technicalName,
  69. string $fieldName,
  70. array $label,
  71. string $entityName
  72. ): void {
  73. $criteria = new Criteria();
  74. $criteria->addFilter(new EqualsFilter('name', $technicalName));
  75. $existing = $customFieldSetRepo->search($criteria, $context);
  76. if ($existing->count() > 0) {
  77. return;
  78. }
  79. $customFieldSetRepo->create([
  80. [
  81. 'id' => Uuid::randomHex(),
  82. 'name' => $technicalName,
  83. 'config' => [
  84. 'label' => $label,
  85. ],
  86. 'customFields' => [
  87. [
  88. 'name' => $fieldName,
  89. 'type' => 'select',
  90. 'config' => [
  91. 'label' => [
  92. 'en-GB' => 'Sanction Validation',
  93. 'de-DE' => 'Sanktionsprüfung',
  94. ],
  95. 'componentName' => 'sw-single-select',
  96. 'customFieldType' => 'select',
  97. 'options' => ValidationStatus::getOptions(),
  98. ],
  99. ],
  100. ],
  101. 'relations' => [
  102. ['entityName' => $entityName],
  103. ],
  104. ],
  105. ], $context);
  106. }
  107. }