commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
ByProjectKeyCustomObjectsPost.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 
38 {
43  public function __construct(string $projectKey, $body = null, array $headers = [], ClientInterface $client = null)
44  {
45  $uri = str_replace(['{projectKey}'], [$projectKey], '{projectKey}/custom-objects');
46  parent::__construct($client, 'POST', $uri, $headers, is_object($body) || is_array($body) ? json_encode($body) : $body);
47  }
48 
54  public function mapFromResponse(?ResponseInterface $response, string $resultType = null)
55  {
56  if (is_null($response)) {
57  return null;
58  }
59  if (is_null($resultType)) {
60  switch ($response->getStatusCode()) {
61  case '200':
62  $resultType = CustomObjectModel::class;
63 
64  break;
65  case '201':
66  $resultType = CustomObjectModel::class;
67 
68  break;
69  case '400':
70  $resultType = ErrorResponseModel::class;
71 
72  break;
73  case '401':
74  $resultType = ErrorResponseModel::class;
75 
76  break;
77  case '403':
78  $resultType = ErrorResponseModel::class;
79 
80  break;
81  case '500':
82  $resultType = ErrorResponseModel::class;
83 
84  break;
85  case '502':
86  $resultType = ErrorResponseModel::class;
87 
88  break;
89  case '503':
90  $resultType = ErrorResponseModel::class;
91 
92  break;
93  default:
94  $resultType = JsonObjectModel::class;
95 
96  break;
97  }
98  }
99 
100  return $resultType::of($this->responseData($response));
101  }
102 
109  public function execute(array $options = [], string $resultType = null)
110  {
111  try {
112  $response = $this->send($options);
113  } catch (ServerException $e) {
114  $response = $e->getResponse();
115  $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
116  throw $e;
117  } catch (ClientException $e) {
118  $response = $e->getResponse();
119  $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
120  throw $e;
121  }
122 
123  return $this->mapFromResponse($response, $resultType);
124  }
125 
132  public function executeAsync(array $options = [], string $resultType = null)
133  {
134  return $this->sendAsync($options)->then(
135  function (ResponseInterface $response) use ($resultType) {
136  return $this->mapFromResponse($response, $resultType);
137  },
138  function (RequestException $e) use ($resultType) {
139  $response = $e->getResponse();
140  if ($e instanceof ServerException) {
141  $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
142  }
143  if ($e instanceof ClientException) {
144  $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
145  }
146  throw $e;
147  }
148  );
149  }
150 
155  public function withExpand($expand): ByProjectKeyCustomObjectsPost
156  {
157  return $this->withQueryParam('expand', $expand);
158  }
159 }
__construct(string $projectKey, $body=null, array $headers=[], ClientInterface $client=null)
execute(array $options=[], string $resultType=null)
mapFromResponse(?ResponseInterface $response, string $resultType=null)
withExpand($expand)
executeAsync(array $options=[], string $resultType=null)
responseData(ResponseInterface $response)
Definition: ApiRequest.php:145
withQueryParam(string $parameterName, $value)
Definition: ApiRequest.php:77
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)