Skip to content

Commit bd58080

Browse files
authored
Add Operation defaults trait (#984)
| Q | A | --------------- | ----- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Related tickets | | License | MIT
2 parents 4296466 + eaee288 commit bd58080

File tree

2 files changed

+146
-112
lines changed

2 files changed

+146
-112
lines changed

src/Component/src/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactory.php

Lines changed: 4 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,18 @@
1414
namespace Sylius\Resource\Metadata\Resource\Factory;
1515

1616
use Sylius\Resource\Metadata\AsResource;
17-
use Sylius\Resource\Metadata\HttpOperation;
18-
use Sylius\Resource\Metadata\MetadataInterface;
1917
use Sylius\Resource\Metadata\Operation;
2018
use Sylius\Resource\Metadata\Operations;
2119
use Sylius\Resource\Metadata\RegistryInterface;
2220
use Sylius\Resource\Metadata\Resource\ResourceMetadataCollection;
2321
use Sylius\Resource\Metadata\ResourceMetadata;
2422
use Sylius\Resource\Reflection\ClassReflection;
25-
use Sylius\Resource\Symfony\Request\State\Responder;
2623
use Sylius\Resource\Symfony\Routing\Factory\RouteName\OperationRouteNameFactory;
2724

2825
final 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($operation, $resources[$index], $this->operationRouteNameFactory, $this->resourceRegistry);
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($operationAttribute, $resources[$index], $this->operationRouteNameFactory, $this->resourceRegistry);
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
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Resource\Metadata\Resource\Factory;
15+
16+
use Sylius\Resource\Metadata\HttpOperation;
17+
use Sylius\Resource\Metadata\MetadataInterface;
18+
use Sylius\Resource\Metadata\Operation;
19+
use Sylius\Resource\Metadata\RegistryInterface;
20+
use Sylius\Resource\Metadata\ResourceMetadata;
21+
use Sylius\Resource\Symfony\Request\State\Responder;
22+
use Sylius\Resource\Symfony\Routing\Factory\RouteName\OperationRouteNameFactory;
23+
24+
/**
25+
* @internal
26+
*/
27+
trait OperationDefaultsTrait
28+
{
29+
private function getResourceWithDefaults(
30+
string $resourceClass,
31+
ResourceMetadata $resource,
32+
MetadataInterface $resourceConfiguration,
33+
): ResourceMetadata {
34+
$resource = $resource->withClass($resourceClass);
35+
36+
if (null === $resource->getAlias()) {
37+
$resource = $resource->withAlias($resourceConfiguration->getAlias());
38+
}
39+
40+
if (null === $resource->getApplicationName()) {
41+
$resource = $resource->withApplicationName($resourceConfiguration->getApplicationName());
42+
}
43+
44+
if (null === $resource->getName()) {
45+
$resource = $resource->withName($resourceConfiguration->getName());
46+
}
47+
48+
return $resource;
49+
}
50+
51+
private function getOperationWithDefaults(
52+
Operation $operation,
53+
ResourceMetadata $resource,
54+
OperationRouteNameFactory $operationRouteNameFactory,
55+
RegistryInterface $resourceRegistry,
56+
): array {
57+
$resourceConfiguration = $resourceRegistry->get($resource->getAlias() ?? '');
58+
59+
$operation = $operation->withResource($resource);
60+
61+
if (null === $resource->getName()) {
62+
$resourceName = $resourceConfiguration->getName();
63+
64+
$resource = $resource->withName($resourceName);
65+
$operation = $operation->withResource($resource);
66+
}
67+
68+
if (null === $resource->getPluralName()) {
69+
$resourcePluralName = $resourceConfiguration->getPluralName();
70+
71+
$resource = $resource->withPluralName($resourcePluralName);
72+
$operation = $operation->withResource($resource);
73+
}
74+
75+
if (null === $operation->getNormalizationContext()) {
76+
$operation = $operation->withNormalizationContext($resource->getNormalizationContext());
77+
}
78+
79+
if (null === $operation->getDenormalizationContext()) {
80+
$operation = $operation->withDenormalizationContext($resource->getDenormalizationContext());
81+
}
82+
83+
if (null === $operation->getValidationContext()) {
84+
$operation = $operation->withValidationContext($resource->getValidationContext());
85+
}
86+
87+
$operation = $operation->withResource($resource);
88+
89+
if (null === $operation->getRepository()) {
90+
$operation = $operation->withRepository($resourceConfiguration->getServiceId('repository'));
91+
}
92+
93+
if (null === $operation->getFormType()) {
94+
$formType = $resource->getFormType() ?? $resourceConfiguration->getClass('form');
95+
$operation = $operation->withFormType($formType);
96+
}
97+
98+
$formOptions = $this->buildFormOptions($operation, $resourceConfiguration);
99+
$operation = $operation->withFormOptions($formOptions);
100+
101+
if ($operation instanceof HttpOperation) {
102+
if (null === $operation->getRoutePrefix()) {
103+
$operation = $operation->withRoutePrefix($resource->getRoutePrefix() ?? null);
104+
}
105+
106+
if (null === $operation->getTwigContextFactory()) {
107+
$operation = $operation->withTwigContextFactory('sylius.twig.context.factory.default');
108+
}
109+
110+
if (null === $routeName = $operation->getRouteName()) {
111+
$routeName = $operationRouteNameFactory->createRouteName($operation);
112+
$operation = $operation->withRouteName($routeName);
113+
}
114+
115+
if (null === $operation->getResponder()) {
116+
$operation = $operation->withResponder(Responder::class);
117+
}
118+
119+
$operation = $operation->withName($routeName);
120+
}
121+
122+
$operationName = $operation->getName();
123+
124+
return [$operationName, $operation];
125+
}
126+
127+
private function buildFormOptions(Operation $operation, MetadataInterface $resourceConfiguration): array
128+
{
129+
$formOptions = array_merge(
130+
['data_class' => $resourceConfiguration->getClass('model')],
131+
$operation->getFormOptions() ?? [],
132+
);
133+
134+
$validationGroups = $operation->getValidationContext()['groups'] ?? null;
135+
136+
if (null !== $validationGroups) {
137+
$formOptions = array_merge(['validation_groups' => $validationGroups], $formOptions);
138+
}
139+
140+
return $formOptions;
141+
}
142+
}

0 commit comments

Comments
 (0)