Source of file BrandEntity.php
Size: 2,856 Bytes - Last Modified: 2021-12-24T05:18:55+00:00
/var/www/docs.ssmods.com/process/src/src/Entities/BrandEntity.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 | <?php namespace IQnection\BigCommerceApp\Entities; use SilverStripe\ORM\ArrayList; use SilverStripe\View\ArrayData; use IQnection\BigCommerceApp\Client; use SilverStripe\Core\Injector\Injector; class BrandEntity extends Entity { use \IQnection\BigCommerceApp\Traits\Entity, \IQnection\BigCommerceApp\Traits\Cacheable; private static $client_class = \BigCommerce\Api\v3\Api\CatalogApi::class; private static $cache_name = 'bigcommerce-brands'; public function Sync() { $apiClient = $this->ApiClient(); $data = $this->ApiData(); if ($data['id']) { $response = $apiClient->updateBrand($data['id'], new \BigCommerce\Api\v3\Model\BrandPut($data)); } else { $response = $apiClient->createBrand(new \BigCommerce\Api\v3\Model\BrandPost($data)); } $this->loadApiData($response->getData()); return $this; } public static function getBrands($refresh = false) { return self::getAll($refresh); } public static function getAll($refresh = false) { $cacheName = self::generateCacheKey(self::Config()->get('cache_name')); $cachedData = self::fromCache($cacheName); if ( (!self::isCached($cacheName)) || (!$cachedData) || ($refresh) ) { $cachedData = ArrayList::create(); $inst = self::singleton(); $apiClient = $inst->ApiClient(); $page = 1; $apiDataResponse = $apiClient->getBrands(['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->getBrands(['page' => $page, 'limit' => 100]); } self::toCache($cacheName, $cachedData); } return $cachedData; } public static function getById($id) { $cacheName = self::generateCacheKey(self::Config()->get('cache_name').__FUNCTION__.$id); $cachedData = self::fromCache($cacheName); if ( (!self::isCached($cacheName)) || (!$cachedData) || ($refresh) ) { $inst = Injector::inst()->create(static::class, []); $apiClient = $inst->ApiClient(); $apiResponse = $apiClient->getBrandById($id); $apiRecords = $apiResponse->getData(); $inst->loadApiData($apiRecord); self::toCache($cacheName, $inst); } return $cachedData; } public static function forDropdown() { $cacheName = self::generateCacheKey(self::Config()->get('cache_name').__FUNCTION__); $cachedData = self::fromCache($cacheName); if ( (!self::isCached($cacheName)) || (!$cachedData) || ($refresh) ) { $cachedData = []; foreach(self::getBrands() as $bcCategory) { $cachedData[$bcCategory->id] = $bcCategory->name; } self::toCache($cacheName, $cachedData); } return $cachedData; } } |