-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathClient.php
958 lines (838 loc) · 22.3 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
<?php
namespace Bigcommerce\Api;
use \Exception as Exception;
/**
* Bigcommerce API Client.
*/
class Client
{
static private $store_url;
static private $username;
static private $api_key;
static private $connection;
static private $resource;
static private $path_prefix = '/api/v2';
static private $oauth_api_path = 'https://api.bigcommerce.com/stores';
static private $auth_mode = 'oauth';
static private $oauth_client_id;
static private $oauth_access_token;
static private $oauth_store_hash;
static private $oauth_path_prefix = '/v2';
/**
* Full URL path to the configured store API.
*
* @var string
*/
static public $api_path;
/**
* Configure the API client with the required credentials.
*
* Requires a settings array to be passed in with the following keys:
*
* - store_url
* - username
* - api_key
*
* @param array $settings
*/
public static function configure($settings)
{
if (isset($settings['auth_mode']) && in_array($settings['auth_mode'], array('oauth', 'basic'))) {
self::$auth_mode = $settings['auth_mode'];
}
// Basic Auth specific settings
if (!isset($settings['store_url']) && self::$auth_mode === 'basic') {
throw new Exception("'store_url' must be provided");
}
if (!isset($settings['username']) && self::$auth_mode === 'basic') {
throw new Exception("'username' must be provided");
}
if (!isset($settings['api_key']) && self::$auth_mode === 'basic') {
throw new Exception("'api_key' must be provided");
}
// OAuth specific settings
if (!isset($settings['client_id']) && self::$auth_mode === 'oauth') {
throw new Exception("'client_id' must be provided");
}
if (!isset($settings['access_token']) && self::$auth_mode === 'oauth') {
throw new Exception("'access_token' must be provided");
}
if (!isset($settings['store_hash']) && self::$auth_mode === 'oauth') {
throw new Exception("'store_hash' must be provided");
}
if ('basic' === self::$auth_mode) {
self::$username = $settings['username'];
self::$api_key = $settings['api_key'];
self::$store_url = rtrim($settings['store_url'], '/');
self::$api_path = self::$store_url . self::$path_prefix;
} elseif ('oauth' === self::$auth_mode) {
self::$oauth_client_id = $settings['client_id'];
self::$oauth_access_token = $settings['access_token'];
self::$oauth_store_hash = $settings['store_hash'];
self::$api_path = self::$oauth_api_path . '/' . self::$oauth_store_hash . self::$oauth_path_prefix;
} else {
throw new Exception('Given Auth mode is not supported');
}
self::$connection = false;
}
/**
* Configure the API client to throw exceptions when HTTP errors occur.
*
* Note that network faults will always cause an exception to be thrown.
*/
public static function failOnError($option=true)
{
self::connection()->failOnError($option);
}
/**
* Return XML strings from the API instead of building objects.
*/
public static function useXml()
{
self::connection()->useXml();
}
/**
* Switch SSL certificate verification on requests.
*/
public static function verifyPeer($option=false)
{
self::connection()->verifyPeer($option);
}
/**
* Set which cipher to use during SSL requests.
*/
public static function setCipher($cipher='rsa_rc4_128_sha')
{
self::connection()->setCipher($cipher);
}
/**
* Connect to the internet through a proxy server.
*
* @param string $host host server
* @param string $port port
*/
public static function useProxy($host, $port=false)
{
self::connection()->useProxy($host, $port);
}
/**
* Get error message returned from the last API request if
* failOnError is false (default).
*
* @return string
*/
public static function getLastError()
{
return self::connection()->getLastError();
}
/**
* Get an instance of the HTTP connection object. Initializes
* the connection if it is not already active.
*
* @return Connection
*/
private static function connection()
{
if (!self::$connection) {
self::$connection = new Connection();
if ('basic' === self::$auth_mode) {
self::$connection->authenticate(self::$username, self::$api_key);
} else {
self::$connection->oAuthAuthenticate(self::$oauth_client_id, self::$oauth_access_token);
}
}
return self::$connection;
}
/**
* Get a collection result from the specified endpoint.
*
* @param string $path api endpoint
* @param string $resource resource class to map individual items
* @param array $fields additional key=>value properties to apply to the object
* @return mixed array|string mapped collection or XML string if useXml is true
*/
public static function getCollection($path, $resource='Resource')
{
$response = self::connection()->get(self::$api_path . $path);
return self::mapCollection($resource, $response);
}
/**
* Get a resource entity from the specified endpoint.
*
* @param string $path api endpoint
* @param string $resource resource class to map individual items
* @return mixed Resource|string resource object or XML string if useXml is true
*/
public static function getResource($path, $resource='Resource')
{
$response = self::connection()->get(self::$api_path . $path);
return self::mapResource($resource, $response);
}
/**
* Get a count value from the specified endpoint.
*
* @param string $path api endpoint
* @return mixed int|string count value or XML string if useXml is true
*/
public static function getCount($path)
{
$response = self::connection()->get(self::$api_path . $path);
if ($response == false || is_string($response)) return $response;
return $response->count;
}
/**
* Send a post request to create a resource on the specified collection.
*
* @param string $path api endpoint
* @param mixed $object object or XML string to create
*/
public static function createResource($path, $object)
{
if (is_array($object)) $object = (object)$object;
return self::connection()->post(self::$api_path . $path, $object);
}
/**
* Send a put request to update the specified resource.
*
* @param string $path api endpoint
* @param mixed $object object or XML string to update
*/
public static function updateResource($path, $object)
{
if (is_array($object)) $object = (object)$object;
return self::connection()->put(self::$api_path . $path, $object);
}
/**
* Send a delete request to remove the specified resource.
*
* @param string $path api endpoint
*/
public static function deleteResource($path)
{
return self::connection()->delete(self::$api_path . $path);
}
/**
* Internal method to wrap items in a collection to resource classes.
*
* @param string $resource name of the resource class
* @param array $object object collection
* @return array
*/
private static function mapCollection($resource, $object)
{
if ($object == false || is_string($object)) return $object;
$baseResource = __NAMESPACE__ . '\\' . $resource;
self::$resource = (class_exists($baseResource)) ? $baseResource : 'Bigcommerce\\Api\\Resources\\' . $resource;
return array_map(array('self', 'mapCollectionObject'), $object);
}
/**
* Callback for mapping collection objects resource classes.
*
* @param stdClass $object
* @return Resource
*/
private static function mapCollectionObject($object)
{
$class = self::$resource;
return new $class($object);
}
/**
* Map a single object to a resource class.
*
* @param string $resource name of the resource class
* @param stdClass $object
* @return Resource
*/
private static function mapResource($resource, $object)
{
if ($object == false || is_string($object)) return $object;
$baseResource = __NAMESPACE__ . '\\' . $resource;
$class = (class_exists($baseResource)) ? $baseResource : 'Bigcommerce\\Api\\Resources\\' . $resource;
return new $class($object);
}
/**
* Map object representing a count to an integer value.
*
* @param stdClass $object
* @return int
*/
private static function mapCount($object)
{
if ($object == false || is_string($object)) return $object;
return $object->count;
}
/**
* Pings the time endpoint to test the connection to a store.
*
* @return DateTime
*/
public static function getTime()
{
$response = self::connection()->get(self::$api_path . '/time');
if ($response == false || is_string($response)) return $response;
return new \DateTime("@{$response->time}");
}
/**
* Returns the default collection of products.
*
* @param array $filter
* @return mixed array|string list of products or XML string if useXml is true
*/
public static function getProducts($filter=false)
{
$filter = Filter::create($filter);
return self::getCollection('/products' . $filter->toQuery(), 'Product');
}
/**
* Gets collection of images for a product.
*
* @param int $id product id
* @return mixed array|string list of products or XML string if useXml is true
*/
public static function getProductImages($id)
{
return self::getResource('/products/' . $id . '/images/', 'ProductImage');
}
/**
* Gets collection of custom fields for a product.
*
* @param int $id product ID
* @return mixed array|string list of products or XML string if useXml is true
*/
public static function getProductCustomFields($id)
{
return self::getCollection('/products/' . $id . '/custom_fields', 'ProductCustomField');
}
/**
* Returns a single custom field by given id
* @param int $product_id product id
* @param int $id custom field id
* @return ProductCustomField|bool Returns ProductCustomField if exists, false if not exists
*/
public static function getProductCustomField($product_id, $id)
{
return self::getResource('/products/' . $product_id . '/custom_fields/' . $id, 'ProductCustomField');
}
/**
* Create a new custom field for a given product.
*
* @param int $product_id product id
* @param int $id custom field id
* @param mixed $object fields to create
* @return Object Object with `id`, `product_id`, `name` and `text` keys
*/
public static function createProductCustomField($product_id, $object)
{
return self::createResource('/products/' . $product_id . '/custom_fields', $object);
}
/**
* Update the given custom field.
*
* @param int $product_id product id
* @param int $id custom field id
* @param mixed $object custom field to update
*/
public static function updateProductCustomField($product_id, $id, $object)
{
return self::updateResource('/products/' . $product_id . '/custom_fields/' . $id, $object);
}
/**
* Delete the given custom field.
*
* @param int $product_id product id
* @param int $id custom field id
*/
public static function deleteProductCustomField($product_id, $id)
{
return self::deleteResource('/products/' . $product_id . '/custom_fields/' . $id);
}
/**
* Returns the total number of products in the collection.
*
* @param array $filter
* @return mixed int|string number of products or XML string if useXml is true
*/
public static function getProductsCount($filter=false)
{
$filter = Filter::create($filter);
return self::getCount('/products/count' . $filter->toQuery());
}
/**
* Returns a single product resource by the given id.
*
* @param int $id product id
* @return Product|string
*/
public static function getProduct($id)
{
return self::getResource('/products/' . $id, 'Product');
}
/**
* Create a new product.
*
* @param mixed $object fields to create
*/
public static function createProduct($object)
{
return self::createResource('/products', $object);
}
/**
* Update the given product.
*
* @param int $id product id
* @param mixed $object fields to update
*/
public static function updateProduct($id, $object)
{
return self::updateResource('/products/' . $id, $object);
}
/**
* Delete the given product.
*
* @param int $id product id
*/
public static function deleteProduct($id)
{
return self::deleteResource('/products/' . $id);
}
/**
* Return the collection of options.
*
* @param array $filter
* @return array
*/
public static function getOptions($filter=false)
{
$filter = Filter::create($filter);
return self::getCollection('/options' . $filter->toQuery(), 'Option');
}
/** create options **/
public static function createOptions($object)
{
return self::createResource('/options', $object);
}
/**
* Return the number of options in the collection
*
* @return int
*/
public static function getOptionsCount()
{
return self::getCount('/options/count');
}
/**
* Return a single option by given id.
*
* @param int $id option id
* @return Option
*/
public static function getOption($id)
{
return self::getResource('/options/' . $id, 'Option');
}
/**
* Delete the given option.
*
* @param int $id option id
*/
public static function deleteOption($id)
{
return self::deleteResource('/options/' . $id);
}
/**
* Return a single value for an option.
*
* @param int $option_id option id
* @param int $id value id
* @return OptionValue
*/
public static function getOptionValue($option_id, $id)
{
return self::getResource('/options/' . $option_id . '/values/' . $id, 'OptionValue');
}
/**
* Return the collection of all option values.
*
* @param mixed $filter
* @return array
*/
public static function getOptionValues($filter=false)
{
$filter = Filter::create($filter);
return self::getCollection('/options/values' . $filter->toQuery(), 'OptionValue');
}
/**
* The collection of categories.
*
* @param mixed $filter
* @return array
*/
public static function getCategories($filter=false)
{
$filter = Filter::create($filter);
return self::getCollection('/categories' . $filter->toQuery(), 'Category');
}
/**
* The number of categories in the collection.
*
* @param mixed $filter
* @return int
*/
public static function getCategoriesCount($filter=false)
{
$filter = Filter::create($filter);
return self::getCount('/categories/count' . $filter->toQuery());
}
/**
* A single category by given id.
*
* @param int $id category id
* @return Category
*/
public static function getCategory($id)
{
return self::getResource('/categories/' . $id, 'Category');
}
/**
* Create a new category from the given data.
*
* @param mixed $object
*/
public static function createCategory($object)
{
return self::createResource('/categories/', $object);
}
/**
* Update the given category.
*
* @param int $id category id
* @param mixed $object
*/
public static function updateCategory($id, $object)
{
return self::updateResource('/categories/' . $id, $object);
}
/**
* Delete the given category.
*
* @param int $id category id
*/
public static function deleteCategory($id)
{
return self::deleteResource('/categories/' . $id);
}
/**
* The collection of brands.
*
* @param mixed $filter
* @return array
*/
public static function getBrands($filter=false)
{
$filter = Filter::create($filter);
return self::getCollection('/brands' . $filter->toQuery(), 'Brand');
}
/**
* The total number of brands in the collection.
*
* @param mixed $filter
* @return int
*/
public static function getBrandsCount($filter=false)
{
$filter = Filter::create($filter);
return self::getCount('/brands/count' . $filter->toQuery());
}
/**
* A single brand by given id.
*
* @param int $id brand id
* @return Brand
*/
public static function getBrand($id)
{
return self::getResource('/brands/' . $id, 'Brand');
}
/**
* Create a new brand from the given data.
*
* @param mixed $object
*/
public static function createBrand($object)
{
return self::createResource('/brands', $object);
}
/**
* Update the given brand.
*
* @param int $id brand id
* @param mixed $object
*/
public static function updateBrand($id, $object)
{
return self::updateResource('/brands/' . $id, $object);
}
/**
* Delete the given brand.
*
* @param int $id brand id
*/
public static function deleteBrand($id)
{
return self::deleteResource('/brands/' . $id);
}
/**
* The collection of orders.
*
* @param mixed $filter
* @return array
*/
public static function getOrders($filter=false)
{
$filter = Filter::create($filter);
return self::getCollection('/orders' . $filter->toQuery(), 'Order');
}
/**
* The number of orders in the collection.
*
* @return int
*/
public static function getOrdersCount()
{
return self::getCount('/orders/count');
}
/**
* A single order.
*
* @param int $id order id
* @return Order
*/
public static function getOrder($id)
{
return self::getResource('/orders/' . $id, 'Order');
}
/**
* Delete the given order (unlike in the Control Panel, this will permanently
* delete the order).
*
* @param int $id order id
*/
public static function deleteOrder($id)
{
return self::deleteResource('/orders/' . $id);
}
/**
* Create an order
**/
public static function createOrder($object)
{
return self::createResource('/orders', $object);
}
/**
* The list of customers.
*
* @param mixed $filter
* @return array
*/
public static function getCustomers($filter=false)
{
$filter = Filter::create($filter);
return self::getCollection('/customers' . $filter->toQuery(), 'Customer');
}
/**
* The total number of customers in the collection.
*
* @param mixed $filter
* @return int
*/
public static function getCustomersCount($filter=false)
{
$filter = Filter::create($filter);
return self::getCount('/customers/count' . $filter->toQuery());
}
/**
* Bulk delete customers.
*
* @param mixed $filter
* @return array
*/
public static function deleteCustomers($filter=false)
{
$filter = Filter::create($filter);
return self::deleteResource('/customers' . $filter->toQuery());
}
/**
* A single customer by given id.
*
* @param int $id customer id
* @return Customer
*/
public static function getCustomer($id)
{
return self::getResource('/customers/' . $id, 'Customer');
}
/**
* Create a new customer from the given data.
*
* @param mixed $object
*/
public static function createCustomer($object)
{
return self::createResource('/customers', $object);
}
/**
* Update the given customer.
*
* @param int $id customer id
* @param mixed $object
*/
public static function updateCustomer($id, $object)
{
return self::updateResource('/customers/' . $id, $object);
}
/**
* Delete the given customer.
*
* @param int $id customer id
*/
public static function deleteCustomer($id)
{
return self::deleteResource('/customers/' . $id);
}
/**
* A list of addresses belonging to the given customer.
*
* @param int $id customer id
* @return array
*/
public static function getCustomerAddresses($id)
{
return self::getCollection('/customers/' . $id . '/addresses', 'Address');
}
/**
* Returns the collection of option sets.
*
* @param array $filter
* @return array
*/
public static function getOptionSets($filter=false)
{
$filter = Filter::create($filter);
return self::getCollection('/optionsets' . $filter->toQuery(), 'OptionSet');
}
/** create optionsets **/
public static function createOptionsets($object)
{
return self::createResource('/optionsets', $object);
}
/** connect optionsets options **/
public static function createOptionsets_Options($object, $id)
{
return self::createResource('/optionsets/'.$id.'/options', $object);
}
/**
* Returns the total number of option sets in the collection.
*
* @return int
*/
public static function getOptionSetsCount()
{
return self::getCount('/optionsets/count');
}
/**
* A single option set by given id.
*
* @param int $id option set id
* @return OptionSet
*/
public static function getOptionSet($id)
{
return self::getResource('/optionsets/' . $id, 'OptionSet');
}
/**
* Status codes used to represent the state of an order.
*
* @return array
*/
public static function getOrderStatuses()
{
return self::getCollection('/orderstatuses', 'OrderStatus');
}
/* product skus */
public static function getSkus($filter=false)
{
$filter = Filter::create($filter);
return self::getCollection('/products/skus' . $filter->toQuery(), 'Sku');
}
public static function createSku($object)
{
return self::createResource('/product/skus', $object);
}
public static function updateSku($id, $object)
{
return self::updateResource('/product/skus' . $id, $object);
}
/* coupons */
public static function getCoupons($filter=false)
{
$filter = Filter::create($filter);
return self::getCollection('/coupons' . $filter->toQuery(), 'Sku');
}
public static function createCoupon($object)
{
return self::createResource('/coupons', $object);
}
public static function updateCoupon($id, $object)
{
return self::updateResource('/coupons/' . $id, $object);
}
public static function listWebHook()
{
return self::getResource('/hooks');
}
public static function getWebHook($id)
{
return self::getResource('/hooks/' . $id);
}
public static function createWebHook($object)
{
return self::createResource('/hooks', $object);
}
public static function updateWebHook($id, $object)
{
return self::updateResource('/hooks/' . $id, $object);
}
public static function deleteWebHook($id)
{
return self::deleteResource('/hooks/' . $id);
}
/**
* The request logs with usage history statistics.
*/
public static function getRequestLogs()
{
return self::getCollection('/requestlogs');
}
public static function getStore()
{
$response = self::connection()->get(self::$api_path . '/store');
return $response;
}
/**
* The number of requests remaining at the current time. Based on the
* last request that was fetched within the current script. If no
* requests have been made, pings the time endpoint to get the value.
*
* @return int
*/
public static function getRequestsRemaining()
{
$limit = self::connection()->getHeader('X-BC-ApiLimit-Remaining');
if (!$limit) {
$result = self::getTime();
if (!$result) return false;
$limit = self::connection()->getHeader('X-BC-ApiLimit-Remaining');
}
return intval($limit);
}
}