custom/plugins/MoptWorldline/src/MoptWorldline.php line 23

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * @author Mediaopt GmbH
  5.  * @package MoptWorldline
  6.  */
  7. namespace MoptWorldline;
  8. use MoptWorldline\Service\Payment;
  9. use MoptWorldline\Service\PaymentMethodHelper;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\Plugin;
  12. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  13. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  14. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  15. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  16. use MoptWorldline\Service\CustomField;
  17. use Shopware\Core\Framework\Context;
  18. use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
  19. class MoptWorldline extends Plugin
  20. {
  21.     const PLUGIN_NAME = 'MoptWorldline';
  22.     const PLUGIN_VERSION = '1.8.5';
  23.     /**
  24.      * @param InstallContext $installContext
  25.      */
  26.     public function install(InstallContext $installContext): void
  27.     {
  28.         parent::install($installContext);
  29.         $customField = new CustomField($this->container);
  30.         $customField->addCustomFields($installContext);
  31.         /** @var EntityRepositoryInterface $paymentMethodRep */
  32.         $paymentMethodRep =$this->container->get('payment_method.repository');
  33.         /** @var EntityRepositoryInterface $salesChannelPaymentMethodRep */
  34.         $salesChannelPaymentMethodRep = $this->container->get('sales_channel_payment_method.repository');
  35.         /** @var PluginIdProvider $pluginIdProvider */
  36.         $pluginIdProvider = $this->container->get(PluginIdProvider::class);
  37.         /** @var EntityRepositoryInterface $salesChannelRep */
  38.         $salesChannelRep = $this->container->get('sales_channel.repository');
  39.         foreach (Payment::METHODS_LIST as $method) {
  40.             PaymentMethodHelper::addPaymentMethod(
  41.                 $paymentMethodRep,
  42.                 $salesChannelPaymentMethodRep,
  43.                 $pluginIdProvider,
  44.                 $installContext->getContext(),
  45.                 $method,
  46.                 null,
  47.                 $salesChannelRep
  48.             );
  49.         }
  50.     }
  51.     /**
  52.      * @param UninstallContext $uninstallContext
  53.      */
  54.     public function uninstall(UninstallContext $uninstallContext): void
  55.     {
  56.         parent::uninstall($uninstallContext);
  57.         $this->setPaymentMethodsStatus(false, $uninstallContext->getContext());
  58.     }
  59.     /**
  60.      * @param ActivateContext $activateContext
  61.      */
  62.     public function activate(ActivateContext $activateContext): void
  63.     {
  64.         parent::activate($activateContext);
  65.         $this->setPaymentMethodsStatus(true, $activateContext->getContext());
  66.     }
  67.     /**
  68.      * @param DeactivateContext $deactivateContext
  69.      */
  70.     public function deactivate(DeactivateContext $deactivateContext): void
  71.     {
  72.         parent::deactivate($deactivateContext);
  73.         $this->setPaymentMethodsStatus(false, $deactivateContext->getContext());
  74.     }
  75.     /**
  76.      * @param bool $status
  77.      * @param Context $context
  78.      * @return void
  79.      */
  80.     private function setPaymentMethodsStatus(bool $status, Context $context)
  81.     {
  82.         /** @var EntityRepositoryInterface $paymentMethodRepository */
  83.         $paymentMethodRepository = $this->container->get('payment_method.repository');
  84.         foreach (Payment::METHODS_LIST as $method) {
  85.             PaymentMethodHelper::setPaymentMethodStatus($paymentMethodRepository, $status, $context, $method['id']);
  86.         }
  87.     }
  88. }
  89. if (file_exists(__DIR__ . '/vendor/autoload.php')) {
  90.     require_once __DIR__ . '/vendor/autoload.php';
  91. }