@@ -31,74 +31,39 @@ public function getResourceJsonInDirectory(string $path, string $resourceType, R
3131 $ finder ->files ()->followLinks ()->ignoreUnreadableDirs ()->in ($ path )->depth ('== 0 ' )->name ('*.json ' );
3232
3333 if ($ finder ->hasResults ()) {
34- switch ($ resourceType ) {
35- case ScreenLayoutData::class:
36- return $ this ->getScreenLayoutData ($ finder , $ type );
37- case TemplateData::class:
38- return $ this ->getTemplateData ($ finder , $ type );
39- default :
40- throw new NotImplementedException ();
41- }
34+ return match ($ resourceType ) {
35+ ScreenLayoutData::class, TemplateData::class => $ this ->getResourceData ($ finder , $ resourceType , $ type ),
36+ default => throw new NotImplementedException (),
37+ };
4238 }
4339 }
4440
4541 return [];
4642 }
4743
48- private function getTemplateData (iterable $ finder , ResourceTypeEnum $ type ): array
44+ private function getResourceData (iterable $ finder, string $ resourceType , ResourceTypeEnum $ type ): array
4945 {
50- $ templates = [];
51-
5246 // Validate template json.
5347 $ schemaStorage = new SchemaStorage ();
54- $ jsonSchemaObject = $ this ->getTemplateJsonSchema ();
55- $ schemaStorage ->addSchema ('file://contentSchema ' , $ jsonSchemaObject );
56- $ validator = new Validator (new Factory ($ schemaStorage ));
57-
58- foreach ($ finder as $ file ) {
59- $ content = json_decode ((string ) $ file ->getContents ());
60- $ validator ->validate ($ content , $ jsonSchemaObject );
6148
62- if (!$ validator ->isValid ()) {
63- $ message = 'JSON file ' .$ file ->getFilename ()." does not validate. Violations: \n" ;
64- foreach ($ validator ->getErrors () as $ error ) {
65- $ message .= sprintf ("\n[%s] %s " , $ error ['property ' ], $ error ['message ' ]);
66- }
67-
68- throw new \Exception ($ message );
69- }
70-
71- if (!Ulid::isValid ($ content ->id )) {
72- throw new \Exception ('The Ulid is not valid ' );
73- }
74-
75- $ repository = $ this ->entityManager ->getRepository (Template::class);
76- $ template = $ repository ->findOneBy (['id ' => Ulid::fromString ($ content ->id )]);
77-
78- $ templates [] = new TemplateData (
79- $ content ->id ,
80- $ content ->title ,
81- $ content ->adminForm ,
82- $ content ->options ,
83- $ template ,
84- null !== $ template ,
85- $ type ,
86- );
49+ switch ($ resourceType ) {
50+ case ScreenLayoutData::class:
51+ $ repository = $ this ->entityManager ->getRepository (ScreenLayout::class);
52+ $ jsonSchemaObject = $ this ->getScreenLayoutJsonSchema ();
53+ break ;
54+ case TemplateData::class:
55+ $ repository = $ this ->entityManager ->getRepository (Template::class);
56+ $ jsonSchemaObject = $ this ->getTemplateJsonSchema ();
57+ break ;
58+ default :
59+ throw new NotImplementedException ();
8760 }
8861
89- return $ templates ;
90- }
91-
92- private function getScreenLayoutData (iterable $ finder , ResourceTypeEnum $ type ): array
93- {
94- $ screenLayouts = [];
95-
96- // Validate template json.
97- $ schemaStorage = new SchemaStorage ();
98- $ jsonSchemaObject = $ this ->getScreenLayoutJsonSchema ();
9962 $ schemaStorage ->addSchema ('file://contentSchema ' , $ jsonSchemaObject );
10063 $ validator = new Validator (new Factory ($ schemaStorage ));
10164
65+ $ results = [];
66+
10267 foreach ($ finder as $ file ) {
10368 $ content = json_decode ((string ) $ file ->getContents ());
10469 $ validator ->validate ($ content , $ jsonSchemaObject );
@@ -116,22 +81,37 @@ private function getScreenLayoutData(iterable $finder, ResourceTypeEnum $type):
11681 throw new \Exception ('The Ulid is not valid ' );
11782 }
11883
119- $ repository = $ this ->entityManager ->getRepository (ScreenLayout::class);
120- $ screenLayout = $ repository ->findOneBy (['id ' => Ulid::fromString ($ content ->id )]);
121-
122- $ screenLayouts [] = new ScreenLayoutData (
123- $ content ->id ,
124- $ content ->title ,
125- $ type ,
126- $ content ->grid ->rows ,
127- $ content ->grid ->columns ,
128- $ screenLayout ,
129- null !== $ screenLayout ,
130- $ content ->regions ,
131- );
84+ $ entity = $ repository ->findOneBy (['id ' => Ulid::fromString ($ content ->id )]);
85+
86+ switch ($ resourceType ) {
87+ case ScreenLayoutData::class:
88+ $ results [] = new ScreenLayoutData (
89+ $ content ->id ,
90+ $ content ->title ,
91+ $ type ,
92+ $ content ->grid ->rows ,
93+ $ content ->grid ->columns ,
94+ $ entity ,
95+ null !== $ entity ,
96+ $ content ->regions ,
97+ );
98+
99+ break ;
100+ case TemplateData::class:
101+ $ results [] = new TemplateData (
102+ $ content ->id ,
103+ $ content ->title ,
104+ $ content ->adminForm ,
105+ $ content ->options ,
106+ $ entity ,
107+ null !== $ entity ,
108+ $ type ,
109+ );
110+ break ;
111+ }
132112 }
133113
134- return $ screenLayouts ;
114+ return $ results ;
135115 }
136116
137117 /**
0 commit comments