<?php declare(strict_types=1);
namespace ProxaPaymentReminder;
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\ContainsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use ProxaPaymentReminder\Manager\CustomFieldManager;
use ProxaPaymentReminder\Manager\MailTemplateManager;
/**
* Class ProxaPaymentReminder
* @package ProxaPaymentReminder
*/
class ProxaPaymentReminder extends Plugin
{
public const REMAINDER_MAILS_COUNT = 2;
/**
* {@inheritdoc}
*/
public function build(ContainerBuilder $container): void
{
parent::build($container);
}
/**
* @param InstallContext $installContext
*/
public function install(InstallContext $installContext): void
{
parent::install($installContext);
($this->getCustomFieldManager($installContext->getContext()))->install();
($this->getMailTemplateManager($installContext->getContext()))->install();
}
/**
* @param UpdateContext $updateContext
*/
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext);
($this->getCustomFieldManager($updateContext->getContext()))->update();
($this->getMailTemplateManager($updateContext->getContext()))->update();
}
/**
* @param UninstallContext $uninstallContext
*/
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
$this->removeConfiguration($uninstallContext->getContext());
($this->getCustomFieldManager($uninstallContext->getContext()))->uninstall();
($this->getMailTemplateManager($uninstallContext->getContext()))->uninstall();
}
/**
* @param Context $context
*/
private function removeConfiguration(Context $context): void
{
/** @var EntityRepositoryInterface $systemConfigRepository */
$systemConfigRepository = $this->container->get('system_config.repository');
$criteria = (new Criteria())->addFilter(new ContainsFilter('configurationKey', $this->getName() . '.config.'));
$idSearchResult = $systemConfigRepository->searchIds($criteria, $context);
$ids = array_map(static function ($id) {
return ['id' => $id];
}, $idSearchResult->getIds());
if ($ids === []) {
return;
}
$systemConfigRepository->delete($ids, $context);
}
/**
* @param Context $context
* @return CustomFieldManager
*/
private function getCustomFieldManager(Context $context): CustomFieldManager
{
return new CustomFieldManager(
$context,
$this->container->get('custom_field_set.repository'),
$this->container->get('snippet.repository')
);
}
/**
* @param Context $context
* @return MailTemplateManager
*/
private function getMailTemplateManager(Context $context): MailTemplateManager
{
return new MailTemplateManager(
$this->container,
$context,
$this->container->get('mail_template.repository'),
$this->container->get('mail_template_type.repository'),
$this->container->get('event_action.repository')
);
}
}