custom/plugins/CioBudget/src/CioBudget.php line 18

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace CioBudget;
  3. use CioBudget\PaymentHandler\BudgetPayment;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  10. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  14. use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
  15. class CioBudget extends Plugin
  16. {
  17.     public function install(InstallContext $installContext): void
  18.     {
  19.         $this->addPaymentMethod($installContext->getContext());
  20.     }
  21.     public function update(UpdateContext $updateContext): void
  22.     {
  23.         $this->addPaymentMethod($updateContext->getContext());
  24.     }
  25.     public function uninstall(UninstallContext $uninstallContext): void
  26.     {
  27.         $this->setPaymentMethodIsActive(false$uninstallContext->getContext());
  28.     }
  29.     public function activate(ActivateContext $context): void
  30.     {
  31.         $this->setPaymentMethodIsActive(true$context->getContext());
  32.         parent::activate($context);
  33.     }
  34.     public function deactivate(DeactivateContext $context): void
  35.     {
  36.         $this->setPaymentMethodIsActive(false$context->getContext());
  37.         parent::deactivate($context);
  38.     }
  39.     private function addPaymentMethod(Context $context): void
  40.     {
  41.         $paymentMethodExists $this->getPaymentMethodId();
  42.         // Payment method exists already, no need to continue here
  43.         if ($paymentMethodExists) {
  44.             return;
  45.         }
  46.         /** @var PluginIdProvider $pluginIdProvider */
  47.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  48.         $pluginId $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
  49.         $examplePaymentData = [
  50.             // payment handler will be selected by the identifier
  51.             'handlerIdentifier' => BudgetPayment::class,
  52.             'name' => 'Budget',
  53.             'description' => 'Bezahlen mit Budget',
  54.             'pluginId' => $pluginId,
  55.         ];
  56.         /** @var EntityRepositoryInterface $paymentRepository */
  57.         $paymentRepository $this->container->get('payment_method.repository');
  58.         $paymentRepository->create([$examplePaymentData], $context);
  59.     }
  60.     private function setPaymentMethodIsActive(bool $activeContext $context): void
  61.     {
  62.         /** @var EntityRepositoryInterface $paymentRepository */
  63.         $paymentRepository $this->container->get('payment_method.repository');
  64.         $paymentMethodId $this->getPaymentMethodId();
  65.         // Payment does not even exist, so nothing to (de-)activate here
  66.         if (!$paymentMethodId) {
  67.             return;
  68.         }
  69.         $paymentMethod = [
  70.             'id' => $paymentMethodId,
  71.             'active' => $active,
  72.         ];
  73.         $paymentRepository->update([$paymentMethod], $context);
  74.     }
  75.     private function getPaymentMethodId(): ?string
  76.     {
  77.         /** @var EntityRepositoryInterface $paymentRepository */
  78.         $paymentRepository $this->container->get('payment_method.repository');
  79.         // Fetch ID for update
  80.         $paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('handlerIdentifier'BudgetPayment::class));
  81.         return $paymentRepository->searchIds($paymentCriteriaContext::createDefaultContext())->firstId();
  82.     }
  83. }