<?php declare(strict_types=1);
namespace Cbax\ModulOrderImportGalaxus;
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 Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
use Doctrine\DBAL\Connection;
use Cbax\ModulExternalOrder\Core\Exception\PluginCustomError;
use Cbax\ModulExternalOrder\Controller\ApiController;
use Cbax\ModulExternalOrder\Bootstrap\Database as externalOrderDatabase;
use Cbax\ModulExternalOrder\Components\ExternalOrderHelper;
use Cbax\ModulExternalOrder\Bootstrap\Assets as externalOrderAssets;
use Cbax\ModulExternalOrder\Components\DatabaseHelper;
use Cbax\ModulOrderImportGalaxus\Bootstrap\Database;
use Cbax\ModulOrderImportGalaxus\Bootstrap\Updater;
class CbaxModulOrderImportGalaxus extends Plugin
{
private function getServices() {
$services = array();
// check if external Order is installed
try {
$this->container->get(ApiController::class);
} catch (\Exception $e) {
$locale = null;
if ($locale === null) {
$locale = 'de-DE';
}
$message = 'cbax-galaxus.exceptionActivateExternalOrdersFirst';
$cbaxCatalog = $this->container->get('translator')->getCatalogue($locale)->all('cbax');
if (!empty($cbaxCatalog[$message])) {
$message = $cbaxCatalog[$message];
}
throw new PluginCustomError($message);
}
/* Standard Services */
$services['systemConfigService'] = $this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService');
$services['systemConfigRepository'] = $this->container->get('system_config.repository');
$services['connectionService'] = $this->container->get(Connection::class);
$services['databaseHelper'] = new DatabaseHelper();
/* Services der Externen Bestellverwaltung */
$services['externalOrderDatabase'] = $this->container->get(externalOrderDatabase::class);
$services['externalOrderHelper'] = $this->container->get(externalOrderHelper::class);
$services['externalOrderAssets'] = $this->container->get(externalOrderAssets::class);
/* Connector spezifische Services */
$services['stateMachineRepository'] = $this->container->get('state_machine.repository');
$services['stateMachineStateRepository'] = $this->container->get('state_machine_state.repository');
return $services;
}
public function install(InstallContext $context): void
{
parent::install($context);
}
public function update(UpdateContext $context): void
{
$services = $this->getServices();
$db = new Database();
$db->patch($services, $context);
$updater = new Updater();
$updater->update($services, $context);
parent::update($context);
}
public function postUpdate(UpdateContext $context) : void {
$services = $this->getServices();
$updater = new Updater();
$updater->afterUpdateSteps($services, $context);
}
public function uninstall(UninstallContext $context): void
{
if ($context->keepUserData()) {
parent::uninstall($context);
return;
}
$services = $this->getServices();
// Datenbank Tabellen löschen
$db = new Database();
$db->removeDatabaseTables($services, $context->getContext());
$this->removePluginConfig($services, $context->getContext());
parent::uninstall($context);
}
public function activate(ActivateContext $context): void
{
$services = $this->getServices();
$db = new Database();
$db->patch($services, $context);
parent::activate($context);
}
public function deactivate(DeactivateContext $context): void
{
parent::deactivate($context);
}
private function removePluginConfig($services, $context)
{
$systemConfigRepository = $services['systemConfigRepository'];
$criteria = new Criteria();
$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);
}
}