Skip to content

Commit

Permalink
frontend API: SEO categories are always correctly resolved in Categor…
Browse files Browse the repository at this point in the history
…yQuery
  • Loading branch information
TomasLudvik committed Sep 7, 2023
1 parent f8114f0 commit fd45b4e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
31 changes: 17 additions & 14 deletions app/src/FrontendApi/Resolver/Category/CategoryQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\Model\CategorySeo\ReadyCategorySeoMix;
use App\Model\CategorySeo\ReadyCategorySeoMixFacade;
use App\Model\Product\Flag\Flag;
use App\Model\Product\Parameter\Exception\ParameterValueNotFoundException;
use App\Model\Product\Parameter\ParameterFacade;
use GraphQL\Type\Definition\ResolveInfo;
use Overblog\GraphQLBundle\Definition\Argument;
Expand All @@ -22,6 +23,7 @@
use Shopsys\FrameworkBundle\Model\Category\Category as BaseCategory;
use Shopsys\FrameworkBundle\Model\Category\CategoryFacade;
use Shopsys\FrameworkBundle\Model\Category\Exception\CategoryNotFoundException;
use Shopsys\FrameworkBundle\Model\Product\Parameter\Exception\ParameterNotFoundException;
use Shopsys\FrontendApiBundle\Model\Error\InvalidArgumentUserError;
use Shopsys\FrontendApiBundle\Model\FriendlyUrl\FriendlyUrlFacade;
use Shopsys\FrontendApiBundle\Model\Resolver\Category\CategoryQuery as BaseCategoryQuery;
Expand Down Expand Up @@ -121,8 +123,13 @@ public function categoryOrSeoMixByUuidOrUrlSlugQuery(
throw new ReadyCategorySeoMixNotFoundUserError(sprintf('ReadyCategorySeoMix with URL slug "%s" does not exist.', $urlSlug));
}

if ($this->isSortingDifferentFromReadyCategorySeoMix($info, $readyCategorySeoMix) || $this->isFilterSet($info)) {
return $readyCategorySeoMix->getCategory();
$matchingReadyCategorySeoMix = $this->findMatchingReadyCategorySeoMix($info, $readyCategorySeoMix->getCategory());

if (
$matchingReadyCategorySeoMix !== $readyCategorySeoMix &&
($this->isFilterSet($info) || $this->isSortingDifferentFromReadyCategorySeoMix($info, $readyCategorySeoMix))
) {
return $matchingReadyCategorySeoMix ?? $readyCategorySeoMix->getCategory();
}

return $readyCategorySeoMix;
Expand Down Expand Up @@ -161,21 +168,17 @@ public function categoriesFilteredByProductFilterForFlagQuery(Argument $argument
private function findMatchingReadyCategorySeoMix(ResolveInfo $info, Category $category): ?ReadyCategorySeoMix
{
$variableValues = $info->variableValues;
$onlyInStock = $variableValues['filter']['onlyInStock'] ?? false;
$minimalPrice = $variableValues['filter']['minimalPrice'] ?? null;
$maximalPrice = $variableValues['filter']['maximalPrice'] ?? null;
$brandChoices = $variableValues['filter']['brands'] ?? [];

if ($onlyInStock || isset($minimalPrice) || isset($maximalPrice) || count($brandChoices) > 0) {
try {
return $this->readyCategorySeoMixFacade->findReadyCategorySeoMixByQueryInputData(
$category->getId(),
$variableValues['filter']['parameters'] ?? [],
$variableValues['filter']['flags'] ?? [],
$variableValues['orderingMode'] ?? ProductsQuery::getDefaultOrderingModeForListing(),
);
} catch (ParameterValueNotFoundException | ParameterNotFoundException) {
return null;
}

return $this->readyCategorySeoMixFacade->findReadyCategorySeoMixByQueryInputData(
$category->getId(),
$variableValues['filter']['parameters'] ?? [],
$variableValues['filter']['flags'] ?? [],
$variableValues['orderingMode'] ?? ProductsQuery::getDefaultOrderingModeForListing(),
);
}

/**
Expand Down
19 changes: 18 additions & 1 deletion app/src/Model/CategorySeo/ReadyCategorySeoMixFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;
use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\UrlListData;
use Shopsys\FrameworkBundle\Model\Product\Parameter\Exception\ParameterNotFoundException;
use Shopsys\FrameworkBundle\Model\Product\Parameter\Parameter;

class ReadyCategorySeoMixFacade
Expand Down Expand Up @@ -311,6 +312,13 @@ private function getParameterValueIdsByParameterId(array $parametersFilterData,
$parameterValueIdsByParameterId = [];

foreach ($parametersFilterData as $parameterFilterData) {
if (array_key_exists($parameterFilterData['parameter'], $parameterIdsByUuids) === false) {
throw new ParameterNotFoundException(sprintf(
'Parameter with uuid "%s" was not found',
$parameterFilterData['parameter'],
));
}

$parameterId = $parameterIdsByUuids[$parameterFilterData['parameter']];

if (count($parameterFilterData['values']) === 0) {
Expand All @@ -319,7 +327,16 @@ private function getParameterValueIdsByParameterId(array $parametersFilterData,
$text = $parameterFilterData['minimalValue'];
$parameterValueId = $this->parameterFacade->getParameterValueIdByText((string)$text, $currentLocale);
} else {
$parameterValueId = $parameterValueIdsByUuids[reset($parameterFilterData['values'])];
$parameterUuid = reset($parameterFilterData['values']);

if (array_key_exists($parameterUuid, $parameterValueIdsByUuids) === false) {
throw new ParameterValueNotFoundException(sprintf(
'Parameter value with uuid "%s" was not found',
$parameterUuid,
));
}

$parameterValueId = $parameterValueIdsByUuids[$parameterUuid];
}
$parameterValueIdsByParameterId[$parameterId] = $parameterValueId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public function testCategoryDataAreReturnedWhenSeoCategoryWithOrderingIsQueried(
$categoryPc = $this->getReference(CategoryDataFixture::CATEGORY_PC);
$categoryPcSlug = $this->urlGenerator->generate('front_product_list', ['id' => $categoryPc->getId()]);
$data = $this->getDataForCategorySeoMixPcNewWithUsb(__DIR__ . '/../../_graphql/query/ReadyCategorySeoMixQuery.graphql', [
'orderingMode' => 'PRIORITY',
'orderingMode' => 'NAME_ASC',
]);

$this->assertNull($data['originalCategorySlug']);
Expand Down

0 comments on commit fd45b4e

Please sign in to comment.