From ee349e7792abe0214536fd22b2bacb8188e95dc7 Mon Sep 17 00:00:00 2001 From: makso8makso Date: Thu, 30 Jan 2025 16:52:36 +0100 Subject: [PATCH] implement category sending to nosto --- Model/Service/Sync/Upsert/SyncService.php | 80 +++++++++-- Operation/CategoryUpdateOnNosto.php | 168 ++++++++++++++++++++++ 2 files changed, 240 insertions(+), 8 deletions(-) create mode 100644 Operation/CategoryUpdateOnNosto.php diff --git a/Model/Service/Sync/Upsert/SyncService.php b/Model/Service/Sync/Upsert/SyncService.php index b8b23bea0..cae34affb 100644 --- a/Model/Service/Sync/Upsert/SyncService.php +++ b/Model/Service/Sync/Upsert/SyncService.php @@ -51,9 +51,12 @@ use Nosto\Tagging\Model\ResourceModel\Magento\Product\Collection as ProductCollection; use Nosto\Tagging\Model\Service\AbstractService; use Nosto\Tagging\Model\Service\Cache\CacheService; +use Nosto\Tagging\Model\Service\Product\Category\CategoryServiceInterface as NostoCategoryService; use Nosto\Tagging\Model\Service\Product\ProductServiceInterface; use Nosto\Tagging\Util\PagingIterator; use Nosto\Tagging\Model\Product\Repository as ProductRepository; +use Nosto\Tagging\Model\Category\Repository as CategoryRepository; +use Nosto\Tagging\Model\Operation\CategoryUpdateOnNosto; class SyncService extends AbstractService { @@ -84,6 +87,11 @@ class SyncService extends AbstractService /** @var ProductRepository */ private ProductRepository $productRepository; + private CategoryRepository $categoryRepository; + + /** @var NostoCategoryService */ + private NostoCategoryService $nostoCategoryService; + /** * Sync constructor. * @param NostoHelperAccount $nostoHelperAccount @@ -93,6 +101,8 @@ class SyncService extends AbstractService * @param ProductServiceInterface $productService * @param CacheService $cacheService * @param ProductRepository $productRepository + * @param CategoryRepository $categoryRepository + * @param CategoryServiceInterface $nostoCategoryService * @param $apiBatchSize * @param $apiTimeout */ @@ -104,6 +114,8 @@ public function __construct( ProductServiceInterface $productService, CacheService $cacheService, ProductRepository $productRepository, + CategoryRepository $categoryRepository, + NostoCategoryService $nostoCategoryService, $apiBatchSize, $apiTimeout ) { @@ -114,6 +126,8 @@ public function __construct( $this->nostoDataHelper = $nostoDataHelper; $this->cacheService = $cacheService; $this->productRepository = $productRepository; + $this->categoryRepository = $categoryRepository; + $this->nostoCategoryService = $nostoCategoryService; $this->apiBatchSize = $apiBatchSize; $this->apiTimeout = $apiTimeout; } @@ -188,26 +202,76 @@ public function syncProducts(ProductCollection $collection, Store $store) */ public function syncCategories(CategoryCollection $collection, Store $store) { - if (!$this->nostoDataHelper->isProductUpdatesEnabled($store)) { - $this->logDebugWithStore( - 'Nosto Category sync is disabled', - $store - ); - return; - } +// if (!$this->nostoDataHelper->isProductUpdatesEnabled($store)) { +// $this->logDebugWithStore( +// 'Nosto Category sync is disabled', +// $store +// ); +// return; +// } + $categoryIdsInBatch = []; $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'); + // todo: send to nosto customer + foreach ($page->getData() as $category) { + $categoryDb = $this->categoryRepository->getById($category['entity_id']); + $categoryIdsInBatch[] = $category['entity_id']; + $id = $category['entity_id']; + $name = explode("/", $categoryDb->getName()); + $name = end($name); + $parentId = $categoryDb->getParentId(); + $urlPath = $this->getUrlPath($categoryDb, $store); + $fullName = $categoryDb->getName(); + $available = $categoryDb->getIsActive() ?? false; + try { + (new CategoryUpdateOnNosto($account, $store->getCurrentUrl(), $id, $name, $parentId, $urlPath, $fullName, $available))->execute(); + } catch (Exception $e) { + $this->logger->logDebugWithStore(sprintf( + 'Failed to update Nosto category %s with %s id', + $name, + $id + )); + } + } + + $this->logDebugWithStore( + sprintf( + 'Nosto category sync with ids - %s', + implode(',', $categoryIdsInBatch) + ), + $store + ); + die; } } + + private function getUrlPath($category, Store $store) + { + $nostoCategory = ''; + try { + $data = []; + $path = $category->getPath(); + $data[] = $category->getName(); + $nostoCategory = count($data) ? '/' . implode('/', $data) : ''; + } catch (Exception $e) { + $this->logDebugWithStore($e, $store); + } + + if (empty($nostoCategory)) { + $nostoCategory = null; + } + + return $nostoCategory ? trim($nostoCategory) : $nostoCategory; + } } diff --git a/Operation/CategoryUpdateOnNosto.php b/Operation/CategoryUpdateOnNosto.php new file mode 100644 index 000000000..961fb7a31 --- /dev/null +++ b/Operation/CategoryUpdateOnNosto.php @@ -0,0 +1,168 @@ + + * @copyright 2020 Nosto Solutions Ltd + * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause + * + */ +namespace Nosto\Tagging\Model\Operation; + +use Nosto\Operation\AbstractGraphQLOperation; +use Nosto\Types\Signup\AccountInterface; +use Nosto\Result\Graphql\Session\SessionResultHandler; + +class CategoryUpdateOnNosto extends AbstractGraphQLOperation +{ + /** + * @var AccountInterface Nosto configuration. + */ + protected $account; + + /** + * @var string active domain + */ + protected $activeDomain; + + /** + * @var string $id + */ + private string $id; + + /** + * @var string $name + */ + private string $name; + + /** + * @var string $parentId + */ + private string $parentId; + + /** + * @var string $urlPath + */ + private string $urlPath; + + /** + * @var string $fullName + */ + private string $fullName; + + /** + * @var boolean + */ + private bool $available; + + /** + * @var string + */ + private string $referer; + + /** + * NewSession constructor. + * @param AccountInterface $account + * @param string $id + * @param string $name + * @param string $parentId + * @param string $urlPath + * @param string $fullName + * @param boolean $available + */ + public function __construct(AccountInterface $account, string $referer, string $id, string $name, string $parentId, string $urlPath, string $fullName, bool $available, string $activeDomain = '') + { + $this->referer = $referer; + $this->id = $id; + $this->name = $name; + $this->parentId = $parentId; + $this->urlPath = $urlPath; + $this->fullName = $fullName; + $this->available = $available; + + parent::__construct($account, $activeDomain); + } + + /** + * @return string + */ + public function getQuery(): string + { + return <<id}", + name: "{$this->name}", + parentId: "{$this->parentId}", + urlPath: "{$this->urlPath}", + fullName: "{$this->fullName}", + available: "{$this->available}" + } + ] + ) { + categoryResult { + errors { + field + message + } + category { + id + name + parentId + urlPath + fullName + available + } + } + } + } +QUERY; + } + + /** + * @return array + */ + public function getVariables(): array + { + return [ + "referer" => $this->referer + ]; + } + + /** + * @return SessionResultHandler + */ + protected function getResultHandler(): SessionResultHandler + { + return new SessionResultHandler(); + } +}