Skip to content

Commit 65020c3

Browse files
committed
4565: Refactored template service and command
1 parent ccbc33a commit 65020c3

10 files changed

+107
-68
lines changed

src/Command/ScreenLayout/ScreenLayoutsInstallCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace App\Command\ScreenLayout;
66

77
use App\Exceptions\NotFoundException;
8-
use App\Model\ScreenLayoutData;
98
use App\Service\ScreenLayoutService;
109
use Symfony\Component\Console\Attribute\AsCommand;
1110
use Symfony\Component\Console\Command\Command;
@@ -63,6 +62,7 @@ final protected function execute(InputInterface $input, OutputInterface $output)
6362
$this->screenLayoutService->installById($screenLayoutUlid, $update, $cleanupRegions);
6463
} catch (NotFoundException $e) {
6564
$io->error($e->getMessage());
65+
6666
return Command::FAILURE;
6767
}
6868

src/Command/ScreenLayout/ScreenLayoutsRemoveCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ final protected function execute(InputInterface $input, OutputInterface $output)
4747
$this->screenLayoutService->remove($ulid);
4848
} catch (NotFoundException|NotAcceptableException $e) {
4949
$io->error($e->getMessage());
50+
5051
return Command::FAILURE;
5152
}
5253

src/Command/Template/TemplatesInstallCommand.php

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

55
namespace App\Command\Template;
66

7-
use App\Model\TemplateData;
7+
use App\Exceptions\NotFoundException;
88
use App\Service\TemplateService;
99
use Symfony\Component\Console\Attribute\AsCommand;
1010
use Symfony\Component\Console\Command\Command;
@@ -40,12 +40,8 @@ final protected function execute(InputInterface $input, OutputInterface $output)
4040
$all = $input->getOption('all');
4141
$update = $input->getOption('update');
4242

43-
$templates = $this->templateService->getTemplates();
44-
4543
if ($all) {
46-
foreach ($templates as $templateToInstall) {
47-
$this->templateService->installTemplate($templateToInstall, $update);
48-
}
44+
$this->templateService->installAll($update);
4945

5046
$io->success('Installed all available templates');
5147

@@ -60,16 +56,15 @@ final protected function execute(InputInterface $input, OutputInterface $output)
6056
return Command::INVALID;
6157
}
6258

63-
$templateToInstall = array_find($templates, fn (TemplateData $templateData): bool => $templateData->id === $templateUlid);
64-
65-
if (null === $templateToInstall) {
66-
$io->error('Template not found.');
59+
try {
60+
$this->templateService->installById($templateUlid, $update);
61+
} catch (NotFoundException $e) {
62+
$io->error($e->getMessage());
6763

6864
return Command::FAILURE;
6965
}
7066

71-
$this->templateService->installTemplate($templateToInstall);
72-
$io->success('Template '.$templateToInstall->title.' installed');
67+
$io->success('Template with ULID: '.$templateUlid.' installed');
7368

7469
return Command::SUCCESS;
7570
}

src/Command/Template/TemplatesListCommand.php

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

55
namespace App\Command\Template;
66

7-
use App\Enum\ResourceTypeEnum;
87
use App\Model\TemplateData;
98
use App\Service\TemplateService;
109
use Symfony\Component\Console\Attribute\AsCommand;
@@ -38,23 +37,15 @@ final protected function execute(InputInterface $input, OutputInterface $output)
3837
$status = $input->getOption('status');
3938

