Skip to content

Commit

Permalink
feat: add dedicated method to get id for existing redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
tinect committed Sep 12, 2024
1 parent b3be123 commit b9caa42
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Message/TinectRedirectUpdateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __invoke(TinectRedirectUpdateMessage $message): void
$redirectId = $message->getId();

if ($redirectId === null) {
$redirectId = $this->redirectFinderService->find($message->getSource(), $message->getSalesChannelDomainId())?->getId();
$redirectId = $this->redirectFinderService->findId($message->getSource(), $message->getSalesChannelDomainId());
}

if ($message->canCreateRedirect()) {
Expand Down
31 changes: 22 additions & 9 deletions src/Services/RedirectFinderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,28 @@ public function __construct(
public function find(string $path, ?string $salesChannelDomainId): ?RedirectEntity
{
$context = new Context(new SystemSource());
$criteria = (new Criteria())
$criteria = $this->getCriteria($path, $salesChannelDomainId);

$redirect = $this->tinectRedirectsRedirectRepository->search($criteria, $context)->first();

if ($redirect instanceof RedirectEntity) {
return $redirect;
}

return null;
}

public function findId(string $path, ?string $salesChannelDomainId): ?string
{
$context = new Context(new SystemSource());
$criteria = $this->getCriteria($path, $salesChannelDomainId);

return $this->tinectRedirectsRedirectRepository->searchIds($criteria, $context)->firstId();
}

private function getCriteria(string $path, ?string $salesChannelDomainId): Criteria
{
return (new Criteria())
->addFilter(new EqualsFilter('source', $path))
->addFilter(
new MultiFilter(MultiFilter::CONNECTION_OR, [
Expand All @@ -33,13 +54,5 @@ public function find(string $path, ?string $salesChannelDomainId): ?RedirectEnti
)
->addSorting(new FieldSorting('salesChannelDomainId', FieldSorting::DESCENDING))
->setLimit(1);

$redirect = $this->tinectRedirectsRedirectRepository->search($criteria, $context)->first();

if ($redirect instanceof RedirectEntity) {
return $redirect;
}

return null;
}
}

0 comments on commit b9caa42

Please sign in to comment.