Skip to content

Commit 0dd0ccd

Browse files
FRW-10007 Implemented Sitemap Generation. (#11378)
FRW-10007 Implemented Sitemap Generation.
1 parent 7e5ecdc commit 0dd0ccd

File tree

7 files changed

+178
-5
lines changed

7 files changed

+178
-5
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
"spryker/product": "^5.5.0 || ^6.0.0",
1313
"spryker/product-storage-extension": "^1.7.0",
1414
"spryker/propel-orm": "^1.5.0",
15+
"spryker/sitemap-extension": "^1.0.0",
1516
"spryker/storage": "^3.4.0",
1617
"spryker/store": "^1.7.0",
1718
"spryker/symfony": "^3.0.0",
1819
"spryker/synchronization": "^0.2.0 || ^1.0.0",
1920
"spryker/synchronization-behavior": "^1.0.0",
2021
"spryker/synchronization-extension": "^1.0.0",
21-
"spryker/transfer": "^3.25.0",
22+
"spryker/transfer": "^3.27.0",
2223
"spryker/url": "^3.3.0",
2324
"spryker/util-encoding": "^1.0.0 || ^2.0.0",
2425
"spryker/util-sanitize": "^2.1.0"

src/Spryker/Shared/ProductStorage/Transfer/product_storage.transfer.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,12 @@
9696
<property name="modifiedColumns" type="array" singular="modifiedColumns"/>
9797
</transfer>
9898

99+
<transfer name="SitemapUrl" strict="true">
100+
<property name="idEntity" type="int"/>
101+
<property name="url" type="string"/>
102+
<property name="updatedAt" type="string"/>
103+
<property name="languageCode" type="string"/>
104+
<property name="storeName" type="string"/>
105+
</transfer>
106+
99107
</transfers>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/**
4+
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5+
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6+
*/
7+
8+
namespace Spryker\Zed\ProductStorage\Communication\Plugin\Sitemap;
9+
10+
use Spryker\Zed\Kernel\Communication\AbstractPlugin;
11+
use Spryker\Zed\SitemapExtension\Dependency\Plugin\SitemapDataProviderPluginInterface;
12+
13+
/**
14+
* @method \Spryker\Zed\ProductStorage\Persistence\ProductStorageRepositoryInterface getRepository()
15+
* @method \Spryker\Zed\ProductStorage\ProductStorageConfig getConfig()
16+
* @method \Spryker\Zed\ProductStorage\Business\ProductStorageFacadeInterface getFacade()
17+
* @method \Spryker\Zed\ProductStorage\Communication\ProductStorageCommunicationFactory getFactory()
18+
*/
19+
class ProductAbstractSitemapDataProviderPlugin extends AbstractPlugin implements SitemapDataProviderPluginInterface
20+
{
21+
/**
22+
* @var string
23+
*/
24+
protected const PRODUCT_ABSTRACT_ENTITY_TYPE = 'product_abstract';
25+
26+
/**
27+
* {@inheritDoc}
28+
*
29+
* @api
30+
*
31+
* @return string
32+
*/
33+
public function getEntityType(): string
34+
{
35+
return static::PRODUCT_ABSTRACT_ENTITY_TYPE;
36+
}
37+
38+
/**
39+
* {@inheritDoc}
40+
* - Returns an array of sitemap URL related data for abstract products.
41+
*
42+
* @api
43+
*
44+
* @param string $storeName
45+
*
46+
* @return array<\Generated\Shared\Transfer\SitemapUrlTransfer>
47+
*/
48+
public function getSitemapUrls(string $storeName): array
49+
{
50+
return $this->getRepository()->getSitemapUrls($storeName);
51+
}
52+
}

src/Spryker/Zed/ProductStorage/Persistence/ProductStoragePersistenceFactory.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Orm\Zed\ProductStorage\Persistence\SpyProductAbstractStorageQuery;
1313
use Orm\Zed\ProductStorage\Persistence\SpyProductConcreteStorageQuery;
1414
use Spryker\Zed\Kernel\Persistence\AbstractPersistenceFactory;
15+
use Spryker\Zed\ProductStorage\Persistence\Propel\Mapper\ProductStorageMapper;
1516
use Spryker\Zed\ProductStorage\ProductStorageDependencyProvider;
1617

1718
/**
@@ -60,4 +61,12 @@ public function getProductAbstractLocalizedAttributesPropelQuery(): SpyProductAb
6061
{
6162
return $this->getProvidedDependency(ProductStorageDependencyProvider::PROPEL_QUERY_PRODUCT_ABSTRACT_LOCALIZED_ATTRIBUTES);
6263
}
64+
65+
/**
66+
* @return \Spryker\Zed\ProductStorage\Persistence\Propel\Mapper\ProductStorageMapper
67+
*/
68+
public function createProductStorageMapper(): ProductStorageMapper
69+
{
70+
return new ProductStorageMapper();
71+
}
6372
}

src/Spryker/Zed/ProductStorage/Persistence/ProductStorageRepository.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class ProductStorageRepository extends AbstractRepository implements ProductStor
4242
*/
4343
protected const COL_FK_PRODUCT_ABSTRACT = 'fk_product_abstract';
4444

