Skip to content

Commit

Permalink
Merge pull request #4 from mageworx/route_query
Browse files Browse the repository at this point in the history
added compatibility with "route" query
  • Loading branch information
mkostevich authored Feb 22, 2023
2 parents b013ccf + 1b99a9f commit 7ec696d
Show file tree
Hide file tree
Showing 7 changed files with 299 additions and 54 deletions.
113 changes: 88 additions & 25 deletions Model/CustomRedirectDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

namespace MageWorx\SeoRedirectsGraphQl\Model;

use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\UrlRewrite\Model\UrlFinderInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use Magento\UrlRewriteGraphQl\Model\DataProvider\EntityDataProviderComposite;
use MageWorx\SeoRedirects\Api\Data\CustomRedirectInterface;
use MageWorx\SeoRedirects\Helper\CustomRedirect\Data as CustomRedirectHelper;
use MageWorx\SeoRedirects\Model\Redirect\CustomRedirect;
Expand Down Expand Up @@ -54,29 +56,35 @@ class CustomRedirectDataProvider implements CustomRedirectDataProviderInterface
protected $idEncoder;

/**
* CustomRedirectDataProvider constructor.
*
* @var EntityDataProviderComposite
*/
protected $entityDataProviderComposite;

/**
* @param CustomRedirectHelper $customRedirectHelper
* @param CustomRedirectCollectionFactory $customRedirectCollectionFactory
* @param CustomRedirectTypeOptions $customRedirectTypeOptions
* @param RedirectTypeRewriteFragmentSource $redirectTypeRewriteFragmentSource
* @param UrlFinderInterface $urlFinder
* @param Uid $idEncoder
* @param EntityDataProviderComposite $entityDataProviderComposite
*/
public function __construct(
CustomRedirectHelper $customRedirectHelper,
CustomRedirectCollectionFactory $customRedirectCollectionFactory,
CustomRedirectTypeOptions $customRedirectTypeOptions,
RedirectTypeRewriteFragmentSource $redirectTypeRewriteFragmentSource,
UrlFinderInterface $urlFinder,
Uid $idEncoder
Uid $idEncoder,
EntityDataProviderComposite $entityDataProviderComposite
) {
$this->customRedirectHelper = $customRedirectHelper;
$this->customRedirectCollectionFactory = $customRedirectCollectionFactory;
$this->customRedirectTypeOptions = $customRedirectTypeOptions;
$this->redirectTypeRewriteFragmentSource = $redirectTypeRewriteFragmentSource;
$this->urlFinder = $urlFinder;
$this->idEncoder = $idEncoder;
$this->entityDataProviderComposite = $entityDataProviderComposite;
}

/**
Expand All @@ -91,6 +99,72 @@ public function getEntityUrlData(int $storeId, int $objectId, string $objectType
return null;
}

$customRedirect = $this->getCustomRedirect($storeId, $objectId, $objectType);

if (!$customRedirect || !$customRedirect->getId()) {
return null;
}

$urlRewrite = $this->getUrlRewrite($customRedirect);

if (!$urlRewrite) {
return null;
}

return [
'id' => $urlRewrite->getEntityId(),
'entity_uid' => $this->idEncoder->encode((string)$urlRewrite->getEntityId()),
'canonical_url' => $urlRewrite->getRequestPath(),
'relative_url' => $urlRewrite->getRequestPath(),
'redirectCode' => (int)$customRedirect->getRedirectCode(),
'redirect_code' => (int)$customRedirect->getRedirectCode(),
'type' => $this->sanitizeType($urlRewrite->getEntityType())
];
}

/**
* @param int $storeId
* @param int $objectId
* @param string $objectType
* @param ResolveInfo $info
* @return array|null
*/
public function getRouteData(int $storeId, int $objectId, string $objectType, ResolveInfo $info): ?array
{
if (!$this->customRedirectHelper->isEnabled($storeId)) {
return null;
}

$customRedirect = $this->getCustomRedirect($storeId, $objectId, $objectType);

if (!$customRedirect || !$customRedirect->getId()) {
return null;
}

$urlRewrite = $this->getUrlRewrite($customRedirect);

if (!$urlRewrite) {
return null;
}

$type = $this->sanitizeType($urlRewrite->getEntityType());
$result = $this->entityDataProviderComposite->getData($type, (int)$urlRewrite->getEntityId(), $info, $storeId);

$result['redirect_code'] = (int)$customRedirect->getRedirectCode();
$result['relative_url'] = $urlRewrite->getRequestPath();
$result['type'] = $type;

return $result;
}

