Files
cake-products/src/Service/InternalCatalogManagerService.php

142 lines
4.7 KiB
PHP

<?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\ExternalProductCatalog;
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 ExternalProductCatalog $externalProductCatalog external product catalog entity
* @param array $data data to save
*
* @return array
*/
public function createNewExternalCatalog(ExternalProductCatalog $externalProductCatalog, array $data = []): array
{
$now = Time::now();
$associated = [];
Log::info('posted data - adding new ExternalProductCatalog');
Log::info(print_r($data, true));
$saveOptions = [
'associated' => $associated,
];
$externalProductCatalog = $this->ProductCatalogs->ExternalProductCatalogs->patchEntity($externalProductCatalog, $data, $saveOptions);
if ($externalProductCatalog->getErrors()) {
Log::debug(print_r('$externalProductCatalog->getErrors() next - failed to save from create new external product catalog', true));
Log::debug(print_r($externalProductCatalog->getErrors(), true));
}
$returnData = [
'entity' => $externalProductCatalog,
'result' => $this->ProductCatalogs->ExternalProductCatalogs->save($externalProductCatalog, $saveOptions),
'apiResults' => [],
];
if ($returnData['result'] && $this->externalCatalogManager) {
// $returnData['apiResults'] = $this->externalCatalogManager->newCatalogCreated($returnData['result']);
}
return $returnData;
}
/**
* @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;
}
}