|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BigCommerce\ApiV3\Customers; |
| 4 | + |
| 5 | +use BigCommerce\ApiV3\Api\ResourceApi; |
| 6 | +use BigCommerce\ApiV3\ResponseModels\Customer\CustomerResponse; |
| 7 | +use BigCommerce\ApiV3\ResponseModels\Customer\CustomersResponse; |
| 8 | +use BigCommerce\ApiV3\ResourceModels\Customer\Customer; |
| 9 | +use GuzzleHttp\Exception\GuzzleException; |
| 10 | +use GuzzleHttp\RequestOptions; |
| 11 | +use Psr\Http\Message\ResponseInterface; |
| 12 | +use UnexpectedValueException; |
| 13 | + |
| 14 | +class CustomersApi extends ResourceApi |
| 15 | +{ |
| 16 | + public const RESOURCE_NAME = 'customers'; |
| 17 | + public const CUSTOMERS_ENDPOINT = 'customers'; |
| 18 | + public const CUSTOMER_ENDPOINT = 'customer/%d'; |
| 19 | + |
| 20 | + |
| 21 | + public function get(): CustomerResponse |
| 22 | + { |
| 23 | + return new CustomerResponse($this->getResource()); |
| 24 | + } |
| 25 | + |
| 26 | + public function getAll(array $filters = [], int $page = 1, int $limit = 250): CustomersResponse |
| 27 | + { |
| 28 | + return new CustomersResponse($this->getAllResources($filters, $page, $limit)); |
| 29 | + } |
| 30 | + |
| 31 | + public function getByEmail($email): ?Customer |
| 32 | + { |
| 33 | + $customers = $this->getAll(['email:in' => $email])->getCustomers(); |
| 34 | + |
| 35 | + if (!$customers[0]) { |
| 36 | + return null; |
| 37 | + } elseif (count($customers) > 1) { |
| 38 | + throw new UnexpectedValueException("There are more than one customer with the email address $email"); |
| 39 | + } |
| 40 | + |
| 41 | + return $customers[0]; |
| 42 | + } |
| 43 | + |
| 44 | + /** the Customers API allows us to delete a batch of customers at a time by passing an a query string containing a |
| 45 | + * list of Customer IDs |
| 46 | + * @param array $ids |
| 47 | + * @return ResponseInterface |
| 48 | + * @throws GuzzleException |
| 49 | + */ |
| 50 | + public function deleteIn(array $ids): ResponseInterface |
| 51 | + { |
| 52 | + $ids = implode(",", $ids); |
| 53 | + $resource = ["id:in" => $ids]; |
| 54 | + |
| 55 | + return $this->getClient()->getRestClient()->delete( |
| 56 | + $this->multipleResourcesEndpoint(), |
| 57 | + [ |
| 58 | + RequestOptions::QUERY => $resource, |
| 59 | + ] |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + public function create(Customer $category): CustomerResponse |
| 64 | + { |
| 65 | + return new CustomerResponse($this->createResource($category)); |
| 66 | + } |
| 67 | + |
| 68 | + public function update(Customer $category): CustomerResponse |
| 69 | + { |
| 70 | + return new CustomerResponse($this->updateResource($category)); |
| 71 | + } |
| 72 | + |
| 73 | + protected function singleResourceEndpoint(): string |
| 74 | + { |
| 75 | + return self::CUSTOMER_ENDPOINT; |
| 76 | + } |
| 77 | + |
| 78 | + protected function multipleResourcesEndpoint(): string |
| 79 | + { |
| 80 | + return self::CUSTOMERS_ENDPOINT; |
| 81 | + } |
| 82 | + |
| 83 | + protected function resourceName(): string |
| 84 | + { |
| 85 | + return self::RESOURCE_NAME; |
| 86 | + } |
| 87 | +} |
0 commit comments