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