<?php
declare(strict_types=1);
namespace NetInventors\NetiNextOrderAmountHandler;
use NetInventors\NetiNextOrderAmountHandler\Service\CompilerPass;
use NetInventors\NetiNextOrderAmountHandler\Service\PaymentHandler\FreePayment;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class NetiNextOrderAmountHandler extends Plugin
{
public function install(InstallContext $context): void
{
$this->addPaymentMethod($context->getContext());
}
public function uninstall(UninstallContext $context): void
{
$this->setPaymentMethodIsActive(false, $context->getContext());
}
public function activate(ActivateContext $context): void
{
$this->setPaymentMethodIsActive(true, $context->getContext());
parent::activate($context);
}
public function deactivate(DeactivateContext $context): void
{
$this->setPaymentMethodIsActive(false, $context->getContext());
parent::deactivate($context);
}
private function addPaymentMethod(Context $context): void
{
$paymentMethodExists = $this->getPaymentMethodId($context);
if ($paymentMethodExists) {
return;
}
/** @var PluginIdProvider $pluginIdProvider */
$pluginIdProvider = $this->container->get(PluginIdProvider::class);
$pluginId = $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
$examplePaymentData = [
'handlerIdentifier' => FreePayment::class,
'name' => 'Free',
'description' => 'Can only be used when the invoice amount is zero.',
'pluginId' => $pluginId,
'afterOrderEnabled' => false,
];
/** @var EntityRepositoryInterface $paymentRepository */
$paymentRepository = $this->container->get('payment_method.repository');
$paymentRepository->create([ $examplePaymentData ], $context);
}
private function setPaymentMethodIsActive(bool $active, Context $context): void
{
/** @var EntityRepositoryInterface $paymentRepository */
$paymentRepository = $this->container->get('payment_method.repository');
$paymentMethodId = $this->getPaymentMethodId($context);
if ($paymentMethodId) {
$paymentMethod = [
'id' => $paymentMethodId,
'active' => $active,
];
$paymentRepository->update([ $paymentMethod ], $context);
}
}
private function getPaymentMethodId(Context $context): ?string
{
/** @var EntityRepositoryInterface $paymentRepository */
$paymentRepository = $this->container->get('payment_method.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('handlerIdentifier', FreePayment::class));
$paymentIds = $paymentRepository->searchIds($criteria, $context);
return $paymentIds->firstId();
}
public function build(ContainerBuilder $container): void
{
parent::build($container);
$container->addCompilerPass(new CompilerPass());
}
}