custom/plugins/ExconceptSchufaCheckPlugin/src/ExconceptSchufaCheckPlugin.php line 16

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ExconceptSchufaCheckPlugin;
  3. use ExconceptSchufaCheckPlugin\Setup\InitialCustomFieldsetData;
  4. use ExconceptSchufaCheckPlugin\Setup\InitialRulesetData;
  5. use Monolog\Handler\StreamHandler;
  6. use Monolog\Logger;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\Plugin;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Uuid\Uuid;
  13. class ExconceptSchufaCheckPlugin extends Plugin
  14. {
  15.     private const LOGGER_NAME 'SchufaCheckLogger';
  16.     public function install(InstallContext $installContext): void
  17.     {
  18.         parent::install($installContext);
  19.         /** @STS: CAREFUL: The sequence order matters. The rulesets will need to reference the custom fields so the custom fields go first! */
  20.         $this->createCustomFieldSets($installContext);
  21.         $this->createCustomRuleSets($installContext);
  22.     }
  23.     private function createCustomFieldSets(InstallContext $installContext): void
  24.     {
  25.         /** @var EntityRepositoryInterface $customFieldSetRepo */
  26.         $customFieldSetRepo $this->container->get('custom_field_set.repository');
  27.         $installableFieldSets InitialCustomFieldsetData::getInstallableFieldSets();
  28.         foreach ($installableFieldSets as $installableFieldSet) {
  29.             $criteria = new Criteria();
  30.             $criteria->addFilter(new EqualsFilter('name'$installableFieldSet['name']));
  31.             $existingFieldSet $customFieldSetRepo->search($criteria$installContext->getContext());
  32.             if ($existingFieldSet->getTotal() === 0) {
  33.                 $installableFieldSet['id'] = Uuid::randomHex();
  34.                 $customFieldSetRepo->create([$installableFieldSet], $installContext->getContext());
  35.                 self::logMessage("Custom field set '" $installableFieldSet['name'] . "' created."Logger::INFO);
  36.             } else {
  37.                 self::logMessage("Custom field set '" $installableFieldSet['name'] . "' already exists."Logger::INFO);
  38.             }
  39.         }
  40.     }
  41.     private function createCustomRuleSets(InstallContext $installContext): void
  42.     {
  43.         /** @var EntityRepositoryInterface $ruleRepository */
  44.         $ruleRepository $this->container->get('rule.repository');
  45.         $installableRuleSets InitialRulesetData::getInstallableRuleSets();
  46.         foreach ($installableRuleSets as $installableRuleSet) {
  47.             $criteria = new Criteria();
  48.             $criteria->addFilter(new EqualsFilter('name'$installableRuleSet['name']));
  49.             $existingRule $ruleRepository->search($criteria$installContext->getContext());
  50.             if ($existingRule->getTotal() === 0) {
  51.                 $installableRuleSet['id'] = Uuid::randomHex();
  52.                 $ruleRepository->create([$installableRuleSet], $installContext->getContext());
  53.                 self::logMessage("Rule '" $installableRuleSet['name'] . "' created."Logger::INFO);
  54.             } else {
  55.                 self::logMessage("Rule '" $installableRuleSet['name'] . "' already exists."Logger::INFO);
  56.             }
  57.         }
  58.     }
  59.     public static function logMessage(string $stringint $level Logger::INFO,$customLoggerName "", array $context=[]): void
  60.     {
  61.         $logger = new Logger(self::LOGGER_NAME);
  62.         $logger->pushHandler(new StreamHandler(self::getLogPath() . DIRECTORY_SEPARATOR self::getLogFilename($customLoggerName), $level));
  63.         $logger->log($level$string$context);
  64.     }
  65.     public static function getLogFilename($customName ""): string
  66.     {
  67.         $currentDate date('Y-m-d');
  68.         if($customName === "") {
  69.             return self::LOGGER_NAME '_' $currentDate '.log';
  70.         }
  71.         return self::LOGGER_NAME '_' $currentDate '_' $customName '.log';
  72.     }
  73.     public static function getLogPath(): string
  74.     {
  75.         return dirname(__FILE__) . DIRECTORY_SEPARATOR
  76.             // Project root directory
  77.             ".." DIRECTORY_SEPARATOR ".." DIRECTORY_SEPARATOR ".." DIRECTORY_SEPARATOR ".." DIRECTORY_SEPARATOR
  78.             // Log folder
  79.             "var" DIRECTORY_SEPARATOR "log";
  80.     }
  81.     public static function getPluginPath(): string
  82.     {
  83.         return dirname(__FILE__);
  84.     }
  85. }