Skip to content

Commit

Permalink
[shopsys] added hreflang feature (#2970)
Browse files Browse the repository at this point in the history
  • Loading branch information
grossmannmartin authored Jan 23, 2024
2 parents 5c352c8 + 525fbb1 commit d238091
Show file tree
Hide file tree
Showing 21 changed files with 246 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Model/Blog/Article/BlogArticleResolverMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ protected function map(): array
'link' => static function (array $blogArticleData) {
return $blogArticleData['url'];
},
'hreflangLinks' => function (array $blogArticleData) {
return $blogArticleData['hreflangLinks'];
},
],
];
}
Expand Down
6 changes: 6 additions & 0 deletions src/Model/Blog/Category/BlogCategoryResolverMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Shopsys\FrameworkBundle\Model\Blog\Article\Elasticsearch\BlogArticleElasticsearchFacade;
use Shopsys\FrameworkBundle\Model\Blog\Category\BlogCategory;
use Shopsys\FrameworkBundle\Model\Blog\Category\BlogCategoryFacade;
use Shopsys\FrameworkBundle\Model\Seo\HreflangLinksFacade;

class BlogCategoryResolverMap extends ResolverMap
{
Expand All @@ -18,12 +19,14 @@ class BlogCategoryResolverMap extends ResolverMap
* @param \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlFacade $friendlyUrlFacade
* @param \Shopsys\FrameworkBundle\Model\Blog\Category\BlogCategoryFacade $blogCategoryFacade
* @param \Shopsys\FrameworkBundle\Model\Blog\Article\Elasticsearch\BlogArticleElasticsearchFacade $blogArticleElasticsearchFacade
* @param \Shopsys\FrameworkBundle\Model\Seo\HreflangLinksFacade $hreflangLinksFacade
*/
public function __construct(
protected readonly Domain $domain,
protected readonly FriendlyUrlFacade $friendlyUrlFacade,
protected readonly BlogCategoryFacade $blogCategoryFacade,
protected readonly BlogArticleElasticsearchFacade $blogArticleElasticsearchFacade,
protected readonly HreflangLinksFacade $hreflangLinksFacade,
) {
}

Expand Down Expand Up @@ -69,6 +72,9 @@ protected function map(): array
'articlesTotalCount' => function (BlogCategory $blogCategory) {
return $this->blogArticleElasticsearchFacade->getByBlogCategoryTotalCount($blogCategory);
},
'hreflangLinks' => function (BlogCategory $blogCategory) {
return $this->hreflangLinksFacade->getForBlogCategory($blogCategory, $this->domain->getId());
},
],
];
}
Expand Down
6 changes: 6 additions & 0 deletions src/Model/Resolver/Brand/BrandResolverMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@
use Overblog\GraphQLBundle\Resolver\ResolverMap;
use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Model\Product\Brand\Brand;
use Shopsys\FrameworkBundle\Model\Seo\HreflangLinksFacade;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class BrandResolverMap extends ResolverMap
{
/**
* @param \Symfony\Component\Routing\Generator\UrlGeneratorInterface $urlGenerator
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
* @param \Shopsys\FrameworkBundle\Model\Seo\HreflangLinksFacade $hreflangLinksFacade
*/
public function __construct(
protected readonly UrlGeneratorInterface $urlGenerator,
protected readonly Domain $domain,
protected readonly HreflangLinksFacade $hreflangLinksFacade,
) {
}

Expand All @@ -44,6 +47,9 @@ protected function map(): array
'seoH1' => function (Brand $brand) {
return $brand->getSeoH1($this->domain->getId());
},
'hreflangLinks' => function (Brand $brand) {
return $this->hreflangLinksFacade->getForBrand($brand, $this->domain->getId());
},
],
];
}
Expand Down
11 changes: 9 additions & 2 deletions src/Model/Resolver/Category/CategoryResolverMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
use Overblog\GraphQLBundle\Resolver\ResolverMap;
use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Model\Category\Category;
use Shopsys\FrameworkBundle\Model\Seo\HreflangLinksFacade;

