Source of file FoxyStripeClient.php
Size: 8,546 Bytes - Last Modified: 2021-12-24T06:45:37+00:00
/var/www/docs.ssmods.com/process/src/src/Model/FoxyStripeClient.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 | <?php namespace Dynamic\FoxyStripe\Model; use Foxy\FoxyClient\FoxyClient; use GuzzleHttp\Client; use Psr\Log\LoggerInterface; use SilverStripe\Core\Injector\Injector; class FoxyStripeClient { /** * @var string */ private static $table_name = 'FS_FoxyStripeClient'; /** * @var */ private $client; /** * @var */ private $current_store; /** * @var */ private $item_categories_url; /** * @var */ private $item_categories; /** * FoxyStripeClient constructor. * @throws \Psr\Container\NotFoundExceptionInterface */ public function __construct() { $config = array( 'use_sandbox' => false, ); if ($setting = FoxyStripeSetting::current_foxystripe_setting()) { $config['client_id'] = $setting->client_id; $config['client_secret'] = $setting->client_secret; $config['refresh_token'] = $setting->refresh_token; $config['access_token'] = $setting->access_token; } $guzzle_config = array( 'defaults' => array( 'debug' => false, 'exceptions' => false, ), ); /* * Set up our Guzzle Client */ $guzzle = new Client($guzzle_config); //CacheSubscriber::attach($guzzle); // todo add caching middleware guzzle-cache-middleware /* * Get our FoxyClient */ $fc = new FoxyClient($guzzle, $config); $this->setClient($fc); $this->setCurrentStore(); $this->setItemCategoriesURL(); $this->setItemCategories(); } /** * @return mixed */ public function getClient() { return $this->client; } /** * @param $client * * @return $this */ public function setClient($client) { $this->client = $client; return $this; } /** * @return bool * @throws \SilverStripe\ORM\ValidationException */ public static function is_valid() { $config = FoxyStripeSetting::current_foxystripe_setting(); return $config->EnableAPI && $config->client_id && $config->client_secret && $config->refresh_token && $config->access_token; } /** * @return mixed */ public function getCurrentStore() { return $this->current_store; } /** * @throws \SilverStripe\ORM\ValidationException */ public function setCurrentStore() { $client = $this->getClient(); $config = FoxyStripeSetting::current_foxystripe_setting(); $errors = array(); $data = array( 'store_domain' => $config->StoreName, ); if ($client && $result = $client->get()) { $errors = array_merge($errors, $client->getErrors($result)); if ($reporting_uri = $client->getLink('fx:reporting')) { $errors = array_merge($errors, $client->getErrors($reporting_uri)); if ($result = $client->get($reporting_uri)) { $errors = array_merge($errors, $client->getErrors($result)); if ($store_exists_uri = $client->getLink('fx:reporting_store_domain_exists')) { $errors = array_merge($errors, $client->getErrors($store_exists_uri)); if ($result = $client->get($store_exists_uri, $data)) { $errors = array_merge($errors, $client->getErrors($result)); if ($store = $client->getLink('fx:store')) { $errors = array_merge($errors, $client->getErrors($store)); $this->current_store = $store; } } } } } if (count($errors)) { Injector::inst()->get(LoggerInterface::class)->error('setCurrentStore errors - '.json_encode($errors)); } } } /** * @param array $data * * @throws \Psr\Container\NotFoundExceptionInterface */ public function updateStore($data = []) { $client = $this->getClient(); $errors = []; $result = $client->patch($this->getCurrentStore(), $data); $errors = array_merge($errors, $client->getErrors($result)); if (count($errors)) { Injector::inst()->get(LoggerInterface::class)->error('updateStore errors - '.json_encode($errors)); } } /** * @return mixed */ public function getItemCategoriesURL() { return $this->item_categories_url; } /** * @throws \Psr\Container\NotFoundExceptionInterface */ public function setItemCategoriesURL() { $client = $this->getClient(); $errors = []; if ($client) { $result = $client->get($this->getCurrentStore()); if (isset($result['_links']['fx:item_categories']['href'])) { $this->item_categories_url = $result['_links']['fx:item_categories']['href']; } $errors = array_merge($errors, $client->getErrors($result)); if (count($errors)) { Injector::inst() ->get(LoggerInterface::class)->error('setItemCategoriesURL errors - '.json_encode($errors)); } } } /** * @return mixed */ public function getItemCategories() { return $this->item_categories; } /** * @throws \Psr\Container\NotFoundExceptionInterface */ public function setItemCategories() { $client = $this->getClient(); $errors = []; if ($client) { $result = $client->get($this->getItemCategoriesURL()); $this->item_categories = $result; $errors = array_merge($errors, $client->getErrors($result)); if (count($errors)) { Injector::inst() ->get(LoggerInterface::class)->error('setItemCategories errors - '.json_encode($errors)); } } } /** * @param $code * * @return bool * * @throws \Psr\Container\NotFoundExceptionInterface */ public function getCategory($code) { if ($categoriesURL = $this->getItemCategoriesURL()) { $client = $this->getClient(); $errors = []; $data = [ 'code' => $code, ]; if ($result = $client->get($categoriesURL, $data)) { if (count($result['_embedded']['fx:item_categories']) > 0) { $category = $result['_embedded']['fx:item_categories'][0]['_links']['self']['href']; return $category; } $errors = array_merge($errors, $client->getErrors($result)); if (count($errors)) { Injector::inst()->get(LoggerInterface::class)->error('getCategory errors - '.json_encode($errors)); } } } return false; } /** * @param array $data * * @throws \Psr\Container\NotFoundExceptionInterface */ public function putCategory($data = []) { $client = $this->getClient(); $errors = []; if ($client) { if ($category = $this->getCategory($data['code'])) { $result = $client->patch($category, $data); } else { $result = $client->post($this->getItemCategoriesURL(), $data); } $errors = array_merge($errors, $client->getErrors($result)); if (count($errors)) { Injector::inst()->get(LoggerInterface::class)->error('putCategory errors - '.json_encode($errors)); } } } /** * @param array $data * * @throws \Psr\Container\NotFoundExceptionInterface */ public function deleteCategory($data = []) { $client = $this->getClient(); $errors = []; if ($category = $this->getCategory($data['code'])) { $result = $client->delete($category); $errors = array_merge($errors, $client->getErrors($result)); if (count($errors)) { Injector::inst()->get(LoggerInterface::class)->error('deleteCategory errors - '.json_encode($errors)); } } } } |