<?php declare(strict_types=1);
namespace Dmits\InfoFlag;
use Dmits\InfoFlag\Setting\Settings;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
use Shopware\Core\System\SystemConfig\SystemConfigService;
class DmitsInfoFlag extends Plugin
{
public function install(InstallContext $installContext): void
{
if ($this->validSettingsNotExists()) {
/** @var SystemConfigService $systemConfigService */
$systemConfigService = $this->container->get(SystemConfigService::class);
foreach (Settings::DEFAULT_VALUES as $key => $value) {
$systemConfigService->set($key, $value);
}
}
parent::install($installContext);
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
/** @var EntityRepositoryInterface $systemConfigRepository */
$systemConfigRepository = $this->container->get('system_config.repository');
$criteria = (new Criteria())
->addFilter(new ContainsFilter('configurationKey', Settings::SYSTEM_CONFIG_DOMAIN));
$idSearchResult = $systemConfigRepository->searchIds($criteria, $uninstallContext->getContext());
$ids = \array_map(static function ($id) {
return ['id' => $id];
}, $idSearchResult->getIds());
if ($ids === []) {
return;
}
$systemConfigRepository->delete($ids, $uninstallContext->getContext());
// Ab Shopware 6.3.5
// $systemConfigService->deletePluginConfiguration(DmitsInfoFlag::class);
}
private function validSettingsNotExists(): bool
{
$systemConfigService = $this->container->get(SystemConfigService::class);
try {
$getpositon=$systemConfigService->getString('DmitsInfoFlag.config.position');
if (($getpositon ?? '') === '') {
return true;
}
} catch (Exception $e) {
return true;
}
return false;
}
}