bring into standalone plugin for distribution
This commit is contained in:
106
src/Service/InternalCatalogManagerService.php
Normal file
106
src/Service/InternalCatalogManagerService.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace CakeProducts\Service;
|
||||
|
||||
use Cake\Core\ServiceConfig;
|
||||
use Cake\Datasource\EntityInterface;
|
||||
use Cake\I18n\FrozenTime;
|
||||
use Cake\I18n\Time;
|
||||
use Cake\Log\Log;
|
||||
use Cake\ORM\Locator\LocatorAwareTrait;
|
||||
use Cake\ORM\Table;
|
||||
use Cake\Utility\Text;
|
||||
use CakeProducts\Model\Entity\ProductCategory;
|
||||
use CakeProducts\Model\Table\ProductCatalogsTable;
|
||||
|
||||
class InternalCatalogManagerService
|
||||
{
|
||||
use LocatorAwareTrait;
|
||||
|
||||
/**
|
||||
* @var ProductCatalogsTable
|
||||
*/
|
||||
protected Table|ProductCatalogsTable $ProductCatalogs;
|
||||
|
||||
/**
|
||||
* @var ServiceConfig
|
||||
*/
|
||||
protected ServiceConfig $serviceConfig;
|
||||
|
||||
/**
|
||||
* @var ExternalCatalogManagerService|null
|
||||
*/
|
||||
protected ?ExternalCatalogManagerService $externalCatalogManager;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function __construct(ExternalCatalogManagerService $externalCatalogManagerService)
|
||||
{
|
||||
$this->ProductCatalogs = $this->fetchTable('CakeProducts.ProductCatalogs');
|
||||
$this->serviceConfig = new ServiceConfig();
|
||||
|
||||
if ($this->serviceConfig->get('CakeProducts.internal.enabled') && $this->serviceConfig->get('CakeProducts.internal.syncExternally')) {
|
||||
$this->externalCatalogManager = $externalCatalogManagerService;
|
||||
}
|
||||
|
||||
}
|
||||
public function getCatalog(string $id = null)
|
||||
{
|
||||
$contain = ['ProductCategories'];
|
||||
if ($this->serviceConfig->get('CakeProducts.internal.syncExternally')) {
|
||||
$contain[] = 'ExternalProductCatalogs';
|
||||
}
|
||||
|
||||
return $this->ProductCatalogs->get($id, contain: $contain);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $id
|
||||
*
|
||||
* @return \App\Model\Entity\ProductCategory|EntityInterface
|
||||
*/
|
||||
public function getCategory(string $id = null)
|
||||
{
|
||||
$contain = ['ProductCatalogs', 'ParentProductCategories', 'ChildProductCategories'];
|
||||
|
||||
return $this->ProductCatalogs->ProductCategories->get($id, contain: $contain);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ProductCategory $productCategory product category entity
|
||||
* @param array $data data to save
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function createNewCategory(ProductCategory $productCategory, array $data = []): array
|
||||
{
|
||||
$now = Time::now();
|
||||
$associated = [];
|
||||
|
||||
Log::info('posted data - adding new ProductCategory');
|
||||
Log::info(print_r($data, true));
|
||||
|
||||
$saveOptions = [
|
||||
'associated' => $associated,
|
||||
];
|
||||
if (!array_key_exists('internal_id', $data) || !$data['internal_id']) {
|
||||
$data['internal_id'] = Text::uuid();
|
||||
}
|
||||
$productCategory = $this->ProductCatalogs->ProductCategories->patchEntity($productCategory, $data, $saveOptions);
|
||||
if ($productCategory->getErrors()) {
|
||||
Log::debug(print_r('$productCategory->getErrors() next - failed to save from create new product category', true));
|
||||
Log::debug(print_r($productCategory->getErrors(), true));
|
||||
}
|
||||
$returnData = [
|
||||
'entity' => $productCategory,
|
||||
'result' => $this->ProductCatalogs->ProductCategories->save($productCategory, $saveOptions),
|
||||
'apiResults' => [],
|
||||
];
|
||||
if ($returnData['result'] && $this->externalCatalogManager) {
|
||||
$returnData['apiResults'] = $this->externalCatalogManager->newCategoryCreated($returnData['result']);
|
||||
}
|
||||
|
||||
return $returnData;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user