45+
/**
46+
* @var int
47+
*/
48+
protected const SITEMAP_QUERY_LIMIT = 1000;
49+
4550
/**
4651
* @param array<int> $productAbstractIds
4752
*
@@ -85,4 +90,31 @@ public function getProductConcretesCountByIdProductAbstracts(array $idProductAbs
8590

8691
return $productConcretesCollection->toArray();
8792
}
93+
94+
/**
95+
* @param string $storeName
96+
*
97+
* @return array<\Generated\Shared\Transfer\SitemapUrlTransfer>
98+
*/
99+
public function getSitemapUrls(string $storeName): array
100+
{
101+
$offset = 0;
102+
$productAbstractStorageQuery = $this->getFactory()
103+
->createSpyProductAbstractStorageQuery()
104+
->filterByStore($storeName)
105+
->orderByIdProductAbstractStorage()
106+
->limit(static::SITEMAP_QUERY_LIMIT)
107+
->offset($offset);
108+
$sitemapUrlTransfers = [];
109+
$productStorageMapper = $this->getFactory()->createProductStorageMapper();
110+
111+
do {
112+
$offset += static::SITEMAP_QUERY_LIMIT;
113+
$productAbstractStorageEntities = $productAbstractStorageQuery->find();
114+
$sitemapUrlTransfers[] = $productStorageMapper->mapProductAbstractStorageEntitiesToSitemapUrlTransfers($productAbstractStorageEntities);
115+
$productAbstractStorageQuery->offset($offset);
116+
} while ($productAbstractStorageEntities->count() === static::SITEMAP_QUERY_LIMIT);
117+
118+
return array_merge(...$sitemapUrlTransfers);
119+
}
88120
}

src/Spryker/Zed/ProductStorage/Persistence/ProductStorageRepositoryInterface.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
22

3-
/**
4-
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5-
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6-
*/
3+
/**
4+
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5+
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6+
*/
7+
8+
declare(strict_types = 1);
79

810
namespace Spryker\Zed\ProductStorage\Persistence;
911

@@ -25,4 +27,11 @@ public function getProductAbstractsByIds(array $productAbstractIds): array;
2527
* @return array<array<string, int>>
2628
*/
2729
public function getProductConcretesCountByIdProductAbstracts(array $idProductAbstract): array;
30+
31+
/**
32+
* @param string $storeName
33+
*
34+
* @return array<\Generated\Shared\Transfer\SitemapUrlTransfer>
35+
*/
36+
public function getSitemapUrls(string $storeName): array;
2837
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/**
4+
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5+
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6+
*/
7+
8+
namespace Spryker\Zed\ProductStorage\Persistence\Propel\Mapper;
9+
10+
use Generated\Shared\Transfer\SitemapUrlTransfer;
11+
use Orm\Zed\ProductStorage\Persistence\SpyProductAbstractStorage;
12+
use Propel\Runtime\Collection\Collection;
13+
14+
class ProductStorageMapper
15+
{
16+
/**
17+
* @var string
18+
*/
19+
protected const COLUMN_URL = 'url';
20+
21+
/**
22+
* @var string
23+
*/
24+
protected const DATE_FORMAT = 'Y-m-d';
25+
26+
/**
27+
* @param \Propel\Runtime\Collection\Collection<\Orm\Zed\ProductStorage\Persistence\SpyProductAbstractStorage> $productAbstractStorageEntities
28+
*
29+
* @return array<\Generated\Shared\Transfer\SitemapUrlTransfer>
30+
*/
31+
public function mapProductAbstractStorageEntitiesToSitemapUrlTransfers(Collection $productAbstractStorageEntities): array
32+
{
33+
$sitemapUrlTransfers = [];
34+
35+
foreach ($productAbstractStorageEntities as $productAbstractStorageEntity) {
36+
$productAbstractStorageData = $productAbstractStorageEntity->getData();
37+
38+
if (!isset($productAbstractStorageData[static::COLUMN_URL])) {
39+
continue;
40+
}
41+
42+
$sitemapUrlTransfers[] = $this->mapProductAbstractStorageEntityToSitemapUrlTransfer($productAbstractStorageEntity);
43+
}
44+
45+
return $sitemapUrlTransfers;
46+
}
47+
48+
/**
49+
* @param \Orm\Zed\ProductStorage\Persistence\SpyProductAbstractStorage $productAbstractStorageEntity
50+
*
51+
* @return \Generated\Shared\Transfer\SitemapUrlTransfer
52+
*/
53+
protected function mapProductAbstractStorageEntityToSitemapUrlTransfer(SpyProductAbstractStorage $productAbstractStorageEntity): SitemapUrlTransfer
54+
{
55+
return (new SitemapUrlTransfer())
56+
->setUrl($productAbstractStorageEntity->getData()[static::COLUMN_URL])
57+
->setUpdatedAt($productAbstractStorageEntity->getUpdatedAt(static::DATE_FORMAT))
58+
->setLanguageCode($productAbstractStorageEntity->getLocale())
59+
->setStoreName($productAbstractStorageEntity->getStore())
60+
->setIdEntity($productAbstractStorageEntity->getFkProductAbstract());
61+
}
62+
}

0 commit comments

Comments
 (0)