<?php
declare(strict_types=1);
namespace WnsSecurityComplianceSuite;
use Shopware\Core\Checkout\Customer\CustomerDefinition;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Order\OrderDefinition;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Uuid\Uuid;
use WnsSecurityComplianceSuite\SanctionList\Constants\ValidationStatus;
class WnsSecurityComplianceSuite extends Plugin
{
public const CONFIG_PLUGIN_CHECK_BILLING_ADDRESS = 'WnsSecurityComplianceSuite.config.checkBillingAddress';
public const CONFIG_PLUGIN_CHECK_SHIPPING_ADDRESS = 'WnsSecurityComplianceSuite.config.checkShippingAddress';
public const ORDER_SANCTION_INVALID_FLAG_CUSTOMFIELD = 'order_sanction_validation_flag';
public const ORDER_SANCTION_TECHNICAL_NAME = 'order_sanction_validation';
public const CUSTOMER_SANCTION_INVALID_FLAG_CUSTOMFIELD = 'customer_sanction_validation_flag';
public const CUSTOMER_SANCTION_TECHNICAL_NAME = 'customer_sanction_validation';
public const API_ACCOUNT_TYPE_ARR = [
CustomerEntity::ACCOUNT_TYPE_BUSINESS => 'company',
CustomerEntity::ACCOUNT_TYPE_PRIVATE => 'individual',
];
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->createCustomFields($installContext->getContext());
}
public function activate(ActivateContext $activateContext): void
{
parent::activate($activateContext);
$this->createCustomFields($activateContext->getContext());
}
private function createCustomFields(Context $context): void
{
/** @var EntityRepository $customFieldSetRepo */
$customFieldSetRepo = $this->container->get('custom_field_set.repository'); // @phpstan-ignore-line
$this->createCustomFieldSetIfNotExists(
$customFieldSetRepo,
$context,
self::CUSTOMER_SANCTION_TECHNICAL_NAME,
self::CUSTOMER_SANCTION_INVALID_FLAG_CUSTOMFIELD,
[
'en-GB' => 'Sanction Validation - Customer',
'de-DE' => 'Sanktionsprüfung - Kunde',
],
CustomerDefinition::ENTITY_NAME
);
$this->createCustomFieldSetIfNotExists(
$customFieldSetRepo,
$context,
self::ORDER_SANCTION_TECHNICAL_NAME,
self::ORDER_SANCTION_INVALID_FLAG_CUSTOMFIELD,
[
'en-GB' => 'Sanction Validation - Order',
'de-DE' => 'Sanktionsprüfung - Bestellung',
],
OrderDefinition::ENTITY_NAME
);
}
private function createCustomFieldSetIfNotExists(
EntityRepository $customFieldSetRepo,
Context $context,
string $technicalName,
string $fieldName,
array $label,
string $entityName
): void {
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', $technicalName));
$existing = $customFieldSetRepo->search($criteria, $context);
if ($existing->count() > 0) {
return;
}
$customFieldSetRepo->create([
[
'id' => Uuid::randomHex(),
'name' => $technicalName,
'config' => [
'label' => $label,
],
'customFields' => [
[
'name' => $fieldName,
'type' => 'select',
'config' => [
'label' => [
'en-GB' => 'Sanction Validation',
'de-DE' => 'Sanktionsprüfung',
],
'componentName' => 'sw-single-select',
'customFieldType' => 'select',
'options' => ValidationStatus::getOptions(),
],
],
],
'relations' => [
['entityName' => $entityName],
],
],
], $context);
}
}