/**
* @param int $storeId
* @param int $objectId
* @param string $objectType
* @return CustomRedirect|null
*/
protected function getCustomRedirect(int $storeId, int $objectId, string $objectType): ?CustomRedirect
{
$redirectTypes = $this->customRedirectTypeOptions->getTypes();

if (empty($redirectTypes[$objectType])) {
Expand All @@ -107,36 +181,25 @@ public function getEntityUrlData(int $storeId, int $objectId, string $objectType
->addFieldToFilter(CustomRedirectInterface::STATUS, CustomRedirect::STATUS_ENABLED)
->addDateRangeFilter();

/** @var \MageWorx\SeoRedirects\Model\Redirect\CustomRedirect $customRedirect */
$customRedirect = $customRedirectCollection->getFirstItem();

if (!$customRedirect->getId()) {
return null;
}
return $customRedirectCollection->getFirstItem();
}

/**
* @param CustomRedirect $customRedirect
* @return UrlRewrite|null
*/
protected function getUrlRewrite(CustomRedirect $customRedirect): ?UrlRewrite
{
$redirectTypeRewriteFragmentSource = $this->redirectTypeRewriteFragmentSource->toArray();

if (empty($redirectTypeRewriteFragmentSource[$customRedirect->getTargetEntityType()])) {
return null;
}

$targetPath = $redirectTypeRewriteFragmentSource[$customRedirect->getTargetEntityType(
)] . $customRedirect->getTargetEntityIdentifier();
$urlRewrite = $this->getRewriteByTargetPath($targetPath, (int)$customRedirect->getStoreId());

if (!$urlRewrite) {
return null;
}
$targetPath = $redirectTypeRewriteFragmentSource[$customRedirect->getTargetEntityType()]
. $customRedirect->getTargetEntityIdentifier();

return [
'id' => $urlRewrite->getEntityId(),
'entity_uid' => $this->idEncoder->encode((string)$urlRewrite->getEntityId()),
'canonical_url' => $urlRewrite->getRequestPath(),
'relative_url' => $urlRewrite->getRequestPath(),
'redirectCode' => (int)$customRedirect->getRedirectCode(),
'redirect_code' => (int)$customRedirect->getRedirectCode(),
'type' => $this->sanitizeType($urlRewrite->getEntityType())
];
return $this->getRewriteByTargetPath($targetPath, (int)$customRedirect->getStoreId());
}

/**
Expand Down
11 changes: 11 additions & 0 deletions Model/CustomRedirectDataProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace MageWorx\SeoRedirectsGraphQl\Model;

use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

interface CustomRedirectDataProviderInterface
{
/**
Expand All @@ -17,4 +19,13 @@ interface CustomRedirectDataProviderInterface
* @return array|null
*/
public function getEntityUrlData(int $storeId, int $objectId, string $objectType): ?array;

/**
* @param int $storeId
* @param int $objectId
* @param string $objectType
* @param ResolveInfo $info
* @return array|null
*/
public function getRouteData(int $storeId, int $objectId, string $objectType, ResolveInfo $info): ?array;
}
124 changes: 96 additions & 28 deletions Model/DpRedirectDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
use Magento\Framework\DataObject;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\Uid;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\UrlRewrite\Model\UrlFinderInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use Magento\UrlRewriteGraphQl\Model\DataProvider\EntityDataProviderComposite;
use Magento\UrlRewriteGraphQl\Model\Resolver\UrlRewrite\CustomUrlLocatorInterface;
use MageWorx\SeoRedirects\Helper\DpRedirect\Data as DpRedirectHelper;
use MageWorx\SeoRedirects\Model\Redirect\DpRedirect;
Expand Down Expand Up @@ -55,29 +57,35 @@ class DpRedirectDataProvider implements DpRedirectDataProviderInterface
protected $idEncoder;

/**
* DpRedirectDataProvider constructor.
*
* @var EntityDataProviderComposite
*/
protected $entityDataProviderComposite;

/**
* @param DpRedirectHelper $dpRedirectHelper
* @param DpRedirectCollectionFactory $dpRedirectCollectionFactory
* @param CategoryCollectionFactory $categoryCollectionFactory
* @param CustomUrlLocatorInterface $customUrlLocator
* @param UrlFinderInterface $urlFinder
* @param Uid $idEncoder
* @param EntityDataProviderComposite $entityDataProviderComposite
*/
public function __construct(
DpRedirectHelper $dpRedirectHelper,
DpRedirectCollectionFactory $dpRedirectCollectionFactory,
CategoryCollectionFactory $categoryCollectionFactory,
CustomUrlLocatorInterface $customUrlLocator,
UrlFinderInterface $urlFinder,
Uid $idEncoder
Uid $idEncoder,
EntityDataProviderComposite $entityDataProviderComposite
) {
$this->dpRedirectHelper = $dpRedirectHelper;
$this->dpRedirectCollectionFactory = $dpRedirectCollectionFactory;
$this->categoryCollectionFactory = $categoryCollectionFactory;
$this->customUrlLocator = $customUrlLocator;
$this->urlFinder = $urlFinder;
$this->idEncoder = $idEncoder;
$this->entityDataProviderComposite = $entityDataProviderComposite;
}

/**
Expand All @@ -92,26 +100,80 @@ public function getEntityUrlData(int $storeId, string $requestPath): ?array
return null;
}

if (substr($requestPath, 0, 1) === '/' && $requestPath !== '/') {
$requestPath = ltrim($requestPath, '/');
$dpRedirect = $this->getDpRedirect($storeId, $requestPath);

if (!$dpRedirect->getCategoryId()) {
return null;
}

$customUrl = $this->customUrlLocator->locateUrl($requestPath);
$requestPath = $customUrl ?: $requestPath;
$urlRewrite = $this->getUrlRewrite($storeId, $dpRedirect);

/** @var \MageWorx\SeoRedirects\Model\ResourceModel\Redirect\DpRedirect\Collection $dpRedirectCollection */
$dpRedirectCollection = $this->dpRedirectCollectionFactory->create();
$dpRedirectCollection
->addStoreFilter($storeId)
->addRequestPathsFilter($requestPath);
if (!$urlRewrite) {
return null;
}

$dpRedirect->setHits($dpRedirect->getHits() + 1);
$dpRedirect->save();

return [
'id' => $urlRewrite->getEntityId(),
'entity_uid' => $this->idEncoder->encode((string)$urlRewrite->getEntityId()),
'canonical_url' => $urlRewrite->getRequestPath(),
'relative_url' => $urlRewrite->getRequestPath(),
'redirect_code' => (int)$this->dpRedirectHelper->getRedirectType(),
'redirectCode' => (int)$this->dpRedirectHelper->getRedirectType(),
'type' => $this->sanitizeType($urlRewrite->getEntityType())
];
}

