Skip to content

Commit

Permalink
Merge pull request #7 from mageworx/update_support_of_route_query
Browse files Browse the repository at this point in the history
Fix support of the ROUTE query
  • Loading branch information
SiarheyUchukhlebau authored Sep 5, 2024
2 parents 32fc23b + fe5ec5a commit 21c228c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 42 deletions.
3 changes: 2 additions & 1 deletion Model/CustomRedirectDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ public function getUrlRewriteDataByCustomRedirectEntity(CustomRedirectInterface
'relative_url' => $urlRewrite->getRequestPath(),
'redirectCode' => (int)$customRedirect->getRedirectCode(),
'redirect_code' => (int)$customRedirect->getRedirectCode(),
'type' => $this->sanitizeType($urlRewrite->getEntityType())
'type' => $this->sanitizeType($urlRewrite->getEntityType()),
'type_id' => $this->sanitizeType($urlRewrite->getEntityType()),
];
}

Expand Down
9 changes: 9 additions & 0 deletions Model/CustomRedirectDataProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace MageWorx\SeoRedirectsGraphQl\Model;

use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use MageWorx\SeoRedirects\Api\Data\CustomRedirectInterface;

interface CustomRedirectDataProviderInterface
{
Expand All @@ -28,4 +29,12 @@ public function getEntityUrlData(int $storeId, int $objectId, string $objectType
* @return array|null
*/
public function getRouteData(int $storeId, int $objectId, string $objectType, ResolveInfo $info): ?array;

/**
* Get entity URL data by the CustomRedirect entity.
*
* @param CustomRedirectInterface $customRedirect
* @return array|null
*/
public function getEntityUrlDataByCustomRedirectEntity(CustomRedirectInterface $customRedirect): ?array;
}
112 changes: 72 additions & 40 deletions Plugin/ModifyRouteDataPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,32 @@
* See LICENSE.txt for license details.
*/

declare(strict_types=1);
declare(strict_types = 1);

namespace MageWorx\SeoRedirectsGraphQl\Plugin;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use MageWorx\SeoRedirectsGraphQl\Model\DpRedirectDataProviderInterface;
use MageWorx\SeoRedirects\Model\Redirect\CustomRedirectFinder;
use MageWorx\SeoRedirectsGraphQl\Model\CustomRedirectDataProviderInterface;
use MageWorx\SeoRedirectsGraphQl\Model\DpRedirectDataProviderInterface;

class ModifyRouteDataPlugin
{
/**
* @var DpRedirectDataProviderInterface
*/
protected $dpRedirectDataProvider;

/**
* @var CustomRedirectDataProviderInterface
*/
protected $customRedirectDataProvider;
protected DpRedirectDataProviderInterface $dpRedirectDataProvider;
protected CustomRedirectDataProviderInterface $customRedirectDataProvider;
protected CustomRedirectFinder $customRedirectFinder;

/**
* ModifyEntityUrlDataPlugin constructor.
*
* @param DpRedirectDataProviderInterface $dpRedirectDataProvider
* @param CustomRedirectDataProviderInterface $customRedirectDataProvider
*/
public function __construct(
DpRedirectDataProviderInterface $dpRedirectDataProvider,
CustomRedirectDataProviderInterface $customRedirectDataProvider
DpRedirectDataProviderInterface $dpRedirectDataProvider,
CustomRedirectDataProviderInterface $customRedirectDataProvider,
CustomRedirectFinder $customRedirectFinder
) {
$this->dpRedirectDataProvider = $dpRedirectDataProvider;
$this->customRedirectDataProvider = $customRedirectDataProvider;
$this->customRedirectFinder = $customRedirectFinder;
}

/**
Expand All @@ -53,38 +44,79 @@ public function __construct(
*/
public function afterResolve(
\Magento\UrlRewriteGraphQl\Model\Resolver\Route $subject,
$result,
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
$result,
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
if ($result === null) {
$result = [];
}

if (empty($result)) {
if (!isset($args['url']) || empty(trim($args['url']))) {
return $result;
if (!isset($args['url']) || empty(trim($args['url']))) { // @TODO: Check it
return null;
}

$newData = $this->dpRedirectDataProvider->getRouteData($storeId, $args['url'], $info);
} else {
$objectId = empty($result['id']) ? null : $result['id'];
$objectId = $objectId ?: (empty($result['entity_id']) ? null : $result['entity_id']);
$objectId = $objectId ?: (empty($result['page_id']) ? null : $result['page_id']);

if (empty($objectId) || empty($result['type'])) {
return $result;
if (!empty($newData)) {
return $newData;
}
}

// Regular custom redirect
if ($this->resultHasId($result) && !empty($result['type'])) {
$objectId = $this->extractIdFromResult($result);
if ($objectId !== null) {
$newData = $this->customRedirectDataProvider->getRouteData(
$storeId,
(int)$objectId,
$result['type'],
$info
);

if (!empty($newData)) {
return $newData;
}
}
}

// Trying to locate custom redirect from custom redirect to custom redirect, so much custom
$customRedirect = $this->customRedirectFinder->getRedirectByPath($args['url'], [], $storeId);
if ($customRedirect === null || $customRedirect->getId() === null) {
return empty($result) ? null : $result;
}

$urlRewriteData = $this->customRedirectDataProvider->getEntityUrlDataByCustomRedirectEntity($customRedirect);

return empty($urlRewriteData) ? null : $urlRewriteData;
}

/**
* Is id in some form exists in the result array
*
* @param array $result
* @return bool
*/
public function resultHasId(array $result): bool
{
return !empty($result['id']) || !empty($result['entity_id']) || !empty($result['page_id']);
}

public function extractIdFromResult(array $result): ?int
{
$objectId = empty($result['id']) ? null : $result['id'];
$objectId = $objectId ?: (empty($result['entity_id']) ? null : $result['entity_id']);
$objectId = $objectId ?: (empty($result['page_id']) ? null : $result['page_id']);

$newData = $this->customRedirectDataProvider->getRouteData(
$storeId,
(int)$objectId,
$result['type'],
$info
);
if ($objectId !== null) {
$objectId = (int)$objectId;
}

return empty($newData) ? $result : $newData;
return $objectId;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
"MageWorx\\SeoRedirectsGraphQl\\": ""
}
},
"version": "1.3.0"
"version": "1.3.1"
}

0 comments on commit 21c228c

Please sign in to comment.