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