4039
try {
41-
$templates = $this->templateService->getTemplates();
42-
43-
$coreTemplateCount = count(array_filter($templates, fn (TemplateData $template) => ResourceTypeEnum::CORE === $template->type));
44-
45-
if (0 === $coreTemplateCount) {
46-
$io->error('No core templates found.');
47-
48-
return Command::INVALID;
49-
}
50-
5140
if ($status) {
52-
$numberOfTemplates = count($templates);
53-
$numberOfInstalledTemplates = count(array_filter($templates, fn ($entry): bool => $entry->installed));
54-
$text = $numberOfInstalledTemplates.' / '.$numberOfTemplates.' templates installed.';
41+
$installStatus = $this->templateService->getInstallStatus();
42+
43+
$text = $installStatus->installed.' / '.$installStatus->available.' templates installed.';
5544

5645
$io->success($text);
5746
} else {
47+
$templates = $this->templateService->getAll();
48+
5849
$io->table(['ID', 'Title', 'Status', 'Type'], array_map(fn (TemplateData $templateData) => [
5950
$templateData->id,
6051
$templateData->title,

src/Command/Template/TemplatesRemoveCommand.php

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44

55
namespace App\Command\Template;
66

7+
use App\Exceptions\NotAcceptableException;
8+
use App\Exceptions\NotFoundException;
79
use App\Repository\SlideRepository;
810
use App\Repository\TemplateRepository;
11+
use App\Service\TemplateService;
912
use Doctrine\ORM\EntityManagerInterface;
1013
use Symfony\Component\Console\Attribute\AsCommand;
1114
use Symfony\Component\Console\Command\Command;
1215
use Symfony\Component\Console\Input\InputArgument;
1316
use Symfony\Component\Console\Input\InputInterface;
1417
use Symfony\Component\Console\Output\OutputInterface;
1518
use Symfony\Component\Console\Style\SymfonyStyle;
16-
use Symfony\Component\Uid\Ulid;
1719

1820
#[AsCommand(
1921
name: 'app:templates:remove',
@@ -25,6 +27,7 @@ public function __construct(
2527
private readonly EntityManagerInterface $entityManager,
2628
private readonly TemplateRepository $templateRepository,
2729
private readonly SlideRepository $slideRepository,
30+
private readonly TemplateService $templateService,
2831
) {
2932
parent::__construct();
3033
}
@@ -46,34 +49,14 @@ final protected function execute(InputInterface $input, OutputInterface $output)
4649
return Command::INVALID;
4750
}
4851

49-
$template = $this->templateRepository->findOneBy(['id' => Ulid::fromString($ulid)]);
52+
try {
53+
$this->templateService->remove($ulid);
54+
} catch (NotFoundException|NotAcceptableException $e) {
55+
$io->error($e->getMessage());
5056

51-
if (!$template) {
52-
$io->error('Template not installed. Aborting.');
53-
54-
return self::INVALID;
57+
return Command::FAILURE;
5558
}
5659

57-
$slides = $this->slideRepository->findBy(['template' => $template]);
58-
$numberOfSlides = count($slides);
59-
60-
if ($numberOfSlides > 0) {
61-
$message = "Aborting. Template is bound to $numberOfSlides following slides:\n\n";
62-
63-
foreach ($slides as $slide) {
64-
$id = $slide->getId();
65-
$message .= "$id\n";
66-
}
67-
68-
$io->error($message);
69-
70-
return self::INVALID;
71-
}
72-
73-
$this->entityManager->remove($template);
74-
75-
$this->entityManager->flush();
76-
7760
$io->success('Template removed.');
7861

7962
return Command::SUCCESS;

src/Command/Template/TemplatesUpdateCommand.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ final protected function execute(InputInterface $input, OutputInterface $output)
2727
{
2828
$io = new SymfonyStyle($input, $output);
2929

30-
$templates = $this->templateService->getTemplates();
31-
32-
foreach ($templates as $templateToUpdate) {
33-
$this->templateService->updateTemplate($templateToUpdate);
34-
}
30+
$this->templateService->updateAll();
3531

3632
$io->success('Updated all installed templates');
3733

src/Command/UpdateCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ final protected function execute(InputInterface $input, OutputInterface $output)
5353
return Command::FAILURE;
5454
}
5555

56-
$allTemplates = $this->templateService->getTemplates();
56+
$allTemplates = $this->templateService->getAll();
5757
$installedTemplates = array_filter($allTemplates, fn ($entry): bool => $entry->installed);
5858

5959
// If no installed templates, we assume that this is a new installation and offer to install all templates.

src/Model/InstallStatus.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace App\Model;
46

57
class InstallStatus

src/Service/ScreenLayoutService.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ public function installAll(bool $update = false, bool $cleanupRegions = false):
5151

5252
public function installById(string $ulidString, bool $update = false, bool $cleanupRegions = false): void
5353
{
54-
$screenLayoutToInstall = array_find($this->getAll(), fn (ScreenLayoutData $screenLayoutData): bool => $screenLayoutData->id === $ulidString);
54+
$screenLayoutToInstall = array_find($this->getAll(), fn (ScreenLayoutData $screenLayoutData): bool => $screenLayoutData->id === $ulidString);
5555

56-
if (null === $screenLayoutToInstall) {
57-
throw new NotFoundException();
58-
}
56+
if (null === $screenLayoutToInstall) {
57+
throw new NotFoundException();
58+
}
5959

60-
$this->install($screenLayoutToInstall, $update, $cleanupRegions);
60+
$this->install($screenLayoutToInstall, $update, $cleanupRegions);
6161
}
6262

6363
public function install(ScreenLayoutData $screenLayoutData, bool $update = false, bool $cleanupRegions = false): void
@@ -177,7 +177,6 @@ public function remove(string $ulidString): void
177177
$this->entityManager->remove($screenLayout);
178178

179179
$this->entityManager->flush();
180-
181180
}
182181

183182
public function getInstallStatus(): InstallStatus

src/Service/TemplateService.php

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66

77
use App\Entity\Template;
88
use App\Enum\ResourceTypeEnum;
9+
use App\Exceptions\NotAcceptableException;
10+
use App\Exceptions\NotFoundException;
11+
use App\Model\InstallStatus;
912
use App\Model\TemplateData;
13+
use App\Repository\SlideRepository;
14+
use App\Repository\TemplateRepository;
1015
use App\Utils\ResourceLoader;
1116
use Doctrine\ORM\EntityManagerInterface;
1217
use Doctrine\ORM\Id\AssignedGenerator;
@@ -19,18 +24,40 @@ class TemplateService
1924

2025
public function __construct(
2126
private readonly EntityManagerInterface $entityManager,
27+
private readonly TemplateRepository $templateRepository,
28+
private readonly SlideRepository $slideRepository,
2229
private readonly ResourceLoader $loader,
2330
) {}
2431

25-
public function getTemplates(): array
32+
public function getAll(): array
2633
{
2734
$core = $this->loader->getResourceInDirectory($this::CORE_TEMPLATES_PATH, TemplateData::class, ResourceTypeEnum::CORE);
2835
$custom = $this->loader->getResourceInDirectory($this::CUSTOM_TEMPLATES_PATH, TemplateData::class, ResourceTypeEnum::CUSTOM);
2936

3037
return array_merge($core, $custom);
3138
}
3239

33-
public function installTemplate(TemplateData $templateData, bool $update = false): void
40+
public function installAll(bool $update): void
41+
{
42+
$templates = $this->getAll();
43+
44+
foreach ($templates as $templateToInstall) {
45+
$this->install($templateToInstall, $update);
46+
}
47+
}
48+
49+
public function installById(string $ulidString, bool $update = false): void
50+
{
51+
$templateToInstall = array_find($this->getAll(), fn (TemplateData $templateData): bool => $templateData->id === $ulidString);
52+
53+
if (null === $templateToInstall) {
54+
throw new NotFoundException();
55+
}
56+
57+
$this->install($templateToInstall, $update);
58+
}
59+
60+
public function install(TemplateData $templateData, bool $update = false): void
3461
{
3562
$template = $templateData->templateEntity;
3663

@@ -53,7 +80,16 @@ public function installTemplate(TemplateData $templateData, bool $update = false
5380
$this->entityManager->flush();
5481
}
5582

56-
public function updateTemplate(TemplateData $templateData): void
83+
public function updateAll(): void
84+
{
85+
$templates = $this->getAll();
86+
87+
foreach ($templates as $templateToUpdate) {
88+
$this->update($templateToUpdate);
89+
}
90+
}
91+
92+
public function update(TemplateData $templateData): void
5793
{
5894
$template = $templateData->templateEntity;
5995

@@ -66,4 +102,40 @@ public function updateTemplate(TemplateData $templateData): void
66102

67103
$this->entityManager->flush();
68104
}
105+
106+
public function remove(string $ulidString): void
107+
{
108+
$template = $this->templateRepository->findOneBy(['id' => Ulid::fromString($ulidString)]);
109+
110+
if (!$template) {
111+
throw new NotFoundException('Template not installed. Aborting.');
112+
}
113+
114+
$slides = $this->slideRepository->findBy(['template' => $template]);
115+
$numberOfSlides = count($slides);
116+
117+
if ($numberOfSlides > 0) {
118+
$message = "Aborting. Template is bound to $numberOfSlides following slides:\n\n";
119+
120+
foreach ($slides as $slide) {
121+
$id = $slide->getId();
122+
$message .= "$id\n";
123+
}
124+
125+
throw new NotAcceptableException($message);
126+
}
127+
128+
$this->entityManager->remove($template);
129+
130+
$this->entityManager->flush();
131+
}
132+
133+
public function getInstallStatus(): InstallStatus
134+
{
135+
$templates = $this->getAll();
136+
$numberOfTemplates = count($templates);
137+
$numberOfInstalledTemplates = count(array_filter($templates, fn ($entry): bool => $entry->installed));
138+
139+
return new InstallStatus($numberOfTemplates, $numberOfInstalledTemplates);
140+
}
69141
}

0 commit comments

Comments
 (0)