Skip to content

Commit

Permalink
implement category sending to nosto
Browse files Browse the repository at this point in the history
  • Loading branch information
makso8makso committed Jan 30, 2025
1 parent c04cd74 commit ee349e7
Show file tree
Hide file tree
Showing 2 changed files with 240 additions and 8 deletions.
80 changes: 72 additions & 8 deletions Model/Service/Sync/Upsert/SyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand All @@ -93,6 +101,8 @@ class SyncService extends AbstractService
* @param ProductServiceInterface $productService
* @param CacheService $cacheService
* @param ProductRepository $productRepository
* @param CategoryRepository $categoryRepository
* @param CategoryServiceInterface $nostoCategoryService

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

View workflow job for this annotation

GitHub Actions / Phan Analysis

Doc-block of $nostoCategoryService in __construct contains phpdoc param type \Nosto\Tagging\Model\Service\Sync\Upsert\CategoryServiceInterface which is incompatible with the param type \Nosto\Tagging\Model\Service\Product\Category\CategoryServiceInterface declared in the signature
* @param $apiBatchSize
* @param $apiTimeout
*/
Expand All @@ -104,6 +114,8 @@ public function __construct(
ProductServiceInterface $productService,
CacheService $cacheService,
ProductRepository $productRepository,
CategoryRepository $categoryRepository,
NostoCategoryService $nostoCategoryService,
$apiBatchSize,
$apiTimeout
) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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

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

View workflow job for this annotation

GitHub Actions / Code Sniffer

Comment refers to a TODO task "send to nosto customer"
foreach ($page->getData() as $category) {
$categoryDb = $this->categoryRepository->getById($category['entity_id']);

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

View workflow job for this annotation

GitHub Actions / Phan Analysis

Call to undeclared method \Nosto\Tagging\Model\Category\Repository::getById (Did you mean expr->getByIds())
$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();

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

View workflow job for this annotation

GitHub Actions / Phan Analysis

Call to method __construct from undeclared class \Nosto\Tagging\Model\Operation\CategoryUpdateOnNosto

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

View workflow job for this annotation

GitHub Actions / Phan Analysis

Call to method execute from undeclared class \Nosto\Tagging\Model\Operation\CategoryUpdateOnNosto

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

View workflow job for this annotation

GitHub Actions / Code Sniffer

Line exceeds 120 characters; contains 150 characters
} catch (Exception $e) {
$this->logger->logDebugWithStore(sprintf(

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

View workflow job for this annotation

GitHub Actions / Phan Analysis

Reference to undeclared property \Nosto\Tagging\Model\Service\Sync\Upsert\SyncService->logger
'Failed to update Nosto category %s with %s id',
$name,
$id
));
}
}

$this->logDebugWithStore(
sprintf(
'Nosto category sync with ids - %s',
implode(',', $categoryIdsInBatch)
),
$store
);

die;

Check warning on line 253 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 253 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 257 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

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);

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

View workflow job for this annotation

GitHub Actions / Phan Analysis

Argument 1 ($message) is $e of type \Exception|\Throwable but \Nosto\Tagging\Model\Service\Sync\Upsert\SyncService::logDebugWithStore() takes string defined at Model/Service/AbstractService.php:216
}

if (empty($nostoCategory)) {
$nostoCategory = null;
}

return $nostoCategory ? trim($nostoCategory) : $nostoCategory;
}
}
168 changes: 168 additions & 0 deletions Operation/CategoryUpdateOnNosto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php
/**
* Copyright (c) 2020, Nosto Solutions Ltd
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Nosto Solutions Ltd <[email protected]>
* @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 <<<QUERY
mutation{
upsertCategories(
categories:
[
{
id: "{$this->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();
}
}

0 comments on commit ee349e7

Please sign in to comment.