Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/refactor #41

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions src/Message/TinectRedirectUpdateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,43 @@
use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\RetryableQuery;
use Shopware\Core\Framework\Uuid\Uuid;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Tinect\Redirects\Services\ExcludedService;
use Tinect\Redirects\Services\RedirectFinderService;

#[AsMessageHandler]
readonly class TinectRedirectUpdateHandler
{
public function __construct(
private Connection $connection,
private ExcludedService $excludedService,
private RedirectFinderService $redirectFinderService
) {
}

public function __invoke(TinectRedirectUpdateMessage $message): void
{
if ($this->excludedService->isExcluded($message->getSource(), $message->getSalesChannelDomainId())) {
return;
}

$redirectId = $message->getId();

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

if ($message->canCreateRedirect()) {
$redirectId = $this->createOrUpdateRedirect($message, $redirectId);
} elseif ($redirectId !== null) {
$this->updateRedirectCount($redirectId);
}

if (\is_string($redirectId)) {
if ($redirectId !== null) {
$this->createRedirectRequest($message, $redirectId);
}
}

private function createOrUpdateRedirect(TinectRedirectUpdateMessage $message): string
private function createOrUpdateRedirect(TinectRedirectUpdateMessage $message, ?string $redirectId): string
{
$query = new RetryableQuery(
$this->connection,
Expand All @@ -44,8 +56,12 @@ private function createOrUpdateRedirect(TinectRedirectUpdateMessage $message): s
)
);

if (!\is_string($redirectId)) {
$redirectId = Uuid::randomHex();
}

$params = [
'id' => \is_string($message->getId()) ? Uuid::fromHexToBytes($message->getId()) : Uuid::randomBytes(),
'id' => Uuid::fromHexToBytes($redirectId),
'source' => $message->getSource(),
'salesChannelDomainId' => null,
'createdAt' => $message->getCreatedAt()->format(Defaults::STORAGE_DATE_TIME_FORMAT),
Expand All @@ -60,12 +76,8 @@ private function createOrUpdateRedirect(TinectRedirectUpdateMessage $message): s
return Uuid::fromBytesToHex($params['id']);
}

private function updateRedirectCount(TinectRedirectUpdateMessage $message): void
private function updateRedirectCount(string $redirectId): void
{
if (!\is_string($message->getId())) {
return;
}

$query = new RetryableQuery(
$this->connection,
$this->connection->prepare(
Expand All @@ -74,7 +86,7 @@ private function updateRedirectCount(TinectRedirectUpdateMessage $message): void
);

$params = [
'id' => Uuid::fromHexToBytes($message->getId()),
'id' => Uuid::fromHexToBytes($redirectId),
];

$query->execute($params);
Expand Down
2 changes: 1 addition & 1 deletion src/Message/TinectRedirectUpdateMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function getReferer(): ?string
return substr($this->referer, 0, RedirectRequestDefinition::MAX_LENGTH_REFERER);
}

public function isCreateRedirect(): bool
public function canCreateRedirect(): bool
{
return $this->createRedirect;
}
Expand Down
45 changes: 45 additions & 0 deletions src/Services/RedirectFinderService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Tinect\Redirects\Services;

use Shopware\Core\Framework\Api\Context\SystemSource;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Tinect\Redirects\Content\Redirect\RedirectEntity;

readonly class RedirectFinderService
{
public function __construct(
private EntityRepository $tinectRedirectsRedirectRepository,
) {
}

public function find(string $path, ?string $salesChannelDomainId): ?RedirectEntity
{
$context = new Context(new SystemSource());
$criteria = (new Criteria())
->addFilter(new EqualsFilter('source', $path))
->addFilter(
new MultiFilter(MultiFilter::CONNECTION_OR, [
new EqualsFilter('salesChannelDomainId', $salesChannelDomainId),
new EqualsFilter('salesChannelDomainId', null),
])
)
->addSorting(new FieldSorting('salesChannelDomainId', FieldSorting::DESCENDING))
->setLimit(1);

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

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

return null;
}
}
38 changes: 6 additions & 32 deletions src/Subscriber/BeforeSendResponseSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@
namespace Tinect\Redirects\Subscriber;

use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
use Shopware\Core\Framework\Api\Context\SystemSource;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\PlatformRequest;
use Shopware\Core\SalesChannelRequest;
use Shopware\Core\System\SalesChannel\Context\AbstractSalesChannelContextFactory;
Expand All @@ -29,21 +22,19 @@
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Messenger\MessageBusInterface;
use Tinect\Redirects\Content\Redirect\RedirectEntity;
use Tinect\Redirects\Message\TinectRedirectUpdateMessage;
use Tinect\Redirects\Services\ExcludedService;
use Tinect\Redirects\Services\RedirectFinderService;

readonly class BeforeSendResponseSubscriber implements EventSubscriberInterface
{
public function __construct(
private EntityRepository $tinectRedirectsRedirectRepository,
private SeoUrlPlaceholderHandlerInterface $seoUrlPlaceholderHandler,
#[Autowire(service: SalesChannelContextFactory::class)]
private AbstractSalesChannelContextFactory $salesChannelContextFactory,
private SystemConfigService $systemConfigService,
private MessageBusInterface $messageBus,
private RequestTransformer $requestTransformer,
private ExcludedService $excludedService
private RedirectFinderService $redirectFinderService
) {
}

Expand Down Expand Up @@ -102,27 +93,14 @@ private function handleRequest(Request $request): ?Response
$salesChannelDomainId = null;
}

$context = new Context(new SystemSource());
$criteria = (new Criteria())
->addFilter(new EqualsFilter('source', $path))
->addFilter(
new MultiFilter(MultiFilter::CONNECTION_OR, [
new EqualsFilter('salesChannelDomainId', $salesChannelDomainId),
new EqualsFilter('salesChannelDomainId', null),
])
)
->addSorting(new FieldSorting('salesChannelDomainId', FieldSorting::DESCENDING))
->setLimit(1);

/** @var ?RedirectEntity $redirect */
$redirect = $this->tinectRedirectsRedirectRepository->search($criteria, $context)->first();
$redirect = $this->redirectFinderService->find($path, $salesChannelDomainId);

$message = new TinectRedirectUpdateMessage(
source: $path,
salesChannelDomainId: $salesChannelDomainId,
ipAddress: $this->getIpAddress($request),
userAgent: $request->headers->get('User-Agent') ?? '',
createRedirect: $this->canCreateRedirect($path, $salesChannelDomainId),
createRedirect: $this->canCreateRedirect($salesChannelDomainId),
id: $redirect?->getId(),
referer: $request->headers->get('referer'),
);
Expand Down Expand Up @@ -210,12 +188,8 @@ private function canSaveIpAddress(?string $salesChannelId): bool
return $this->systemConfigService->getBool('TinectRedirects.config.saveIpAddresses', $salesChannelId);
}

private function canCreateRedirect(string $path, ?string $salesChannelId): bool
private function canCreateRedirect(?string $salesChannelId): bool
{
if (!$this->systemConfigService->getBool('TinectRedirects.config.createNewRedirects', $salesChannelId)) {
return false;
}

return !$this->excludedService->isExcluded($path, $salesChannelId);
return $this->systemConfigService->getBool('TinectRedirects.config.createNewRedirects', $salesChannelId);
}
}
Loading