$categories */ private ?array $categories = null; /** * @throws InvalidArgumentException */ public function __construct( private Dispatcher $dispatcher, ) { parent::__construct(); } /** * @throws ParseException * @throws NoHandlerFoundException */ protected function execute( InputInterface $input, OutputInterface $output ): int { $output->writeln('Importing presets...'); $presets = Yaml::parseFile('database/seeds/presets.yml'); $presets = json_decode(json_encode($presets)); foreach ($presets as $presetJson) { // Check if the category exists, if not, create it $category = $this->findOrCreateCategory($presetJson->category); $output->writeln( sprintf( '• Importing preset %s into category %s', $presetJson->title, $category->getTitle()->value ) ); $cmd = new CreatePresetCommand( $presetJson->type, $presetJson->title ); $cmd ->setDescription($presetJson->description) ->setStatus($presetJson->status) ->setTemplate($presetJson->template) ->setImage($presetJson->image) ->setColor($presetJson->color); $cmd->categoryId = $category->getId(); // $cmd->lock = true; // Built-in presets are locked try { $this->dispatcher->dispatch($cmd); } catch (TemplateExistsException $th) { $output->writeln( sprintf( '• Preset %s already exists', $presetJson->title ) ); } } return Command::SUCCESS; } /** * @throws NoHandlerFoundException */ private function findOrCreateCategory(string $title): CategoryEntity { if (is_null($this->categories)) { /** @property null|Traversable $categories */ $categories = $this->dispatcher->dispatch( new ListCategoriesCommand() ); $this->categories = []; foreach ($categories as $category) { $this->categories[] = $category; } } foreach ($this->categories as $category) { if ($category->getTitle()->value === $title) { return $category; } } $cmd = new CreateCategoryCommand($title); $category = $this->dispatcher->dispatch($cmd); $this->categories[] = $category; return $category; } }