| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Dynamic\FoxyStripe\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Foxy\FoxyClient\FoxyClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use GuzzleHttp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Core\Injector\Injector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class FoxyStripeClient | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     private static $table_name = 'FS_FoxyStripeClient'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $current_store; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $item_categories_url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     private $item_categories; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * FoxyStripeClient constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @throws \Psr\Container\NotFoundExceptionInterface | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 49 |  |     public function __construct() | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $config = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 49 |  |             'use_sandbox' => false, | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 49 |  |         if ($setting = FoxyStripeSetting::current_foxystripe_setting()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 49 |  |             $config['client_id'] = $setting->client_id; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 49 |  |             $config['client_secret'] = $setting->client_secret; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 49 |  |             $config['refresh_token'] = $setting->refresh_token; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 49 |  |             $config['access_token'] = $setting->access_token; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 49 |  |             $config['access_token_expires'] = 7200; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $guzzle_config = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 49 |  |             'defaults' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                 'debug' => false, | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                 'exceptions' => false, | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         /* | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |          * Set up our Guzzle Client | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |          */ | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 49 |  |         $guzzle = new Client($guzzle_config); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         //CacheSubscriber::attach($guzzle); // todo add caching middleware guzzle-cache-middleware | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         /* | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |          * Get our FoxyClient | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |          */ | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 49 |  |         $fc = new FoxyClient($guzzle, $config); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 49 |  |         $this->setClient($fc); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 49 |  |         $this->setCurrentStore(); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 49 |  |         $this->setItemCategoriesURL(); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 49 |  |         $this->setItemCategories(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 49 |  |     public function getClient() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 49 |  |         return $this->client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @param $client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 49 |  |     public function setClient($client) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 49 |  |         $this->client = $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 49 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @throws \SilverStripe\ORM\ValidationException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 11 |  |     public static function is_valid() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 11 |  |         $config = FoxyStripeSetting::current_foxystripe_setting(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 11 |  |         return $config->EnableAPI && | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 11 |  |             $config->client_id && | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 11 |  |             $config->client_secret && | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 11 |  |             $config->refresh_token && | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 11 |  |             $config->access_token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 49 |  |     public function getCurrentStore() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 49 |  |         return $this->current_store; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @throws \SilverStripe\ORM\ValidationException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 49 |  |     public function setCurrentStore() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 49 |  |         $client = $this->getClient(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 49 |  |         $config = FoxyStripeSetting::current_foxystripe_setting(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 49 |  |         $errors = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 49 |  |             'store_domain' => $config->StoreName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 49 |  |         if ($client && $result = $client->get()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 49 |  |             $errors = array_merge($errors, $client->getErrors($result)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 49 |  |             if ($reporting_uri = $client->getLink('fx:reporting')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                 $errors = array_merge($errors, $client->getErrors($reporting_uri)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                 if ($result = $client->get($reporting_uri)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                     $errors = array_merge($errors, $client->getErrors($result)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                     if ($store_exists_uri = $client->getLink('fx:reporting_store_domain_exists')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                         $errors = array_merge($errors, $client->getErrors($store_exists_uri)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                         if ($result = $client->get($store_exists_uri, $data)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                             $errors = array_merge($errors, $client->getErrors($result)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                             if ($store = $client->getLink('fx:store')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                                 $errors = array_merge($errors, $client->getErrors($store)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                                 $this->current_store = $store; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 49 |  |             if (count($errors)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |                 Injector::inst()->get(LoggerInterface::class)->error('setCurrentStore errors - ' . json_encode($errors)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * @param $uri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * @param null $data | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     public function post($uri, $data = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         $this->getClient()->post($uri, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      * @param $uri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * @param null $data | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     public function patch($uri, $data = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         $this->getClient()->patch($uri, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      * @throws \Psr\Container\NotFoundExceptionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     public function updateStore($data = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         $client = $this->getClient(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         $errors = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         $result = $client->patch($this->getCurrentStore(), $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         $errors = array_merge($errors, $client->getErrors($result)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         if (count($errors)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |             Injector::inst()->get(LoggerInterface::class)->error('updateStore errors - ' . json_encode($errors)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 | 49 |  |     public function getItemCategoriesURL() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 | 49 |  |         return $this->item_categories_url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |      * @throws \Psr\Container\NotFoundExceptionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 | 49 |  |     public function setItemCategoriesURL() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 | 49 |  |         $client = $this->getClient(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 | 49 |  |         $errors = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 | 49 |  |         if ($client) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 | 49 |  |             $result = $client->get($this->getCurrentStore()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 | 49 |  |             if (isset($result['_links']['fx:item_categories']['href'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |                 $this->item_categories_url = $result['_links']['fx:item_categories']['href']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 | 49 |  |             $errors = array_merge($errors, $client->getErrors($result)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 | 49 |  |             if (count($errors)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |                 Injector::inst() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |                     ->get(LoggerInterface::class)->error('setItemCategoriesURL errors - ' . json_encode($errors)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |     public function getItemCategories() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |         return $this->item_categories; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |      * @throws \Psr\Container\NotFoundExceptionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 | 49 |  |     public function setItemCategories() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 | 49 |  |         $client = $this->getClient(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 | 49 |  |         $errors = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 | 49 |  |         if ($client) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 | 49 |  |             $result = $client->get($this->getItemCategoriesURL()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 | 49 |  |             $this->item_categories = $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 | 49 |  |             $errors = array_merge($errors, $client->getErrors($result)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 | 49 |  |             if (count($errors)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |                 Injector::inst() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |                     ->get(LoggerInterface::class)->error('setItemCategories errors - ' . json_encode($errors)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |      * @param $code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |      * @throws \Psr\Container\NotFoundExceptionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 | 10 |  |     public function getCategory($code) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 | 10 |  |         if ($categoriesURL = $this->getItemCategoriesURL()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |             $client = $this->getClient(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |             $errors = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |             $data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |                 'code' => $code, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |             if ($result = $client->get($categoriesURL, $data)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |                 if (count($result['_embedded']['fx:item_categories']) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |                     $category = $result['_embedded']['fx:item_categories'][0]['_links']['self']['href']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |                     return $category; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |                 $errors = array_merge($errors, $client->getErrors($result)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |                 if (count($errors)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |                     Injector::inst()->get(LoggerInterface::class)->error('getCategory errors - ' . json_encode($errors)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 | 10 |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |      * @throws \Psr\Container\NotFoundExceptionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 | 9 |  |     public function putCategory($data = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 | 9 |  |         $client = $this->getClient(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 | 9 |  |         $errors = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 | 9 |  |         if ($client) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 | 9 |  |             if ($category = $this->getCategory($data['code'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |                 $result = $client->patch($category, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 | 9 |  |                 $result = $client->post($this->getItemCategoriesURL(), $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 | 9 |  |             $errors = array_merge($errors, $client->getErrors($result)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 | 9 |  |             if (count($errors)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |                 Injector::inst()->get(LoggerInterface::class)->error('putCategory errors - ' . json_encode($errors)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |      * @throws \Psr\Container\NotFoundExceptionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 | 1 |  |     public function deleteCategory($data = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 | 1 |  |         $client = $this->getClient(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 | 1 |  |         $errors = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 | 1 |  |         if ($category = $this->getCategory($data['code'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |             $result = $client->delete($category); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |             $errors = array_merge($errors, $client->getErrors($result)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |             if (count($errors)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |                 Injector::inst()->get(LoggerInterface::class)->error('deleteCategory errors - ' . json_encode($errors)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 328 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 329 |  |  |  |