commercetools-sdk-php-v2  master
The platform, import-api and ml-api PHP sdks generated from our api reference.
ByProjectKeyApiClientsByIDDelete.php
1 <?php
2 
3 declare(strict_types=1);
10 
22 use GuzzleHttp\ClientInterface;
23 use GuzzleHttp\Exception\ClientException;
24 use GuzzleHttp\Exception\RequestException;
25 use GuzzleHttp\Exception\ServerException;
26 use GuzzleHttp\Promise\PromiseInterface;
27 
28 use Psr\Http\Message\ResponseInterface;
29 
37 {
42  public function __construct(string $projectKey, string $ID, $body = null, array $headers = [], ClientInterface $client = null)
43  {
44  $uri = str_replace(['{projectKey}', '{ID}'], [$projectKey, $ID], '{projectKey}/api-clients/{ID}');
45  parent::__construct($client, 'DELETE', $uri, $headers, is_object($body) || is_array($body) ? json_encode($body) : $body);
46  }
47 
53  public function mapFromResponse(?ResponseInterface $response, string $resultType = null)
54  {
55  if (is_null($response)) {
56  return null;
57  }
58  if (is_null($resultType)) {
59  switch ($response->getStatusCode()) {
60  case '200':
61  $resultType = ApiClientModel::class;
62 
63  break;
64  case '400':
65  $resultType = ErrorResponseModel::class;
66 
67  break;
68  case '401':
69  $resultType = ErrorResponseModel::class;
70 
71  break;
72  case '403':
73  $resultType = ErrorResponseModel::class;
74 
75  break;
76  case '500':
77  $resultType = ErrorResponseModel::class;
78 
79  break;
80  case '502':
81  $resultType = ErrorResponseModel::class;
82 
83  break;
84  case '503':
85  $resultType = ErrorResponseModel::class;
86 
87  break;
88  default:
89  $resultType = JsonObjectModel::class;
90 
91  break;
92  }
93  }
94 
95  return $resultType::of($this->responseData($response));
96  }
97 
104  public function execute(array $options = [], string $resultType = null)
105  {
106  try {
107  $response = $this->send($options);
108  } catch (ServerException $e) {
109  $response = $e->getResponse();
110  $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
111  throw $e;
112  } catch (ClientException $e) {
113  $response = $e->getResponse();
114  $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
115  throw $e;
116  }
117 
118  return $this->mapFromResponse($response, $resultType);
119  }
120 
127  public function executeAsync(array $options = [], string $resultType = null)
128  {
129  return $this->sendAsync($options)->then(
130  function (ResponseInterface $response) use ($resultType) {
131  return $this->mapFromResponse($response, $resultType);
132  },
133  function (RequestException $e) use ($resultType) {
134  $response = $e->getResponse();
135  if ($e instanceof ServerException) {
136  $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
137  }
138  if ($e instanceof ClientException) {
139  $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
140  }
141  throw $e;
142  }
143  );
144  }
145 }
__construct(string $projectKey, string $ID, $body=null, array $headers=[], ClientInterface $client=null)
mapFromResponse(?ResponseInterface $response, string $resultType=null)
responseData(ResponseInterface $response)
Definition: ApiRequest.php:145
sendAsync(array $options=[])
Definition: ApiRequest.php:129
static createClientException(ClientException $e, ApiRequest $request, ?ResponseInterface $response, ?JsonObject $result)
static createServerException(ServerException $e, ApiRequest $request, ?ResponseInterface $response, ?JsonObject $result)