<?php
declare(strict_types=1);
/*
* (c) shopware AG <info@shopware.com>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Swag\AmazonPay;
use Doctrine\DBAL\Connection;
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 Swag\AmazonPay\Installer\CustomFieldsInstaller;
use Swag\AmazonPay\Installer\DatabaseInstaller;
use Swag\AmazonPay\Installer\DefaultSalutationInstaller;
use Swag\AmazonPay\Installer\EventActionInstaller;
use Swag\AmazonPay\Installer\MailTemplateInstaller;
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 Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
if (\file_exists(__DIR__ . '/../vendor/autoload.php')) {
(new SwagAmazonPayClassLoader())->register();
}
class SwagAmazonPay extends Plugin
{
public function install(InstallContext $installContext): void
{
/** @var EntityRepositoryInterface $paymentMethodRepository */
$paymentMethodRepository = $this->container->get('payment_method.repository');
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
/** @var EntityRepositoryInterface $salutationRepository */
$salutationRepository = $this->container->get('salutation.repository');
/** @var EntityRepositoryInterface $ruleRepository */
$ruleRepository = $this->container->get('rule.repository');
/** @var EntityRepositoryInterface $currencyRepository */
$currencyRepository = $this->container->get('currency.repository');
/** @var PluginIdProvider $pluginIdProvider */
$pluginIdProvider = $this->container->get(PluginIdProvider::class);
/** @var EntityRepositoryInterface $mailTemplateRepository */
$mailTemplateRepository = $this->container->get('mail_template.repository');
/** @var EntityRepositoryInterface $mailTemplateTypeRepository */
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
/** @var EntityRepositoryInterface $eventActionRepository */
$eventActionRepository = $this->container->get('event_action.repository');
(new PaymentMethodInstaller($paymentMethodRepository, $pluginIdProvider))->install($installContext);
(new CustomFieldsInstaller($customFieldSetRepository))->install($installContext);
(new DefaultSalutationInstaller($salutationRepository))->install($installContext);
(new RuleInstaller($ruleRepository, $currencyRepository, $paymentMethodRepository))->install($installContext);
(new MailTemplateInstaller($mailTemplateRepository, $mailTemplateTypeRepository))->install($installContext);
(new EventActionInstaller($eventActionRepository, $mailTemplateRepository))->install($installContext);
}
public function update(UpdateContext $updateContext): void
{
/** @var EntityRepositoryInterface $ruleRepository */
$ruleRepository = $this->container->get('rule.repository');
/** @var EntityRepositoryInterface $currencyRepository */
$currencyRepository = $this->container->get('currency.repository');
/** @var EntityRepositoryInterface $paymentMethodRepository */
$paymentMethodRepository = $this->container->get('payment_method.repository');
(new RuleInstaller($ruleRepository, $currencyRepository, $paymentMethodRepository))->update($updateContext);
}
public function deactivate(DeactivateContext $deactivateContext): void
{
/** @var EntityRepositoryInterface $paymentRepository */
$paymentRepository = $this->container->get('payment_method.repository');
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
/** @var PluginIdProvider $pluginIdProvider */
$pluginIdProvider = $this->container->get(PluginIdProvider::class);
(new PaymentMethodInstaller($paymentRepository, $pluginIdProvider))->deactivate($deactivateContext);
(new CustomFieldsInstaller($customFieldSetRepository))->deactivate($deactivateContext);
}
public function uninstall(UninstallContext $uninstallContext): void
{
/** @var EntityRepositoryInterface $paymentMethodRepository */
$paymentMethodRepository = $this->container->get('payment_method.repository');
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
/** @var PluginIdProvider $pluginIdProvider */
$pluginIdProvider = $this->container->get(PluginIdProvider::class);
/** @var Connection $connection */
$connection = $this->container->get(Connection::class);
/** @var EntityRepositoryInterface $ruleRepository */
$ruleRepository = $this->container->get('rule.repository');
/** @var EntityRepositoryInterface $currencyRepository */
$currencyRepository = $this->container->get('currency.repository');
/** @var EntityRepositoryInterface $mailTemplateRepository */
$mailTemplateRepository = $this->container->get('mail_template.repository');
/** @var EntityRepositoryInterface $mailTemplateTypeRepository */
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
/** @var EntityRepositoryInterface $eventActionRepository */
$eventActionRepository = $this->container->get('event_action.repository');
(new PaymentMethodInstaller($paymentMethodRepository, $pluginIdProvider))->uninstall($uninstallContext);
(new CustomFieldsInstaller($customFieldSetRepository))->uninstall($uninstallContext);
(new DatabaseInstaller($connection))->uninstall($uninstallContext);
(new RuleInstaller($ruleRepository, $currencyRepository, $paymentMethodRepository))->uninstall($uninstallContext);
(new MailTemplateInstaller($mailTemplateRepository, $mailTemplateTypeRepository))->uninstall($uninstallContext);
(new EventActionInstaller($eventActionRepository, $mailTemplateRepository))->uninstall($uninstallContext);
(new ConfigurationUninstaller($connection))->uninstall($uninstallContext);
$logsDir = $this->container->getParameter('kernel.logs_dir');
if (!\is_string($logsDir)) {
return;
}
(new LogUninstaller($logsDir))->uninstall($uninstallContext);
}
public function activate(ActivateContext $activateContext): void
{
/** @var EntityRepositoryInterface $paymentRepository */
$paymentRepository = $this->container->get('payment_method.repository');
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
/** @var PluginIdProvider $pluginIdProvider */
$pluginIdProvider = $this->container->get(PluginIdProvider::class);
(new PaymentMethodInstaller($paymentRepository, $pluginIdProvider))->activate($activateContext);
(new CustomFieldsInstaller($customFieldSetRepository))->activate($activateContext);
}
public function build(ContainerBuilder $container): void
{
parent::build($container);
$shopwareVersion = $container->getParameter('kernel.shopware_version');
if (!\is_string($shopwareVersion)) {
throw new \RuntimeException('SwagAmazonPay needs the kernel.shopware_version parameter to determine which services to load.');
}
// Shopware version specific service loading
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/DependencyInjection/Dynamic'));
if (\version_compare($shopwareVersion, '6.4.0', '<')) {
$loader->load('SW6.3.xml');
return;
}
$loader->load('SW6.4.xml');
}
public function enrichPrivileges(): array
{
return [
'order.viewer' => [
'swag_amazon_pay_payment_notification:read',
],
];
}
}