1414namespace Sylius \Resource \Metadata \Resource \Factory ;
1515
1616use Sylius \Resource \Metadata \AsResource ;
17- use Sylius \Resource \Metadata \HttpOperation ;
18- use Sylius \Resource \Metadata \MetadataInterface ;
1917use Sylius \Resource \Metadata \Operation ;
2018use Sylius \Resource \Metadata \Operations ;
2119use Sylius \Resource \Metadata \RegistryInterface ;
2220use Sylius \Resource \Metadata \Resource \ResourceMetadataCollection ;
2321use Sylius \Resource \Metadata \ResourceMetadata ;
2422use Sylius \Resource \Reflection \ClassReflection ;
25- use Sylius \Resource \Symfony \Request \State \Responder ;
2623use Sylius \Resource \Symfony \Routing \Factory \RouteName \OperationRouteNameFactory ;
2724
2825final class AttributesResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
2926{
27+ use OperationDefaultsTrait;
28+
3029 public function __construct (
3130 private RegistryInterface $ resourceRegistry ,
3231 private OperationRouteNameFactory $ operationRouteNameFactory ,
@@ -77,7 +76,7 @@ private function buildResourceOperations(array $attributes, string $resourceClas
7776
7877 /** @var Operation $operation */
7978 foreach ($ resource ->getOperations () ?? new Operations () as $ operation ) {
80- [$ key , $ operation ] = $ this ->getOperationWithDefaults ($ resources [$ index ], $ operation );
79+ [$ key , $ operation ] = $ this ->getOperationWithDefaults ($ this -> operationRouteNameFactory , $ this -> resourceRegistry , $ resources [$ index ], $ operation );
8180 $ operations [$ key ] = $ operation ;
8281 }
8382
@@ -107,7 +106,7 @@ private function buildResourceOperations(array $attributes, string $resourceClas
107106 /** @var Operation $operationAttribute */
108107 $ operationAttribute = $ attribute ->newInstance ();
109108
110- [$ key , $ operation ] = $ this ->getOperationWithDefaults ($ resources [$ index ], $ operationAttribute );
109+ [$ key , $ operation ] = $ this ->getOperationWithDefaults ($ this -> operationRouteNameFactory , $ this -> resourceRegistry , $ resources [$ index ], $ operationAttribute );
111110
112111 $ operations = $ resources [$ index ]->getOperations () ?? new Operations ();
113112
@@ -117,111 +116,4 @@ private function buildResourceOperations(array $attributes, string $resourceClas
117116
118117 return $ resources ;
119118 }
120-
121- private function getResourceWithDefaults (string $ resourceClass , ResourceMetadata $ resource , MetadataInterface $ resourceConfiguration ): ResourceMetadata
122- {
123- $ resource = $ resource ->withClass ($ resourceClass );
124-
125- if (null === $ resource ->getAlias ()) {
126- $ resource = $ resource ->withAlias ($ resourceConfiguration ->getAlias ());
127- }
128-
129- if (null === $ resource ->getApplicationName ()) {
130- $ resource = $ resource ->withApplicationName ($ resourceConfiguration ->getApplicationName ());
131- }
132-
133- if (null === $ resource ->getName ()) {
134- $ resource = $ resource ->withName ($ resourceConfiguration ->getName ());
135- }
136-
137- return $ resource ;
138- }
139-
140- private function getOperationWithDefaults (ResourceMetadata $ resource , Operation $ operation ): array
141- {
142- $ resourceConfiguration = $ this ->resourceRegistry ->get ($ resource ->getAlias () ?? '' );
143-
144- $ operation = $ operation ->withResource ($ resource );
145-
146- if (null === $ resource ->getName ()) {
147- $ resourceName = $ resourceConfiguration ->getName ();
148-
149- $ resource = $ resource ->withName ($ resourceName );
150- $ operation = $ operation ->withResource ($ resource );
151- }
152-
153- if (null === $ resource ->getPluralName ()) {
154- $ resourcePluralName = $ resourceConfiguration ->getPluralName ();
155-
156- $ resource = $ resource ->withPluralName ($ resourcePluralName );
157- $ operation = $ operation ->withResource ($ resource );
158- }
159-
160- if (null === $ operation ->getNormalizationContext ()) {
161- $ operation = $ operation ->withNormalizationContext ($ resource ->getNormalizationContext ());
162- }
163-
164- if (null === $ operation ->getDenormalizationContext ()) {
165- $ operation = $ operation ->withDenormalizationContext ($ resource ->getDenormalizationContext ());
166- }
167-
168- if (null === $ operation ->getValidationContext ()) {
169- $ operation = $ operation ->withValidationContext ($ resource ->getValidationContext ());
170- }
171-
172- $ operation = $ operation ->withResource ($ resource );
173-
174- if (null === $ operation ->getRepository ()) {
175- $ operation = $ operation ->withRepository ($ resourceConfiguration ->getServiceId ('repository ' ));
176- }
177-
178- if (null === $ operation ->getFormType ()) {
179- $ formType = $ resource ->getFormType () ?? $ resourceConfiguration ->getClass ('form ' );
180- $ operation = $ operation ->withFormType ($ formType );
181- }
182-
183- $ formOptions = $ this ->buildFormOptions ($ operation , $ resourceConfiguration );
184- $ operation = $ operation ->withFormOptions ($ formOptions );
185-
186- if ($ operation instanceof HttpOperation) {
187- if (null === $ operation ->getRoutePrefix ()) {
188- $ operation = $ operation ->withRoutePrefix ($ resource ->getRoutePrefix () ?? null );
189- }
190-
191- if (null === $ operation ->getTwigContextFactory ()) {
192- $ operation = $ operation ->withTwigContextFactory ('sylius.twig.context.factory.default ' );
193- }
194-
195- if (null === $ routeName = $ operation ->getRouteName ()) {
196- $ routeName = $ this ->operationRouteNameFactory ->createRouteName ($ operation );
197- $ operation = $ operation ->withRouteName ($ routeName );
198- }
199-
200- if (null === $ operation ->getResponder ()) {
201- $ operation = $ operation ->withResponder (Responder::class);
202- }
203-
204- $ operation = $ operation ->withName ($ routeName );
205- }
206-
207- $ operationName = $ operation ->getName ();
208-
209- return [$ operationName , $ operation ];
210- }
211-
212- private function buildFormOptions (Operation $ operation , MetadataInterface $ resourceConfiguration ): array
213- {
214- $ formOptions = array_merge (
215- ['data_class ' => $ resourceConfiguration ->getClass ('model ' )],
216- $ operation ->getFormOptions () ?? [],
217- );
218-
219- $ validationGroups = $ operation ->getValidationContext ()['groups ' ] ?? null ;
220-
221- if (null !== $ validationGroups ) {
222- $ formOptions = array_merge (['validation_groups ' => $ validationGroups ], $ formOptions );
223- }
224-
225- return $ formOptions ;
226- }
227119}
0 commit comments