commercetools-sdk-php-v2  master
The platform, import-api and ml-api PHP sdks generated from our api reference.
ByProjectKeyCategoriesImportContainersByImportContainerKeyPost.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 
36 {
41  public function __construct(string $projectKey, string $importContainerKey, $body = null, array $headers = [], ClientInterface $client = null)
42  {
43  $uri = str_replace(['{projectKey}', '{importContainerKey}'], [$projectKey, $importContainerKey], '{projectKey}/categories/import-containers/{importContainerKey}');
44  parent::__construct($client, 'POST', $uri, $headers, is_object($body) || is_array($body) ? json_encode($body) : $body);
45  }
46 
52  public function mapFromResponse(?ResponseInterface $response, string $resultType = null)
53  {
54  if (is_null($response)) {
55  return null;
56  }
57  if (is_null($resultType)) {
58  switch ($response->getStatusCode()) {
59  case '201':
60  $resultType = ImportResponseModel::class;
61 
62  break;
63  case '400':
64  $resultType = ErrorResponseModel::class;
65 
66  break;
67  default:
68  $resultType = JsonObjectModel::class;
69 
70  break;
71  }
72  }
73 
74  return $resultType::of($this->responseData($response));
75  }
76 
83  public function execute(array $options = [], string $resultType = null)
84  {
85  try {
86  $response = $this->send($options);
87  } catch (ServerException $e) {
88  $response = $e->getResponse();
89  $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
90  throw $e;
91  } catch (ClientException $e) {
92  $response = $e->getResponse();
93  $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
94  throw $e;
95  }
96 
97  return $this->mapFromResponse($response, $resultType);
98  }
99 
106  public function executeAsync(array $options = [], string $resultType = null)
107  {
108  return $this->sendAsync($options)->then(
109  function (ResponseInterface $response) use ($resultType) {
110  return $this->mapFromResponse($response, $resultType);
111  },
112  function (RequestException $e) use ($resultType) {
113  $response = $e->getResponse();
114  if ($e instanceof ServerException) {
115  $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
116  }
117  if ($e instanceof ClientException) {
118  $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
119  }
120  throw $e;
121  }
122  );
123  }
124 }
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)
__construct(string $projectKey, string $importContainerKey, $body=null, array $headers=[], ClientInterface $client=null)