<?php declare(strict_types=1);
namespace Swag\AmazonPay;
use Doctrine\DBAL\Connection;
use Shopware\Core\Checkout\Payment\PaymentMethodDefinition;
use Shopware\Core\Content\Rule\RuleDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\Plugin;
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\Context\UpdateContext;
use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
use Shopware\Core\System\Currency\CurrencyDefinition;
use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetDefinition;
use Shopware\Core\System\Salutation\SalutationDefinition;
use Swag\AmazonPay\Installer\CustomFieldsInstaller;
use Swag\AmazonPay\Installer\DatabaseInstaller;
use Swag\AmazonPay\Installer\DefaultSalutationInstaller;
use Swag\AmazonPay\Installer\PaymentMethodInstaller;
use Swag\AmazonPay\Installer\RuleInstaller;
use Swag\AmazonPay\Uninstaller\ConfigurationUninstaller;
use Swag\AmazonPay\Uninstaller\LogUninstaller;
use Swag\AmazonPay\Util\SwagAmazonPayClassLoader;
use Swag\AmazonPay\Util\Util;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
if (\file_exists(__DIR__ . '/../vendor/autoload.php')) {
(new SwagAmazonPayClassLoader())->register();
}
class SwagAmazonPay extends Plugin
{
public function install(InstallContext $installContext): void
{
/** @var PluginIdProvider $pluginIdProvider */
$pluginIdProvider = $this->container->get(PluginIdProvider::class);
$paymentMethodRepository = $this->getRepository($this->container, PaymentMethodDefinition::ENTITY_NAME);
$customFieldSetRepository = $this->getRepository($this->container, CustomFieldSetDefinition::ENTITY_NAME);
$salutationRepository = $this->getRepository($this->container, SalutationDefinition::ENTITY_NAME);
$ruleRepository = $this->getRepository($this->container, RuleDefinition::ENTITY_NAME);
$currencyRepository = $this->getRepository($this->container, CurrencyDefinition::ENTITY_NAME);
(new PaymentMethodInstaller($paymentMethodRepository, $pluginIdProvider))->install($installContext);
(new CustomFieldsInstaller($customFieldSetRepository))->install($installContext);
(new DefaultSalutationInstaller($salutationRepository))->install($installContext);
(new RuleInstaller($ruleRepository, $currencyRepository, $paymentMethodRepository))->install($installContext);
}
public function update(UpdateContext $updateContext): void
{
$ruleRepository = $this->getRepository($this->container, RuleDefinition::ENTITY_NAME);
$currencyRepository = $this->getRepository($this->container, CurrencyDefinition::ENTITY_NAME);
$paymentMethodRepository = $this->getRepository($this->container, PaymentMethodDefinition::ENTITY_NAME);
(new RuleInstaller($ruleRepository, $currencyRepository, $paymentMethodRepository))->update($updateContext);
}
public function deactivate(DeactivateContext $deactivateContext): void
{
/** @var PluginIdProvider $pluginIdProvider */
$pluginIdProvider = $this->container->get(PluginIdProvider::class);
$paymentMethodRepository = $this->getRepository($this->container, PaymentMethodDefinition::ENTITY_NAME);
$customFieldSetRepository = $this->getRepository($this->container, CustomFieldSetDefinition::ENTITY_NAME);
(new PaymentMethodInstaller($paymentMethodRepository, $pluginIdProvider))->deactivate($deactivateContext);
(new CustomFieldsInstaller($customFieldSetRepository))->deactivate($deactivateContext);
}
public function uninstall(UninstallContext $uninstallContext): void
{
/** @var Connection $connection */
$connection = $this->container->get(Connection::class);
/** @var PluginIdProvider $pluginIdProvider */
$pluginIdProvider = $this->container->get(PluginIdProvider::class);
$paymentMethodRepository = $this->getRepository($this->container, PaymentMethodDefinition::ENTITY_NAME);
$customFieldSetRepository = $this->getRepository($this->container, CustomFieldSetDefinition::ENTITY_NAME);
$ruleRepository = $this->getRepository($this->container, RuleDefinition::ENTITY_NAME);
$currencyRepository = $this->getRepository($this->container, CurrencyDefinition::ENTITY_NAME);
(new PaymentMethodInstaller($paymentMethodRepository, $pluginIdProvider))->uninstall($uninstallContext);
(new CustomFieldsInstaller($customFieldSetRepository))->uninstall($uninstallContext);
(new DatabaseInstaller($connection))->uninstall($uninstallContext);
(new RuleInstaller($ruleRepository, $currencyRepository, $paymentMethodRepository))->uninstall($uninstallContext);
(new ConfigurationUninstaller($connection))->uninstall($uninstallContext);
$logsDir = $this->container->getParameter('kernel.logs_dir');
(new LogUninstaller($logsDir))->uninstall($uninstallContext);
}
public function activate(ActivateContext $activateContext): void
{
/** @var PluginIdProvider $pluginIdProvider */
$pluginIdProvider = $this->container->get(PluginIdProvider::class);
$paymentRepository = $this->getRepository($this->container, PaymentMethodDefinition::ENTITY_NAME);
$customFieldSetRepository = $this->getRepository($this->container, CustomFieldSetDefinition::ENTITY_NAME);
(new PaymentMethodInstaller($paymentRepository, $pluginIdProvider))->activate($activateContext);
(new CustomFieldsInstaller($customFieldSetRepository))->activate($activateContext);
}
public function enrichPrivileges(): array
{
return [
'order.viewer' => [
'swag_amazon_pay_payment_notification:read',
],
];
}
private function getRepository(ContainerInterface $container, string $entityName) //SW65 add return type : EntityRepository
{
$repository = $container->get(\sprintf('%s.repository', $entityName), ContainerInterface::NULL_ON_INVALID_REFERENCE);
if (interface_exists(Util::ENTITY_REPOSITORY_INTERFACE)) {
//SW65 may remove this if statement
if (!$repository instanceof EntityRepositoryInterface) {
throw new ServiceNotFoundException(\sprintf('%s.repository', $entityName));
}
} else {
if (!$repository instanceof EntityRepository) {
throw new ServiceNotFoundException(\sprintf('%s.repository', $entityName));
}
}
return $repository;
}
}