<?php declare(strict_types=1);
namespace ExconceptSchufaCheckPlugin;
use ExconceptSchufaCheckPlugin\Setup\InitialCustomFieldsetData;
use ExconceptSchufaCheckPlugin\Setup\InitialRulesetData;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
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\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Uuid\Uuid;
class ExconceptSchufaCheckPlugin extends Plugin
{
private const LOGGER_NAME = 'SchufaCheckLogger';
public function install(InstallContext $installContext): void
{
parent::install($installContext);
/** @STS: CAREFUL: The sequence order matters. The rulesets will need to reference the custom fields so the custom fields go first! */
$this->createCustomFieldSets($installContext);
$this->createCustomRuleSets($installContext);
}
private function createCustomFieldSets(InstallContext $installContext): void
{
/** @var EntityRepositoryInterface $customFieldSetRepo */
$customFieldSetRepo = $this->container->get('custom_field_set.repository');
$installableFieldSets = InitialCustomFieldsetData::getInstallableFieldSets();
foreach ($installableFieldSets as $installableFieldSet) {
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', $installableFieldSet['name']));
$existingFieldSet = $customFieldSetRepo->search($criteria, $installContext->getContext());
if ($existingFieldSet->getTotal() === 0) {
$installableFieldSet['id'] = Uuid::randomHex();
$customFieldSetRepo->create([$installableFieldSet], $installContext->getContext());
self::logMessage("Custom field set '" . $installableFieldSet['name'] . "' created.", Logger::INFO);
} else {
self::logMessage("Custom field set '" . $installableFieldSet['name'] . "' already exists.", Logger::INFO);
}
}
}
private function createCustomRuleSets(InstallContext $installContext): void
{
/** @var EntityRepositoryInterface $ruleRepository */
$ruleRepository = $this->container->get('rule.repository');
$installableRuleSets = InitialRulesetData::getInstallableRuleSets();
foreach ($installableRuleSets as $installableRuleSet) {
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', $installableRuleSet['name']));
$existingRule = $ruleRepository->search($criteria, $installContext->getContext());
if ($existingRule->getTotal() === 0) {
$installableRuleSet['id'] = Uuid::randomHex();
$ruleRepository->create([$installableRuleSet], $installContext->getContext());
self::logMessage("Rule '" . $installableRuleSet['name'] . "' created.", Logger::INFO);
} else {
self::logMessage("Rule '" . $installableRuleSet['name'] . "' already exists.", Logger::INFO);
}
}
}
public static function logMessage(string $string, int $level = Logger::INFO,$customLoggerName = "", array $context=[]): void
{
$logger = new Logger(self::LOGGER_NAME);
$logger->pushHandler(new StreamHandler(self::getLogPath() . DIRECTORY_SEPARATOR . self::getLogFilename($customLoggerName), $level));
$logger->log($level, $string, $context);
}
public static function getLogFilename($customName = ""): string
{
$currentDate = date('Y-m-d');
if($customName === "") {
return self::LOGGER_NAME . '_' . $currentDate . '.log';
}
return self::LOGGER_NAME . '_' . $currentDate . '_' . $customName . '.log';
}
public static function getLogPath(): string
{
return dirname(__FILE__) . DIRECTORY_SEPARATOR
// Project root directory
. ".." . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR
// Log folder
. "var" . DIRECTORY_SEPARATOR . "log";
}
public static function getPluginPath(): string
{
return dirname(__FILE__);
}
}