Skip to content

Commit

Permalink
chore: use shorthand for constructor services
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed Feb 7, 2024
1 parent 1aff72e commit 750e337
Showing 1 changed file with 19 additions and 28 deletions.
47 changes: 19 additions & 28 deletions src/Api/Controller/GetWebResponseByPathController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use RZ\Roadiz\CoreBundle\Api\DataTransformer\WebResponseDataTransformerInterface;
use RZ\Roadiz\CoreBundle\Api\Model\WebResponseInterface;
use RZ\Roadiz\CoreBundle\Entity\Redirection;
use RZ\Roadiz\CoreBundle\Preview\PreviewResolverInterface;
use RZ\Roadiz\CoreBundle\Routing\PathResolverInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RequestStack;
Expand All @@ -19,27 +20,13 @@

final class GetWebResponseByPathController extends AbstractController
{
private RequestStack $requestStack;
private PathResolverInterface $pathResolver;
private WebResponseDataTransformerInterface $webResponseDataTransformer;
private IriConverterInterface $iriConverter;

/**
* @param RequestStack $requestStack
* @param PathResolverInterface $pathResolver
* @param WebResponseDataTransformerInterface $webResponseDataTransformer
* @param IriConverterInterface $iriConverter
*/
public function __construct(
RequestStack $requestStack,
PathResolverInterface $pathResolver,
WebResponseDataTransformerInterface $webResponseDataTransformer,
IriConverterInterface $iriConverter
private readonly RequestStack $requestStack,
private readonly PathResolverInterface $pathResolver,
private readonly WebResponseDataTransformerInterface $webResponseDataTransformer,
private readonly IriConverterInterface $iriConverter,
private readonly PreviewResolverInterface $previewResolver
) {
$this->requestStack = $requestStack;
$this->pathResolver = $pathResolver;
$this->webResponseDataTransformer = $webResponseDataTransformer;
$this->iriConverter = $iriConverter;
}

public function __invoke(): ?WebResponseInterface
Expand All @@ -54,9 +41,6 @@ public function __invoke(): ?WebResponseInterface
$resource = $this->normalizeResourcePath(
(string) $this->requestStack->getMainRequest()->query->get('path')
);
if (null === $resource) {
throw new ResourceNotFoundException('Resource not found');
}
$this->requestStack->getMainRequest()->attributes->set('data', $resource);
$this->requestStack->getMainRequest()->attributes->set('id', $resource->getId());
/*
Expand All @@ -72,9 +56,9 @@ public function __invoke(): ?WebResponseInterface

/**
* @param string $path
* @return PersistableInterface|null
* @return PersistableInterface
*/
protected function normalizeResourcePath(string $path): ?PersistableInterface
protected function normalizeResourcePath(string $path): PersistableInterface
{
/*
* Serve any PersistableInterface Resource by implementing
Expand All @@ -88,12 +72,19 @@ protected function normalizeResourcePath(string $path): ?PersistableInterface
);
$resource = $resourceInfo->getResource();

if (null === $resource) {
throw new ResourceNotFoundException('Cannot resolve resource path.');
}

/*
* Normalize redirection
*/
if ($resource instanceof Redirection) {
if (null !== $resource->getRedirectNodeSource()) {
$resource = $resource->getRedirectNodeSource();
if (null !== $nodeSource = $resource->getRedirectNodeSource()) {
if (!$this->previewResolver->isPreview() && !$nodeSource->getNode()->isPublished()) {
throw new ResourceNotFoundException('Cannot resolve resource path.');
}
$resource = $nodeSource;
} elseif (
null !== $resource->getRedirectUri() &&
(new UnicodeString($resource->getRedirectUri()))->startsWith('/')
Expand All @@ -114,10 +105,10 @@ protected function normalizeResourcePath(string $path): ?PersistableInterface
return $resource;
}

protected function addResourceToCacheTags(?PersistableInterface $resource): void
protected function addResourceToCacheTags(PersistableInterface $resource): void
{
$request = $this->requestStack->getMainRequest();
if (null !== $request && null !== $resource) {
if (null !== $request) {
$iri = $this->iriConverter->getIriFromResource($resource);
$request->attributes->set('_resources', $request->attributes->get('_resources', []) + [ $iri => $iri ]);
}
Expand Down

0 comments on commit 750e337

Please sign in to comment.