Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4bfe804

Browse files
committed
minor #6739 Simplified the BatchActionDto value resolver (javiereguiluz)
This PR was merged into the 5.x branch. Discussion ---------- Simplified the BatchActionDto value resolver The `ValueResolverInterface` was introduced in Symfony 6.2 (see symfony/symfony#47363), so we don't need to take care of the previous interface. Commits ------- 1d69f8d Simplified the BatchActionDto value resolver
2 parents dbda894 + 1d69f8d commit 4bfe804

File tree

1 file changed

+17
-52
lines changed

1 file changed

+17
-52
lines changed

src/ArgumentResolver/BatchActionDtoResolver.php

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,35 @@
55
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
66
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Provider\AdminContextProviderInterface;
77
use EasyCorp\Bundle\EasyAdminBundle\Dto\BatchActionDto;
8-
use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
9-
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGeneratorInterface;
108
use Symfony\Component\HttpFoundation\Request;
11-
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
129
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
1310
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
1411

1512
/*
1613
* @author Javier Eguiluz <[email protected]>
1714
*/
18-
if (interface_exists(ValueResolverInterface::class)) {
19-
final class BatchActionDtoResolver implements ValueResolverInterface
20-
{
21-
public function __construct(
22-
private readonly AdminContextProviderInterface $adminContextProvider,
23-
) {
24-
}
25-
26-
public function resolve(Request $request, ArgumentMetadata $argument): iterable
27-
{
28-
if (BatchActionDto::class !== $argument->getType()) {
29-
return [];
30-
}
31-
32-
if (null === $context = $this->adminContextProvider->getContext()) {
33-
throw new \RuntimeException(sprintf('Some of your controller actions have type-hinted an argument with the "%s" class but that\'s only available for actions run to serve EasyAdmin requests. Remove the type-hint or make sure the action is part of an EasyAdmin request.', BatchActionDto::class));
34-
}
35-
36-
yield new BatchActionDto(
37-
$context->getRequest()->request->get(EA::BATCH_ACTION_NAME),
38-
$context->getRequest()->request->all()[EA::BATCH_ACTION_ENTITY_IDS] ?? [],
39-
$context->getRequest()->request->get(EA::ENTITY_FQCN),
40-
$context->getRequest()->request->get(EA::BATCH_ACTION_CSRF_TOKEN)
41-
);
42-
}
15+
final class BatchActionDtoResolver implements ValueResolverInterface
16+
{
17+
public function __construct(
18+
private readonly AdminContextProviderInterface $adminContextProvider,
19+
) {
4320
}
44-
} else {
45-
final class BatchActionDtoResolver implements ArgumentValueResolverInterface
46-
{
47-
private AdminContextProvider $adminContextProvider;
4821

49-
public function __construct(AdminContextProviderInterface $adminContextProvider, AdminUrlGeneratorInterface $adminUrlGenerator)
50-
{
51-
$this->adminContextProvider = $adminContextProvider;
52-
$this->adminUrlGenerator = $adminUrlGenerator;
22+
public function resolve(Request $request, ArgumentMetadata $argument): iterable
23+
{
24+
if (BatchActionDto::class !== $argument->getType()) {
25+
return [];
5326
}
5427

55-
public function supports(Request $request, ArgumentMetadata $argument): bool
56-
{
57-
return BatchActionDto::class === $argument->getType();
28+
if (null === $context = $this->adminContextProvider->getContext()) {
29+
throw new \RuntimeException(sprintf('Some of your controller actions have type-hinted an argument with the "%s" class but that\'s only available for actions run to serve EasyAdmin requests. Remove the type-hint or make sure the action is part of an EasyAdmin request.', BatchActionDto::class));
5830
}
5931

60-
public function resolve(Request $request, ArgumentMetadata $argument): iterable
61-
{
62-
if (null === $context = $this->adminContextProvider->getContext()) {
63-
throw new \RuntimeException(sprintf('Some of your controller actions have type-hinted an argument with the "%s" class but that\'s only available for actions run to serve EasyAdmin requests. Remove the type-hint or make sure the action is part of an EasyAdmin request.', BatchActionDto::class));
64-
}
65-
66-
yield new BatchActionDto(
67-
$context->getRequest()->request->get(EA::BATCH_ACTION_NAME),
68-
$context->getRequest()->request->all()[EA::BATCH_ACTION_ENTITY_IDS] ?? [],
69-
$context->getRequest()->request->get(EA::ENTITY_FQCN),
70-
$context->getRequest()->request->get(EA::BATCH_ACTION_CSRF_TOKEN)
71-
);
72-
}
32+
yield new BatchActionDto(
33+
$context->getRequest()->request->get(EA::BATCH_ACTION_NAME),
34+
$context->getRequest()->request->all()[EA::BATCH_ACTION_ENTITY_IDS] ?? [],
35+
$context->getRequest()->request->get(EA::ENTITY_FQCN),
36+
$context->getRequest()->request->get(EA::BATCH_ACTION_CSRF_TOKEN)
37+
);
7338
}
7439
}

0 commit comments

Comments
 (0)