Skip to content

Commit 06d6cd6

Browse files
committed
Merge branch 'feature/BEG-81_initial_version' of github.com:aligent/magento2-prerender-io into feature/BEG-81_initial_version
2 parents 27cb3ea + 24e9f32 commit 06d6cd6

File tree

5 files changed

+63
-9
lines changed

5 files changed

+63
-9
lines changed

Helper/Config.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@ class Config
1717
/** @var ScopeConfigInterface */
1818
private ScopeConfigInterface $scopeConfig;
1919

20+
/**
21+
*
22+
* @param ScopeConfigInterface $scopeConfig
23+
*/
2024
public function __construct(ScopeConfigInterface $scopeConfig)
2125
{
2226
$this->scopeConfig = $scopeConfig;
2327
}
2428

2529
/**
30+
* Return if recaching functionality is enabled or not
31+
*
2632
* @param int|null $storeId
2733
* @return bool
2834
*/
@@ -36,6 +42,8 @@ public function isRecacheEnabled(?int $storeId = null): bool
3642
}
3743

3844
/**
45+
* Return configured Prerender.io token for API calls
46+
*
3947
* @param int|null $storeId
4048
* @return string|null
4149
*/

Model/Api/PrerenderClient.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,33 @@ class PrerenderClient implements PrerenderClientInterface
2727
/** @var LoggerInterface */
2828
private LoggerInterface $logger;
2929

30+
/**
31+
*
32+
* @param Config $prerenderConfigHelper
33+
* @param ClientInterface $client
34+
* @param SerializerInterface $jsonSerializer
35+
* @param LoggerInterface $logger
36+
*/
3037
public function __construct(
3138
Config $prerenderConfigHelper,
3239
ClientInterface $client,
3340
SerializerInterface $jsonSerializer,
3441
LoggerInterface $logger
3542
) {
3643
$this->prerenderConfigHelper = $prerenderConfigHelper;
37-
$this->client = $client;
3844
$this->jsonSerializer = $jsonSerializer;
3945
$this->logger = $logger;
46+
$this->client = $client;
47+
$this->client->addHeader('content-type', 'application/json');
4048
}
4149

50+
/**
51+
* Call Prerender.io API to recache list of URLs
52+
*
53+
* @param array $urls
54+
* @param int $storeId
55+
* @return void
56+
*/
4257
public function recacheUrls(array $urls, int $storeId): void
4358
{
4459
if (!$this->prerenderConfigHelper->isRecacheEnabled($storeId)) {
@@ -50,16 +65,19 @@ public function recacheUrls(array $urls, int $storeId): void
5065
return;
5166
}
5267

53-
if (count($urls) > self::MAX_URLS) {
54-
$batches = array_chunk($urls, self::MAX_URLS);
55-
foreach ($batches as $batch) {
56-
$this->sendRequest($batch, $token);
57-
}
58-
} else {
59-
$this->sendRequest($urls, $token);
68+
$batches = array_chunk($urls, self::MAX_URLS);
69+
foreach ($batches as $batch) {
70+
$this->sendRequest($batch, $token);
6071
}
6172
}
6273

74+
/**
75+
* Sends a JSON POST request to Prerender.io
76+
*
77+
* @param array $urls
78+
* @param string $token
79+
* @return void
80+
*/
6381
private function sendRequest(array $urls, string $token): void
6482
{
6583
$payload = $this->jsonSerializer->serialize(
@@ -68,7 +86,6 @@ private function sendRequest(array $urls, string $token): void
6886
'urls' => $urls
6987
]
7088
);
71-
$this->client->addHeader('content-type', 'application/json');
7289
try {
7390
$this->client->post(self::PRERENDER_RECACHE_URL, $payload);
7491
} catch (\Exception $e) {

Model/Indexer/Product/ProductIndexer.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ class ProductIndexer implements IndexerActionInterface, MviewActionInterface, Di
3838
/** @var int|null */
3939
private ?int $batchSize;
4040

41+
/**
42+
*
43+
* @param DimensionProviderInterface $dimensionProvider
44+
* @param GetUrlsForProducts $getUrlsForProducts
45+
* @param PrerenderClientInterface $prerenderClient
46+
* @param DeploymentConfig $deploymentConfig
47+
* @param Config $prerenderConfigHelper
48+
* @param int|null $batchSize
49+
*/
4150
public function __construct(
4251
DimensionProviderInterface $dimensionProvider,
4352
GetUrlsForProducts $getUrlsForProducts,
@@ -110,6 +119,10 @@ public function execute($ids): void
110119
}
111120

112121
/**
122+
* Execute indexing per dimension (store)
123+
*
124+
* @param arry $dimensions
125+
* @param \Traversable $entityIds
113126
* @throws FileSystemException
114127
* @throws RuntimeException
115128
*/

Model/Product/GetUrlsForProducts.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ class GetUrlsForProducts
2222
/** @var Emulation */
2323
private Emulation $emulation;
2424

25+
/**
26+
*
27+
* @param CollectionFactory $productCollectionFactory
28+
* @param StoreManagerInterface $storeManager
29+
* @param Emulation $emulation
30+
*/
2531
public function __construct(
2632
CollectionFactory $productCollectionFactory,
2733
StoreManagerInterface $storeManager,
@@ -32,6 +38,13 @@ public function __construct(
3238
$this->emulation = $emulation;
3339
}
3440

41+
/**
42+
* Generate product URLs based on URL_REWRITE entries
43+
*
44+
* @param array $productIds
45+
* @param int $storeId
46+
* @return array
47+
*/
3548
public function execute(array $productIds, int $storeId): array
3649
{
3750
$productCollection = $this->productCollectionFactory->create();

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"php": ">=7.4",
66
"magento/framework": "*"
77
},
8+
"require-dev": {
9+
"magento/magento-coding-standard": ">=23"
10+
},
811
"type": "magento2-module",
912
"license": [
1013
"GPL-3.0-only"

0 commit comments

Comments
 (0)