File size: 3,852 Bytes
cf14c12 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | <?php
declare(strict_types=1);
namespace Presentation\Commands;
use Category\Application\Commands\CreateCategoryCommand;
use Category\Application\Commands\ListCategoriesCommand;
use Category\Domain\Entities\CategoryEntity;
use Preset\Application\Commands\CreatePresetCommand;
use Preset\Domain\Exceptions\TemplateExistsException;
use Shared\Infrastructure\CommandBus\Dispatcher;
use Shared\Infrastructure\CommandBus\Exception\NoHandlerFoundException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
use Traversable;
#[AsCommand(name: 'app:import:presets')]
class ImportPresetsCommand extends Command
{
/** @var null|array<int,CategoryEntity> $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(
'<info>•</info> Importing preset <options=bold>%s</> into category <options=bold>%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(
'<comment>•</comment> Preset <options=bold>%s</> already exists',
$presetJson->title
)
);
}
}
return Command::SUCCESS;
}
/**
* @throws NoHandlerFoundException
*/
private function findOrCreateCategory(string $title): CategoryEntity
{
if (is_null($this->categories)) {
/** @property null|Traversable<int,CategoryEntity> $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;
}
}
|