66
77use App \Entity \Template ;
88use App \Enum \ResourceTypeEnum ;
9+ use App \Exceptions \NotAcceptableException ;
10+ use App \Exceptions \NotFoundException ;
11+ use App \Model \InstallStatus ;
912use App \Model \TemplateData ;
13+ use App \Repository \SlideRepository ;
14+ use App \Repository \TemplateRepository ;
1015use App \Utils \ResourceLoader ;
1116use Doctrine \ORM \EntityManagerInterface ;
1217use 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