Skip to content

Commit

Permalink
Simplify some code
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Oct 24, 2023
1 parent 91427f0 commit 3b20674
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 100 deletions.
47 changes: 13 additions & 34 deletions src/ArgumentResolver/AdminContextResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,22 @@
/*
* @author Javier Eguiluz <[email protected]>
*/
if (interface_exists(ValueResolverInterface::class)) {
final class AdminContextResolver implements ValueResolverInterface
{
private AdminContextProvider $adminContextProvider;

public function __construct(AdminContextProvider $adminContextProvider)
{
$this->adminContextProvider = $adminContextProvider;
}

public function resolve(Request $request, ArgumentMetadata $argument): iterable
{
if (AdminContext::class !== $argument->getType()) {
return [];
}
final class AdminContextResolver implements ArgumentValueResolverInterface, ValueResolverInterface

Check failure on line 15 in src/ArgumentResolver/AdminContextResolver.php

View workflow job for this annotation

GitHub Actions / phpstan

Class EasyCorp\Bundle\EasyAdminBundle\ArgumentResolver\AdminContextResolver implements unknown interface Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface.
{
private AdminContextProvider $adminContextProvider;

yield $this->adminContextProvider->getContext();
}
}
} else {
final class AdminContextResolver implements ArgumentValueResolverInterface
public function __construct(AdminContextProvider $adminContextProvider)
{
private AdminContextProvider $adminContextProvider;

public function __construct(AdminContextProvider $adminContextProvider)
{
$this->adminContextProvider = $adminContextProvider;
}
$this->adminContextProvider = $adminContextProvider;
}

public function supports(Request $request, ArgumentMetadata $argument): bool
{
return AdminContext::class === $argument->getType();
}
public function supports(Request $request, ArgumentMetadata $argument): bool
{
return AdminContext::class === $argument->getType();
}

public function resolve(Request $request, ArgumentMetadata $argument): iterable
{
yield $this->adminContextProvider->getContext();
}
public function resolve(Request $request, ArgumentMetadata $argument): iterable
{
yield $this->adminContextProvider->getContext();
}
}
94 changes: 28 additions & 66 deletions src/ArgumentResolver/BatchActionDtoResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,77 +14,39 @@
/*
* @author Javier Eguiluz <[email protected]>
*/
if (interface_exists(ValueResolverInterface::class)) {
final class BatchActionDtoResolver implements ValueResolverInterface
{
private AdminContextProvider $adminContextProvider;
private AdminUrlGeneratorInterface $adminUrlGenerator;

public function __construct(AdminContextProvider $adminContextProvider, AdminUrlGeneratorInterface $adminUrlGenerator)
{
$this->adminContextProvider = $adminContextProvider;
$this->adminUrlGenerator = $adminUrlGenerator;
}

public function resolve(Request $request, ArgumentMetadata $argument): iterable
{
if (BatchActionDto::class !== $argument->getType()) {
return [];
}
final class BatchActionDtoResolver implements ArgumentValueResolverInterface, ValueResolverInterface

Check failure on line 17 in src/ArgumentResolver/BatchActionDtoResolver.php

View workflow job for this annotation

GitHub Actions / phpstan

Class EasyCorp\Bundle\EasyAdminBundle\ArgumentResolver\BatchActionDtoResolver implements unknown interface Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface.
{
private AdminContextProvider $adminContextProvider;
private AdminUrlGeneratorInterface $adminUrlGenerator;

if (null === $context = $this->adminContextProvider->getContext()) {
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));
}

$batchActionUrl = $context->getRequest()->request->get(EA::BATCH_ACTION_URL);
$batchActionUrlQueryString = parse_url($batchActionUrl, \PHP_URL_QUERY);
parse_str($batchActionUrlQueryString, $batchActionUrlParts);
$referrerUrl = $batchActionUrlParts[EA::REFERRER] ?? $this->adminUrlGenerator->unsetAll()->generateUrl();

yield new BatchActionDto(
$context->getRequest()->request->get(EA::BATCH_ACTION_NAME),
$context->getRequest()->request->all()[EA::BATCH_ACTION_ENTITY_IDS] ?? [],
$context->getRequest()->request->get(EA::ENTITY_FQCN),
$referrerUrl,
$context->getRequest()->request->get(EA::BATCH_ACTION_CSRF_TOKEN)
);
}
}
} else {
final class BatchActionDtoResolver implements ArgumentValueResolverInterface
public function __construct(AdminContextProvider $adminContextProvider, AdminUrlGeneratorInterface $adminUrlGenerator)
{
private AdminContextProvider $adminContextProvider;
private AdminUrlGeneratorInterface $adminUrlGenerator;
$this->adminContextProvider = $adminContextProvider;
$this->adminUrlGenerator = $adminUrlGenerator;
}

public function __construct(AdminContextProvider $adminContextProvider, AdminUrlGeneratorInterface $adminUrlGenerator)
{
$this->adminContextProvider = $adminContextProvider;
$this->adminUrlGenerator = $adminUrlGenerator;
}
public function supports(Request $request, ArgumentMetadata $argument): bool
{
return BatchActionDto::class === $argument->getType();
}

public function supports(Request $request, ArgumentMetadata $argument): bool
{
return BatchActionDto::class === $argument->getType();
public function resolve(Request $request, ArgumentMetadata $argument): iterable
{
if (null === $context = $this->adminContextProvider->getContext()) {
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));
}

public function resolve(Request $request, ArgumentMetadata $argument): iterable
{
if (null === $context = $this->adminContextProvider->getContext()) {
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));
}

$batchActionUrl = $context->getRequest()->request->get(EA::BATCH_ACTION_URL);
$batchActionUrlQueryString = parse_url($batchActionUrl, \PHP_URL_QUERY);
parse_str($batchActionUrlQueryString, $batchActionUrlParts);
$referrerUrl = $batchActionUrlParts[EA::REFERRER] ?? $this->adminUrlGenerator->unsetAll()->generateUrl();

yield new BatchActionDto(
$context->getRequest()->request->get(EA::BATCH_ACTION_NAME),
$context->getRequest()->request->all()[EA::BATCH_ACTION_ENTITY_IDS] ?? [],
$context->getRequest()->request->get(EA::ENTITY_FQCN),
$referrerUrl,
$context->getRequest()->request->get(EA::BATCH_ACTION_CSRF_TOKEN)
);
}
$batchActionUrl = $context->getRequest()->request->get(EA::BATCH_ACTION_URL);
$batchActionUrlQueryString = parse_url($batchActionUrl, \PHP_URL_QUERY);
parse_str($batchActionUrlQueryString, $batchActionUrlParts);
$referrerUrl = $batchActionUrlParts[EA::REFERRER] ?? $this->adminUrlGenerator->unsetAll()->generateUrl();

yield new BatchActionDto(
$context->getRequest()->request->get(EA::BATCH_ACTION_NAME),
$context->getRequest()->request->all()[EA::BATCH_ACTION_ENTITY_IDS] ?? [],
$context->getRequest()->request->get(EA::ENTITY_FQCN),
$referrerUrl,
$context->getRequest()->request->get(EA::BATCH_ACTION_CSRF_TOKEN)
);
}
}

0 comments on commit 3b20674

Please sign in to comment.