14
14
namespace Sylius \Resource \Metadata \Resource \Factory ;
15
15
16
16
use Sylius \Resource \Metadata \AsResource ;
17
- use Sylius \Resource \Metadata \HttpOperation ;
18
- use Sylius \Resource \Metadata \MetadataInterface ;
19
17
use Sylius \Resource \Metadata \Operation ;
20
18
use Sylius \Resource \Metadata \Operations ;
21
19
use Sylius \Resource \Metadata \RegistryInterface ;
22
20
use Sylius \Resource \Metadata \Resource \ResourceMetadataCollection ;
23
21
use Sylius \Resource \Metadata \ResourceMetadata ;
24
22
use Sylius \Resource \Reflection \ClassReflection ;
25
- use Sylius \Resource \Symfony \Request \State \Responder ;
26
23
use Sylius \Resource \Symfony \Routing \Factory \RouteName \OperationRouteNameFactory ;
27
24
28
25
final class AttributesResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
29
26
{
27
+ use OperationDefaultsTrait;
28
+
30
29
public function __construct (
31
30
private RegistryInterface $ resourceRegistry ,
32
31
private OperationRouteNameFactory $ operationRouteNameFactory ,
@@ -77,7 +76,7 @@ private function buildResourceOperations(array $attributes, string $resourceClas
77
76
78
77
/** @var Operation $operation */
79
78
foreach ($ resource ->getOperations () ?? new Operations () as $ operation ) {
80
- [$ key , $ operation ] = $ this ->getOperationWithDefaults ($ resources [$ index ], $ operation );
79
+ [$ key , $ operation ] = $ this ->getOperationWithDefaults ($ operation , $ resources [$ index ], $ this -> operationRouteNameFactory , $ this -> resourceRegistry );
81
80
$ operations [$ key ] = $ operation ;
82
81
}
83
82
@@ -107,7 +106,7 @@ private function buildResourceOperations(array $attributes, string $resourceClas
107
106
/** @var Operation $operationAttribute */
108
107
$ operationAttribute = $ attribute ->newInstance ();
109
108
110
- [$ key , $ operation ] = $ this ->getOperationWithDefaults ($ resources [$ index ], $ operationAttribute );
109
+ [$ key , $ operation ] = $ this ->getOperationWithDefaults ($ operationAttribute , $ resources [$ index ], $ this -> operationRouteNameFactory , $ this -> resourceRegistry );
111
110
112
111
$ operations = $ resources [$ index ]->getOperations () ?? new Operations ();
113
112
@@ -117,111 +116,4 @@ private function buildResourceOperations(array $attributes, string $resourceClas
117
116
118
117
return $ resources ;
119
118
}
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
- }
227
119
}
0 commit comments