class CategoryResolverMap extends ResolverMap
{
/**
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
* @param \Shopsys\FrameworkBundle\Model\Seo\HreflangLinksFacade $hreflangLinksFacade
*/
public function __construct(protected readonly Domain $domain)
{
public function __construct(
protected readonly Domain $domain,
protected readonly HreflangLinksFacade $hreflangLinksFacade,
) {
}

/**
Expand All @@ -33,6 +37,9 @@ protected function map(): array
'seoMetaDescription' => function (Category $category) {
return $category->getSeoMetaDescription($this->domain->getId());
},
'hreflangLinks' => function (Category $category) {
return $this->hreflangLinksFacade->getForCategory($category, $this->domain->getId());
},
],
];
}
Expand Down
24 changes: 24 additions & 0 deletions src/Model/Resolver/Image/SeoPageImagesQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrontendApiBundle\Model\Resolver\Image;

use GraphQL\Executor\Promise\Promise;
use Shopsys\FrameworkBundle\Model\Seo\Page\SeoPage;
use Shopsys\FrameworkBundle\Model\Seo\Page\SeoPageFacade;

class SeoPageImagesQuery extends ImagesQuery
{
/**
* @param \Shopsys\FrameworkBundle\Model\Seo\Page\SeoPage $seoPage
* @return \GraphQL\Executor\Promise\Promise
*/
public function ogImageBySeoPageQuery(SeoPage $seoPage): Promise
{
return $this->mainImageByEntityPromiseQuery(
$seoPage,
SeoPageFacade::IMAGE_TYPE_OG,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,13 @@ public function getMainVariant(array $data): array
{
return $this->productElasticsearchProvider->getVisibleProductArrayById($data['main_variant_id']);
}

/**
* @param array $data
* @return array
*/
public function getHreflangLinks(array $data): array
{
return $data['hreflang_links'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Shopsys\FrameworkBundle\Model\Product\Availability\ProductAvailabilityFacade;
use Shopsys\FrameworkBundle\Model\Product\Collection\ProductCollectionFacade;
use Shopsys\FrameworkBundle\Model\Product\Product;
use Shopsys\FrameworkBundle\Model\Seo\HreflangLinksFacade;
use Shopsys\FrontendApiBundle\Model\Parameter\ParameterWithValuesFactory;
use Shopsys\FrontendApiBundle\Model\Product\ProductAccessoryFacade;

Expand All @@ -21,6 +22,7 @@ class ProductEntityFieldMapper
* @param \Shopsys\FrameworkBundle\Model\Customer\User\CurrentCustomerUser $currentCustomerUser
* @param \Shopsys\FrontendApiBundle\Model\Parameter\ParameterWithValuesFactory $parameterWithValuesFactory
* @param \Shopsys\FrameworkBundle\Model\Product\Availability\ProductAvailabilityFacade $productAvailabilityFacade
* @param \Shopsys\FrameworkBundle\Model\Seo\HreflangLinksFacade $hreflangLinksFacade
*/
public function __construct(
protected readonly Domain $domain,
Expand All @@ -29,6 +31,7 @@ public function __construct(
protected readonly CurrentCustomerUser $currentCustomerUser,
protected readonly ParameterWithValuesFactory $parameterWithValuesFactory,
protected readonly ProductAvailabilityFacade $productAvailabilityFacade,
protected readonly HreflangLinksFacade $hreflangLinksFacade,
) {
}

Expand Down Expand Up @@ -157,4 +160,13 @@ public function getOrderingPriority(Product $product): int
{
return $product->getOrderingPriority($this->domain->getId());
}

/**
* @param \Shopsys\FrameworkBundle\Model\Product\Product $product
* @return \Shopsys\FrameworkBundle\Model\Seo\HreflangLink[]
*/
public function getHreflangLinks(Product $product): array
{
return $this->hreflangLinksFacade->getForProduct($product, $this->domain->getId());
}
}
21 changes: 21 additions & 0 deletions src/Model/Resolver/SeoPage/Exception/SeoPageNotFoundUserError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrontendApiBundle\Model\Resolver\SeoPage\Exception;

use Shopsys\FrontendApiBundle\Model\Error\EntityNotFoundUserError;
use Shopsys\FrontendApiBundle\Model\Error\UserErrorWithCodeInterface;

class SeoPageNotFoundUserError extends EntityNotFoundUserError implements UserErrorWithCodeInterface
{
protected const CODE = 'seo-page-not-found';

/**
* {@inheritdoc}
*/
public function getUserErrorCode(): string
{
return static::CODE;
}
}
44 changes: 44 additions & 0 deletions src/Model/Resolver/SeoPage/SeoPageQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrontendApiBundle\Model\Resolver\SeoPage;

use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Model\Seo\Page\Exception\SeoPageNotFoundException;
use Shopsys\FrameworkBundle\Model\Seo\Page\SeoPage;
use Shopsys\FrameworkBundle\Model\Seo\Page\SeoPageFacade;
use Shopsys\FrameworkBundle\Model\Seo\Page\SeoPageSlugTransformer;
use Shopsys\FrontendApiBundle\Model\Resolver\AbstractQuery;
use Shopsys\FrontendApiBundle\Model\Resolver\SeoPage\Exception\SeoPageNotFoundUserError;

class SeoPageQuery extends AbstractQuery
{
/**
* @param \Shopsys\FrameworkBundle\Model\Seo\Page\SeoPageFacade $seoPageFacade
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
*/
public function __construct(
protected readonly SeoPageFacade $seoPageFacade,
protected readonly Domain $domain,
) {
}

/**
* @param string $pageSlug
* @return \Shopsys\FrameworkBundle\Model\Seo\Page\SeoPage
*/
public function seoPageByPageSlugQuery(string $pageSlug): SeoPage
{
$domainId = $this->domain->getId();

try {
$slug = SeoPageSlugTransformer::transformFriendlyUrlToSeoPageSlug($pageSlug);
$seoPage = $this->seoPageFacade->getByDomainIdAndPageSlug($domainId, $slug);
} catch (SeoPageNotFoundException $seoPageNotFoundException) {
throw new SeoPageNotFoundUserError($seoPageNotFoundException->getMessage());
}

return $seoPage;
}
}
40 changes: 40 additions & 0 deletions src/Model/Resolver/SeoPage/SeoPageResolverMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrontendApiBundle\Model\Resolver\SeoPage;

use Overblog\GraphQLBundle\Resolver\ResolverMap;
use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Model\Seo\HreflangLinksFacade;
use Shopsys\FrameworkBundle\Model\Seo\Page\SeoPage;

class SeoPageResolverMap extends ResolverMap
{
/**
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
* @param \Shopsys\FrameworkBundle\Model\Seo\HreflangLinksFacade $hreflangLinksFacade
*/
public function __construct(
protected readonly Domain $domain,
protected readonly HreflangLinksFacade $hreflangLinksFacade,
) {
}

/**
* @return array
*/
protected function map(): array
{
return [
'SeoPage' => [
'title' => fn (SeoPage $seoPage) => $seoPage->getSeoTitle($this->domain->getId()),
'metaDescription' => fn (SeoPage $seoPage) => $seoPage->getSeoMetaDescription($this->domain->getId()),
'canonicalUrl' => fn (SeoPage $seoPage) => $seoPage->getCanonicalUrl($this->domain->getId()),
'ogTitle' => fn (SeoPage $seoPage) => $seoPage->getSeoOgTitle($this->domain->getId()),
'ogDescription' => fn (SeoPage $seoPage) => $seoPage->getSeoOgDescription($this->domain->getId()),
'hreflangLinks' => fn (SeoPage $seoPage) => $this->hreflangLinksFacade->getForSeoPage($seoPage, $this->domain->getId()),
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ BlogArticleDecorator:
- 'Breadcrumb'
- 'Slug'
- 'ArticleInterface'
- 'Hreflang'
fields:
id:
type: "Int!"
Expand Down Expand Up @@ -68,3 +69,6 @@ BlogArticleDecorator:
type:
type: "String"
defaultValue: null
hreflangLinks:
type: "[HreflangLink!]!"
description: "Alternate links for hreflang meta tags"
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ BlogCategoryDecorator:
interfaces:
- 'Breadcrumb'
- 'Slug'
- 'Hreflang'
fields:
uuid:
type: "Uuid!"
Expand Down Expand Up @@ -51,3 +52,6 @@ BlogCategoryDecorator:
articlesTotalCount:
type: "Int!"
description: "Total count of blog articles in this category"
hreflangLinks:
type: "[HreflangLink!]!"
description: "Alternate links for hreflang meta tags"
5 changes: 5 additions & 0 deletions src/Resources/config/graphql-types/BrandDecorator.types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ BrandDecorator:
type: object
decorator: true
config:
interfaces:
- 'Hreflang'
description: "Represents a brand"
fields:
uuid:
Expand Down Expand Up @@ -41,3 +43,6 @@ BrandDecorator:
config:
orderingModeType: 'ProductOrderingModeEnum'
resolve: '@=query("productsByBrandQuery", args, value)'
hreflangLinks:
type: "[HreflangLink!]!"
description: "Alternate links for hreflang meta tags"
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ CategoryDecorator:
categoryHierarchy:
type: "[CategoryHierarchyItem!]!"
description: "All parent category names with their IDs and UUIDs"
hreflangLinks:
type: "[HreflangLink!]!"
description: "Alternate links for hreflang meta tags"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
HreflangDecorator:
type: interface
decorator: true
config:
description: "Represents entity able to return alternate links for hreflang meta tags"
fields:
hreflangLinks:
type: "[HreflangLink!]!"
description: "Alternate links for hreflang meta tags"
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
HreflangLinkDecorator:
type: object
decorator: true
config:
fields:
hreflang:
type: "String!"
description: "Language code for hreflang meta tag"
href:
type: "String!"
description: "URL for hreflang meta tag"
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ MainVariantDecorator:
type: '[Variant!]!'
interfaces:
- 'Product'
- 'Hreflang'
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,6 @@ ProductDecorator:
seoMetaDescription:
type: "String"
description: "Seo meta description of product"
hreflangLinks:
type: "[HreflangLink!]!"
description: "Alternate links for hreflang meta tags"
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ RegularProductDecorator:
config:
interfaces:
- 'Product'
- 'Hreflang'
Loading

0 comments on commit d238091

Please sign in to comment.