Skip to content

Commit 080c6c4

Browse files
committed
4565: Fixed template commands
1 parent 633b227 commit 080c6c4

File tree

4 files changed

+61
-40
lines changed

4 files changed

+61
-40
lines changed

src/Command/TemplatesInstallCommand.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace App\Command;
66

7+
use App\Model\TemplateData;
78
use App\Service\TemplateService;
89
use Doctrine\ORM\EntityManagerInterface;
910
use Symfony\Component\Console\Attribute\AsCommand;
@@ -41,10 +42,8 @@ final protected function execute(InputInterface $input, OutputInterface $output)
4142
$templates = $this->templateService->getAllTemplates();
4243

4344
if ($all) {
44-
foreach ($templates as $templateArray) {
45-
if (!$templateArray['installed']) {
46-
$this->templateService->installTemplate($templateArray);
47-
}
45+
foreach ($templates as $templateToInstall) {
46+
$this->templateService->installTemplate($templateToInstall);
4847
}
4948

5049
$io->success("Installed all available templates");
@@ -58,24 +57,20 @@ final protected function execute(InputInterface $input, OutputInterface $output)
5857
return Command::INVALID;
5958
}
6059

61-
$templateToInstall = array_find($templates, function (array $template) use ($templateUlid): bool {
62-
return $template['id'] === $templateUlid;
60+
$templatesFound = array_find($templates, function (TemplateData $templateData) use ($templateUlid): bool {
61+
return $templateData->id === $templateUlid;
6362
});
6463

65-
if ($templateToInstall !== null) {
66-
if ($templateToInstall['installed']) {
67-
$io->warning("Template ULID already installed");
68-
return Command::INVALID;
69-
} else {
70-
$this->templateService->installTemplate($templateToInstall);
71-
$io->success("Template " .$templateToInstall['title'] . " installed");
72-
}
73-
} else {
74-
$io->warning("Template files not found.");
75-
return Command::INVALID;
64+
if (count($templatesFound) !== 1) {
65+
$io->error("Template not found.");
66+
return Command::FAILURE;
7667
}
7768

69+
$templateToInstall = $templatesFound[0];
70+
71+
$this->templateService->installTemplate($templateToInstall);
72+
$io->success("Template " .$templateToInstall->title . " installed");
73+
7874
return Command::SUCCESS;
7975
}
80-
8176
}

src/Command/TemplatesListCommand.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace App\Command;
66

7+
use App\Model\TemplateData;
78
use App\Service\TemplateService;
89
use Symfony\Component\Console\Attribute\AsCommand;
910
use Symfony\Component\Console\Command\Command;
@@ -38,11 +39,11 @@ final protected function execute(InputInterface $input, OutputInterface $output)
3839

3940
$customTemplates = $this->templateService->getCustomTemplates();
4041

41-
$io->table(['ID', 'Title', 'Status', 'Type'], array_map(fn (array $templateArray) => [
42-
$templateArray['id'],
43-
$templateArray['title'],
44-
$templateArray['installed'] ? 'Installed' : 'Not Installed',
45-
$templateArray['type'],
42+
$io->table(['ID', 'Title', 'Status', 'Type'], array_map(fn (TemplateData $templateData) => [
43+
$templateData->id,
44+
$templateData->title,
45+
$templateData->installed ? 'Installed' : 'Not Installed',
46+
$templateData->type,
4647
], array_merge($templates, $customTemplates)));
4748

4849
return Command::SUCCESS;

src/Model/TemplateData.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Model;
4+
5+
use App\Entity\Template;
6+
7+
class TemplateData
8+
{
9+
public function __construct(
10+
public readonly string $id,
11+
public readonly string $title,
12+
public readonly array $adminForm,
13+
public readonly object $options,
14+
public readonly ?Template $templateEntity,
15+
public readonly bool $installed,
16+
public readonly string $type,
17+
) {}
18+
}

src/Service/TemplateService.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Service;
44

55
use App\Entity\Template;
6+
use App\Model\TemplateData;
67
use Doctrine\ORM\EntityManagerInterface;
78
use Doctrine\ORM\Id\AssignedGenerator;
89
use JsonSchema\Constraints\Factory;
@@ -19,18 +20,24 @@ public function __construct(
1920
) {
2021
}
2122

22-
public function installTemplate($templateData): void
23+
public function installTemplate(TemplateData $templateData): void
2324
{
24-
$template = new Template();
25-
$template->setTitle($templateData['title']);
25+
$template = $templateData->templateEntity;
2626

27-
$metadata = $this->entityManager->getClassMetaData($template::class);
28-
$metadata->setIdGenerator(new AssignedGenerator());
27+
if ($template === null) {
28+
$template = new Template();
2929

30-
$ulid = Ulid::fromString($templateData['id']);
31-
$template->setId($ulid);
30+
$metadata = $this->entityManager->getClassMetaData($template::class);
31+
$metadata->setIdGenerator(new AssignedGenerator());
32+
33+
$ulid = Ulid::fromString($templateData->id);
34+
$template->setId($ulid);
35+
36+
$this->entityManager->persist($template);
37+
}
38+
39+
$template->setTitle($templateData->title);
3240

33-
$this->entityManager->persist($template);
3441
$this->entityManager->flush();
3542
}
3643

@@ -99,15 +106,15 @@ public function getTemplates(iterable $finder, bool $customTemplates = false): a
99106
$repository = $this->entityManager->getRepository(Template::class);
100107
$template = $repository->findOneBy(['id' => Ulid::fromString($content->id)]);
101108

102-
$templates[] = [
103-
'id' => $content->id,
104-
'title' => $content->title,
105-
'adminForm' => $content->adminForm,
106-
'options' => $content->options,
107-
'templateEntity' => $template,
108-
'installed' => $template !== null,
109-
'type' => $customTemplates ? 'Custom' : 'Core',
110-
];
109+
$templates[] = new TemplateData(
110+
$content->id,
111+
$content->title,
112+
$content->adminForm,
113+
$content->options,
114+
$template,
115+
$template !== null,
116+
$customTemplates ? 'Custom' : 'Core',
117+
);
111118
}
112119

113120
return $templates;

0 commit comments

Comments
 (0)