/** @var DpRedirect $dpRedirect */
$dpRedirect = $dpRedirectCollection->getFirstItem();
/**
* @param int $storeId
* @param string $requestPath
* @param ResolveInfo $info
* @return array|null
* @throws GraphQlNoSuchEntityException
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getRouteData(int $storeId, string $requestPath, ResolveInfo $info): ?array
{
if (!$this->dpRedirectHelper->isEnabled($storeId)) {
return null;
}

$dpRedirect = $this->getDpRedirect($storeId, $requestPath);

if (!$dpRedirect->getCategoryId()) {
return null;
}

$urlRewrite = $this->getUrlRewrite($storeId, $dpRedirect);

if (!$urlRewrite) {
return null;
}

$dpRedirect->setHits($dpRedirect->getHits() + 1);
$dpRedirect->save();

$type = $this->sanitizeType($urlRewrite->getEntityType());
$result = $this->entityDataProviderComposite->getData($type, (int)$urlRewrite->getEntityId(), $info, $storeId);

$result['redirect_code'] = (int)$this->dpRedirectHelper->getRedirectType();
$result['relative_url'] = $urlRewrite->getRequestPath();
$result['type'] = $type;

return $result;
}

/**
* @param int $storeId
* @param DpRedirect $dpRedirect
* @return UrlRewrite|null
* @throws GraphQlNoSuchEntityException
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function getUrlRewrite(int $storeId, DpRedirect $dpRedirect)
{
$categoryIds = array_unique([$dpRedirect->getCategoryId(), $dpRedirect->getPriorityCategoryId()]);
$collection = $this->categoryCollectionFactory->create();
$collection
Expand All @@ -125,30 +187,36 @@ public function getEntityUrlData(int $storeId, string $requestPath): ?array
return null;
}

$urlRewrite = $this->urlFinder->findOneByData(
return $this->urlFinder->findOneByData(
[
UrlRewrite::ENTITY_ID => $category->getId(),
UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE,
UrlRewrite::STORE_ID => $storeId,
]
);
}

if (!$urlRewrite) {
return null;
/**
* @param int $storeId
* @param string $requestPath
* @return DpRedirect
*/
protected function getDpRedirect(int $storeId, string $requestPath): DpRedirect
{
if (substr($requestPath, 0, 1) === '/' && $requestPath !== '/') {
$requestPath = ltrim($requestPath, '/');
}

$dpRedirect->setHits($dpRedirect->getHits() + 1);
$dpRedirect->save();
$customUrl = $this->customUrlLocator->locateUrl($requestPath);
$requestPath = $customUrl ?: $requestPath;

return [
'id' => $urlRewrite->getEntityId(),
'entity_uid' => $this->idEncoder->encode((string)$urlRewrite->getEntityId()),
'canonical_url' => $urlRewrite->getRequestPath(),
'relative_url' => $urlRewrite->getRequestPath(),
'redirect_code' => (int)$this->dpRedirectHelper->getRedirectType(),
'redirectCode' => (int)$this->dpRedirectHelper->getRedirectType(),
'type' => $this->sanitizeType($urlRewrite->getEntityType())
];
/** @var \MageWorx\SeoRedirects\Model\ResourceModel\Redirect\DpRedirect\Collection $dpRedirectCollection */
$dpRedirectCollection = $this->dpRedirectCollectionFactory->create();
$dpRedirectCollection
->addStoreFilter($storeId)
->addRequestPathsFilter($requestPath);

return $dpRedirectCollection->getFirstItem();
}

/**
Expand Down
Loading

0 comments on commit 7ec696d

Please sign in to comment.