<?php
namespace MoorlInterrupter;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
class MoorlInterrupter extends Plugin
{
public const NAME = 'MoorlInterrupter';
public const DATA_CREATED_AT = '2021-08-22 00:00:00.000';
public const PLUGIN_TABLES = [
'moorl_interrupter',
'moorl_interrupter_translation',
'moorl_interrupter_category'
];
public const SHOPWARE_TABLES = [
'custom_field_set',
'unit',
'shipping_method',
'shipping_method_translation',
'cms_page',
'cms_page_translation',
'cms_section',
'cms_block',
'category',
'category_translation',
'product',
'product_translation',
'product_category',
'product_visibility',
'theme',
'theme_sales_channel',
'sales_channel'
];
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
$this->removePluginData();
$this->dropTables();
}
private function removePluginData(): void
{
$connection = $this->container->get(Connection::class);
foreach (array_reverse(self::SHOPWARE_TABLES) as $table) {
$sql = sprintf("SET FOREIGN_KEY_CHECKS=0; DELETE FROM `%s` WHERE `created_at` = '%s';", $table, self::DATA_CREATED_AT);
try {
$connection->executeUpdate($sql);
} catch (\Exception $exception) {
continue;
}
}
}
private function dropTables(): void
{
$connection = $this->container->get(Connection::class);
foreach (self::PLUGIN_TABLES as $table) {
$sql = sprintf('SET FOREIGN_KEY_CHECKS=0; DROP TABLE IF EXISTS `%s`;', $table);
$connection->executeUpdate($sql);
}
}
}