<?php
declare(strict_types=1);
namespace Onedrop\News;
use Doctrine\DBAL\Connection;
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\DataAbstractionLayer\Search\Filter\MultiFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
/**
* @SuppressWarnings(PHPMD.CamelCaseClassName)
*/
class odsNews extends Plugin
{
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
/* Delete all categories with the type "news" */
$this->deleteNewsCategory();
/* Delete cms block with type news-full-listing & news-short-listing */
$this->deleteCmsBlocks();
/* Delete cms slots with type news-full-listing & news-short-listing */
$this->deleteCmsSlots();
/* Drop the database tables */
$this->dropDatabaseTable();
}
protected function dropDatabaseTable(): void
{
/** @var Connection $connection */
$connection = $this->container->get(Connection::class);
$connection->executeStatement('SET FOREIGN_KEY_CHECKS=0;');
$connection->executeStatement('DROP TABLE IF EXISTS `ods_news_manufacturer`');
$connection->executeStatement('DROP TABLE IF EXISTS `ods_news`');
$connection->executeStatement('DROP TABLE IF EXISTS `ods_news_translation`');
$connection->executeStatement('SET FOREIGN_KEY_CHECKS=1;');
}
private function deleteNewsCategory(): void
{
/** @var EntityRepositoryInterface $categoryRepository */
$categoryRepository = $this->container->get('category.repository');
$criteria = (new Criteria())
->addFilter(new EqualsFilter('category.type', 'news'));
$idSearchResult = $categoryRepository->searchIds($criteria, Context::createDefaultContext());
if (empty($idSearchResult->getIds())) {
return;
}
$categoryIds = \array_map(static function ($categoryId) {
return ['id' => $categoryId];
}, $idSearchResult->getIds());
$categoryRepository->delete($categoryIds, Context::createDefaultContext());
}
private function deleteCmsBlocks(): void
{
$cmsBlockRepository = $this->container->get('cms_block.repository');
$criteria = (new Criteria())
->addFilter(
new MultiFilter(
MultiFilter::CONNECTION_OR,
[
new EqualsFilter('cms_block.type', 'news-full-listing'),
new EqualsFilter('cms_block.type', 'news-short-listing'),
]
)
);
$idSearchResult = $cmsBlockRepository->searchIds($criteria, Context::createDefaultContext());
if (empty($idSearchResult->getIds())) {
return;
}
$blockIds = \array_map(static function ($blockId) {
return ['id' => $blockId];
}, $idSearchResult->getIds());
$cmsBlockRepository->delete($blockIds, Context::createDefaultContext());
}
private function deleteCmsSlots(): void
{
$cmsSlotRepository = $this->container->get('cms_slot.repository');
$criteria = (new Criteria())
->addFilter(
new MultiFilter(
MultiFilter::CONNECTION_OR,
[
new EqualsFilter('cms_slot.type', 'short-listing-element'),
new EqualsFilter('cms_slot.type', 'full-listing-element'),
]
)
);
$idSearchResult = $cmsSlotRepository->searchIds($criteria, Context::createDefaultContext());
if (empty($idSearchResult->getIds())) {
return;
}
$slotIds = \array_map(static function ($slotId) {
return ['id' => $slotId];
}, $idSearchResult->getIds());
$cmsSlotRepository->delete($slotIds, Context::createDefaultContext());
}
}