vendor/crehler/mojebambino-engine/src/Subscriber/PaymentMethodSubscriber.php line 22

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Crehler\MojeBambinoEngine\Subscriber;
  3. use Crehler\MojeBambinoEngine\Util\Lifecycle\ActivateDeactivate;
  4. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  7. use Shopware\Core\System\CustomField\CustomFieldTypes;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class PaymentMethodSubscriber implements EventSubscriberInterface
  10. {
  11.     public static function getSubscribedEvents(): array
  12.     {
  13.         return [
  14.             'payment_method.loaded' => 'onPaymentMethodLoaded'
  15.         ];
  16.     }
  17.     public function onPaymentMethodLoaded(EntityLoadedEvent $event)
  18.     {
  19.         if ($event->getContext()->getScope() !== Context::CRUD_API_SCOPE) {
  20.             return;
  21.         }
  22.         /** @var PaymentMethodEntity $paymentMethod */
  23.         foreach ($event->getEntities() as $paymentMethod) {
  24.             $this->initializationCustomFields(ActivateDeactivate::PAYMENT_METHOD_CUSTOM_FIELDS$paymentMethod);
  25.         }
  26.     }
  27.     private function initializationCustomFields(array $customFieldsPaymentMethodEntity $paymentMethod): void
  28.     {
  29.         $prepareCustomFields = [];
  30.         if ($paymentMethod->getCustomFields() !== null
  31.             || ($paymentMethod->getCustomFields() !== null && isset($paymentMethod->getCustomFields()[$customField['name']]))) {
  32.             return;
  33.         }
  34.         foreach ($customFields as $customField) {
  35.             if ($customField['type'] === CustomFieldTypes::BOOL) {
  36.                 $prepareCustomFields[$customField['name']] = false;
  37.             } else {
  38.                 $prepareCustomFields[$customField['name']] = null;
  39.             }
  40.         }
  41.         $paymentMethod->setCustomFields($prepareCustomFields);
  42.     }
  43. }