Skip to content

Commit

Permalink
Prepare for send categories to Nosto
Browse files Browse the repository at this point in the history
  • Loading branch information
ugljesaspx committed Dec 19, 2024
1 parent 7e75e9e commit c04cd74
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 26 deletions.
5 changes: 4 additions & 1 deletion Model/Category/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ public function __construct(
*/
public function getByIds(array $ids)
{
//@TODO implement
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('entity_id', $ids, 'in')
->create();
return $this->categoryRepository->getList($searchCriteria);

Check failure on line 87 in Model/Category/Repository.php

View workflow job for this annotation

GitHub Actions / Phan Analysis

Call to undeclared method \Magento\Catalog\Model\CategoryRepository::getList
}

/**
Expand Down
2 changes: 0 additions & 2 deletions Model/ResourceModel/Magento/Category/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class Collection extends MagentoCategoryCollection
{
/**
* @return Collection
* @throws LocalizedException
*/
public function addActiveFilter(): Collection
{
Expand All @@ -53,7 +52,6 @@ public function addActiveFilter(): Collection
/**
* @param array $ids
* @return Collection
* @throws LocalizedException
*/
public function addIdsToFilter(array $ids): Collection
{
Expand Down
8 changes: 6 additions & 2 deletions Model/Service/Sync/AbstractBulkConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,15 @@ public function processOperation(OperationInterface $operation)
{
$serializedData = $operation->getSerializedData();
$unserializedData = $this->jsonHelper->jsonDecode($serializedData);
$productIds = $unserializedData['product_ids'];

$productIds = $unserializedData['product_ids'] ?? null;
$categoryIds = $unserializedData['category_ids'] ?? null;
$storeId = $unserializedData['store_id'];
try {
$idsToProcess = $productIds ?: $categoryIds;

$this->storeEmulation->startEnvironmentEmulation((int)$storeId);
$this->doOperation($productIds, $storeId);
$this->doOperation($idsToProcess, $storeId);
/**
* Argument is of type string but array is expected
*/
Expand Down
45 changes: 33 additions & 12 deletions Model/Service/Sync/AbstractBulkPublisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,34 +123,52 @@ private function publishCollectionToMessageQueue(
);
return;
}
$productIdsChunks = array_chunk($entityIds, $this->getBulkSize());

$bulkUuid = $this->identityService->generateId();
/**
* Argument is of type string but array is expected
*/
/** @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal */
$bulkDescription = __('Sync ' . count($entityIds) . ' Nosto products');
$bulkDescription = __('Sync ' . count($entityIds) . ' Nosto entities');

Check warning on line 128 in Model/Service/Sync/AbstractBulkPublisher.php

View workflow job for this annotation

GitHub Actions / Phan Analysis

Argument 1 ($argc) is (('Sync ' . count($entityIds)) . ' Nosto entities') of type string but \__() takes array (no real type) defined at vendor/magento/framework/Phrase/__.php:16 (the inferred real argument type has nothing in common with the parameter's phpdoc type)
$operationsData = [];
foreach ($productIdsChunks as $productIdsChunk) {
$operationsData[] = $this->buildOperationData(
$storeId,
$productIdsChunk,
$bulkUuid
);

// TODO: @Ugljesa Check the code.

Check warning on line 131 in Model/Service/Sync/AbstractBulkPublisher.php

View workflow job for this annotation

GitHub Actions / Code Sniffer

Comment refers to a TODO task "@ugljesa Check the code"
if (isset($entityIds['entity']) && $entityIds['entity'] === 'category') {
// Process Categories.
$categoryIdsChunks = array_chunk($entityIds['categoryIds'], $this->getBulkSize());

foreach ($categoryIdsChunks as $categoryIdsChunk) {
$operationsData[] = $this->buildOperationData(
$storeId,
[],
$categoryIdsChunk,
$bulkUuid
);
}
} else {
// Process Products.
$productIdsChunks = array_chunk($entityIds, $this->getBulkSize());
foreach ($productIdsChunks as $productIdsChunk) {
$operationsData[] = $this->buildOperationData(
$storeId,
$productIdsChunk,
[],
$bulkUuid
);
}
}

$operations = [];
foreach ($operationsData as $operationData) {
$operations[] = $this->operationFactory->create($operationData);
}

if (empty($operations)) {
return;
}

$result = $this->bulkManagement->scheduleBulk(
$bulkUuid,
$operations,
$bulkDescription
);

if (!$result) {
$msg = 'Something went wrong while publishing bulk to RabbitMQ. Please check your connection';
/** @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal */
Expand Down Expand Up @@ -190,17 +208,20 @@ abstract public function getMetaData(): string;
* Build asynchronous operation data
* @param int $storeId
* @param array $productIds
* @param array $categoryIdsChunks
* @param string $bulkUuid
* @return array
*/
private function buildOperationData(
int $storeId,
array $productIds,
array $categoryIdsChunks,
string $bulkUuid
) {
$dataToEncode = [
'meta_information' => $this->getMetaData(),
'product_ids' => $productIds,
'category_ids' => $categoryIdsChunks,
'store_id' => $storeId
];
return [
Expand Down
16 changes: 8 additions & 8 deletions Model/Service/Sync/Upsert/Category/AsyncBulkConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@
namespace Nosto\Tagging\Model\Service\Sync\Upsert\Category;

use Magento\Framework\EntityManager\EntityManager;
use Magento\Framework\Exception\LocalizedException;

Check warning on line 40 in Model/Service/Sync/Upsert/Category/AsyncBulkConsumer.php

View workflow job for this annotation

GitHub Actions / Phan Analysis

Possibly zero references to use statement for classlike/namespace LocalizedException (\Magento\Framework\Exception\LocalizedException)
use Magento\Framework\Json\Helper\Data as JsonHelper;
use Magento\Store\Model\App\Emulation;
use Nosto\Exception\MemoryOutOfBoundsException;

Check warning on line 43 in Model/Service/Sync/Upsert/Category/AsyncBulkConsumer.php

View workflow job for this annotation

GitHub Actions / Phan Analysis

Possibly zero references to use statement for classlike/namespace MemoryOutOfBoundsException (\Nosto\Exception\MemoryOutOfBoundsException)
use Nosto\NostoException;

Check warning on line 44 in Model/Service/Sync/Upsert/Category/AsyncBulkConsumer.php

View workflow job for this annotation

GitHub Actions / Phan Analysis

Possibly zero references to use statement for classlike/namespace NostoException (\Nosto\NostoException)
use Nosto\Tagging\Helper\Scope as NostoScopeHelper;
use Nosto\Tagging\Logger\Logger;
use Nosto\Tagging\Model\ResourceModel\Magento\Product\CollectionFactory;
use Nosto\Tagging\Model\ResourceModel\Magento\Category\CollectionFactory;
use Nosto\Tagging\Model\Service\Sync\AbstractBulkConsumer;
use Nosto\Tagging\Model\Service\Sync\Upsert\SyncService;

Expand Down Expand Up @@ -95,15 +96,14 @@ public function __construct(

/**
* @inheritDoc
* @throws MemoryOutOfBoundsException
* @throws NostoException
*/
public function doOperation(array $productIds, string $storeId)
public function doOperation(array $categoryIds, string $storeId)
{
// TODO: Add store.

Check warning on line 102 in Model/Service/Sync/Upsert/Category/AsyncBulkConsumer.php

View workflow job for this annotation

GitHub Actions / Code Sniffer

Comment refers to a TODO task "Add store"
$store = $this->nostoScopeHelper->getStore($storeId);
$productCollection = $this->collectionFactory->create()
->addIdsToFilter($productIds)
->addStoreFilter($storeId);
$this->syncService->syncProducts($productCollection, $store);
$categoryCollection = $this->collectionFactory->create()
->addIdsToFilter($categoryIds);

$this->syncService->syncCategories($categoryCollection, $store);
}
}
30 changes: 30 additions & 0 deletions Model/Service/Sync/Upsert/SyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use Magento\Catalog\Model\Product;
use Magento\Store\Model\Store;
use Nosto\Exception\MemoryOutOfBoundsException;
use Nosto\Tagging\Model\ResourceModel\Magento\Category\Collection as CategoryCollection;
use Nosto\NostoException;
use Nosto\Operation\UpsertProduct;
use Nosto\Request\Http\Exception\AbstractHttpException;
Expand Down Expand Up @@ -180,4 +181,33 @@ public function syncProducts(ProductCollection $collection, Store $store)
}
$this->logBenchmarkSummary(self::BENCHMARK_SYNC_NAME, $store, $this);
}

/**
* @throws NostoException
* @throws MemoryOutOfBoundsException
*/
public function syncCategories(CategoryCollection $collection, Store $store)
{
if (!$this->nostoDataHelper->isProductUpdatesEnabled($store)) {
$this->logDebugWithStore(
'Nosto Category sync is disabled',
$store
);
return;
}
$account = $this->nostoHelperAccount->findAccount($store);
$this->startBenchmark('nosto_category_upsert', self::BENCHMARK_SYNC_BREAKPOINT);
$index = 0;
$collection->setPageSize($this->apiBatchSize);
$iterator = new PagingIterator($collection);
/** @var CategoryCollection $page */
foreach ($iterator as $page) {
$categoryIdsInBatch = [];
$this->checkMemoryConsumption('category sync');

die;

Check warning on line 208 in Model/Service/Sync/Upsert/SyncService.php

View workflow job for this annotation

GitHub Actions / Code Sniffer

Use of die language construct is discouraged.

Check failure on line 208 in Model/Service/Sync/Upsert/SyncService.php

View workflow job for this annotation

GitHub Actions / Code Sniffer

Use of die language construct is discouraged.

}

}

Check warning on line 212 in Model/Service/Sync/Upsert/SyncService.php

View workflow job for this annotation

GitHub Actions / Code Sniffer

Function closing brace must go on the next line following the body; found 1 blank lines before brace
}
10 changes: 9 additions & 1 deletion Model/Service/Update/CategoryUpdateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@

class CategoryUpdateService extends AbstractService
{
const CATEGORY_SERIVCE = 'category';

/** @var NostoCategoryRepository $nostoCategoryRepository */
private NostoCategoryRepository $nostoCategoryRepository;

Expand Down Expand Up @@ -113,7 +115,12 @@ public function addCollectionToUpdateMessageQueue(CategoryCollection $collection
);
/** @var CategoryCollection $page */
foreach ($iterator as $page) {
$this->upsertBulkPublisher->execute($store->getId(), $this->toParentCategoryIds($page));
$data = [
'entity' => self::CATEGORY_SERIVCE,
'categoryIds' => $this->toParentCategoryIds($page)
];

$this->upsertBulkPublisher->execute($store->getId(), $data);
}
}

Expand All @@ -125,6 +132,7 @@ private function toParentCategoryIds(CategoryCollection $collection): array
{
$categoryIds = [];
/** @var CategoryInterface $category */
// TODO: @ugljesa Check instanceof.

Check warning on line 135 in Model/Service/Update/CategoryUpdateService.php

View workflow job for this annotation

GitHub Actions / Code Sniffer

Comment refers to a TODO task "@ugljesa Check instanceof"
foreach ($collection->getItems() as $category) {
try {
/** @phan-suppress-next-line PhanTypeMismatchArgument */
Expand Down
1 change: 1 addition & 0 deletions Observer/Order/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use Magento\Framework\Module\Manager as ModuleManager;
use Magento\Sales\Model\Order;
use Magento\Store\Model\Store;
use Nosto\Model\Order\Buyer;

Check warning on line 49 in Observer/Order/Save.php

View workflow job for this annotation

GitHub Actions / Phan Analysis

Possibly zero references to use statement for classlike/namespace Buyer (\Nosto\Model\Order\Buyer)
use Nosto\Model\Order\Order as NostoOrder;
use Nosto\Operation\Order\OrderCreate as NostoOrderCreate;
use Nosto\Operation\Order\OrderStatus as NostoOrderUpdate;
Expand Down

0 comments on commit c04cd74

Please sign in to comment.