commercetools-sdk-php-v2  master
The platform, import-api and ml-api PHP sdks generated from our api reference.
ByProjectKeyInventoryPost.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}/inventory');
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 '201':
62  $resultType = InventoryEntryModel::class;
63 
64  break;
65  case '400':
66  $resultType = ErrorResponseModel::class;
67 
68  break;
69  case '401':
70  $resultType = ErrorResponseModel::class;
71 
72  break;
73  case '403':
74  $resultType = ErrorResponseModel::class;
75 
76  break;
77  case '500':
78  $resultType = ErrorResponseModel::class;
79 
80  break;
81  case '502':
82  $resultType = ErrorResponseModel::class;
83 
84  break;
85  case '503':
86  $resultType = ErrorResponseModel::class;
87 
88  break;
89  default:
90  $resultType = JsonObjectModel::class;
91 
92  break;
93  }
94  }
95 
96  return $resultType::of($this->responseData($response));
97  }
98 
105  public function execute(array $options = [], string $resultType = null)
106  {
107  try {
108  $response = $this->send($options);
109  } catch (ServerException $e) {
110  $response = $e->getResponse();
111  $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
112  throw $e;
113  } catch (ClientException $e) {
114  $response = $e->getResponse();
115  $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
116  throw $e;
117  }
118 
119  return $this->mapFromResponse($response, $resultType);
120  }
121 
128  public function executeAsync(array $options = [], string $resultType = null)
129  {
130  return $this->sendAsync($options)->then(
131  function (ResponseInterface $response) use ($resultType) {
132  return $this->mapFromResponse($response, $resultType);
133  },
134  function (RequestException $e) use ($resultType) {
135  $response = $e->getResponse();
136  if ($e instanceof ServerException) {
137  $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
138  }
139  if ($e instanceof ClientException) {
140  $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
141  }
142  throw $e;
143  }
144  );
145  }
146 
151  public function withExpand($expand): ByProjectKeyInventoryPost
152  {
153  return $this->withQueryParam('expand', $expand);
154  }
155 }
execute(array $options=[], string $resultType=null)
executeAsync(array $options=[], string $resultType=null)
mapFromResponse(?ResponseInterface $response, string $resultType=null)
withExpand($expand)
__construct(string $projectKey, $body=null, array $headers=[], ClientInterface $client=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)