Source of file ModifierEntity.php
Size: 3,405 Bytes - Last Modified: 2021-12-24T05:18:55+00:00
/var/www/docs.ssmods.com/process/src/src/Entities/ModifierEntity.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 | <?php namespace IQnection\BigCommerceApp\Entities; use SilverStripe\ORM\ArrayList; use SilverStripe\View\ArrayData; use SilverStripe\ORM\Hierarchy\Hierarchy; use SilverStripe\ORM\DataObject; use IQnection\BigCommerceApp\Client; use SilverStripe\Core\Injector\Injector; class ModifierEntity extends Entity { use \IQnection\BigCommerceApp\Traits\Cacheable; private static $client_class = \BigCommerce\Api\v3\Api\CatalogApi::class; private static $cache_name = 'bigcommerce-modifiers'; private static $_childEntitiesMap = [ 'option_values' => ModifierValueEntity::class ]; public function ApiData() { $data = parent::ApiData(); // if (is_array($data['option_values'])) // { // $apiOptionValues = []; // foreach($this->option_values as $optionValue) // { // $apiOptionValues[] = $optionValue->ApiData(); // } // $data['option_values'] = $apiOptionValues; // } if (!$data['config']) { $data['config'] = []; } return $data; } public function loadApiData($data) { parent::loadApiData($data); if (!$this->option_values) { $this->option_values = ArrayList::create(); } return $this; } public function Sync() { $apiClient = $this->ApiClient(); $data = $this->ApiData(); $id = $data['id'] ? $data['id'] : ($data['BigID'] ? $data['BigID'] : null); if ( ($id) && ($data['product_id']) ) { $response = $apiClient->updateModifier($data['product_id'], $id, new \BigCommerce\Api\v3\Model\ModifierPut($data)); } else { $response = $apiClient->createModifier($data['product_id'],new \BigCommerce\Api\v3\Model\ModifierPost($data)); } $this->loadApiData($response->getData()); return $this; } /* public function modifierValues($refresh = false) { $cacheName = self::generateCacheKey(self::Config()->get('cache_name'), $this->id, $this->product_id); $cachedData = self::fromCache($cacheName); if ( (!self::isCached($cacheName)) || (!$cachedData) || ($refresh) ) { $cachedData = ArrayList::create(); if ($this->id) { $apiClient = $this->ApiClient(); $apiDataResponse = $apiClient->getModifierValues($productID, $this->id); foreach($apiDataResponse->getData() as $apiData) { $newInst = Injector::inst()->create(ModifierValue::class, []); $newInst->loadApiData($apiData); $cachedData->push($newInst); } } self::toCache($cacheName, $cachedData); } return $cachedData; }*/ public static function getModifiers($productID,$refresh = false) { $cacheName = self::generateCacheKey(self::Config()->get('cache_name').$productID); $cachedData = self::fromCache($cacheName); if ( (!self::isCached($cacheName)) || (!$cachedData) || ($refresh) ) { $cachedData = ArrayList::create(); $inst = self::singleton(); $apiClient = $inst->ApiClient(); $page = 1; $apiDataResponse = $apiClient->getModifiers($productID,['page' => $page, 'limit' => 100]); $responseMeta = $apiDataResponse->getMeta(); while(($apiDatas = $apiDataResponse->getData()) && (count($apiDatas))) { foreach($apiDatas as $apiData) { $newInst = Injector::inst()->create(static::class, []); $newInst->loadApiData($apiData); $cachedData->push($newInst); } $page++; $apiDataResponse = $apiClient->getModifiers($productID,['page' => $page, 'limit' => 100]); } self::toCache($cacheName, $cachedData); } return $cachedData; } } |