commercetools-sdk-php-v2  master
The platform, import-api and ml-api PHP sdks generated from our api reference.
ByProjectKeyImportContainersGet.php
1 <?php
2 
3 declare(strict_types=1);
10 
20 use GuzzleHttp\ClientInterface;
21 use GuzzleHttp\Exception\ClientException;
22 use GuzzleHttp\Exception\RequestException;
23 use GuzzleHttp\Exception\ServerException;
24 use GuzzleHttp\Promise\PromiseInterface;
25 
26 use Psr\Http\Message\ResponseInterface;
27 
34 {
39  public function __construct(string $projectKey, $body = null, array $headers = [], ClientInterface $client = null)
40  {
41  $uri = str_replace(['{projectKey}'], [$projectKey], '{projectKey}/import-containers');
42  parent::__construct($client, 'GET', $uri, $headers, is_object($body) || is_array($body) ? json_encode($body) : $body);
43  }
44 
50  public function mapFromResponse(?ResponseInterface $response, string $resultType = null)
51  {
52  if (is_null($response)) {
53  return null;
54  }
55  if (is_null($resultType)) {
56  switch ($response->getStatusCode()) {
57  case '200':
58  $resultType = ImportContainerPagedResponseModel::class;
59 
60  break;
61  default:
62  $resultType = JsonObjectModel::class;
63 
64  break;
65  }
66  }
67 
68  return $resultType::of($this->responseData($response));
69  }
70 
77  public function execute(array $options = [], string $resultType = null)
78  {
79  try {
80  $response = $this->send($options);
81  } catch (ServerException $e) {
82  $response = $e->getResponse();
83  $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
84  throw $e;
85  } catch (ClientException $e) {
86  $response = $e->getResponse();
87  $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
88  throw $e;
89  }
90 
91  return $this->mapFromResponse($response, $resultType);
92  }
93 
100  public function executeAsync(array $options = [], string $resultType = null)
101  {
102  return $this->sendAsync($options)->then(
103  function (ResponseInterface $response) use ($resultType) {
104  return $this->mapFromResponse($response, $resultType);
105  },
106  function (RequestException $e) use ($resultType) {
107  $response = $e->getResponse();
108  if ($e instanceof ServerException) {
109  $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
110  }
111  if ($e instanceof ClientException) {
112  $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
113  }
114  throw $e;
115  }
116  );
117  }
118 
124  {
125  return $this->withQueryParam('limit', $limit);
126  }
127 
132  public function withOffset($offset): ByProjectKeyImportContainersGet
133  {
134  return $this->withQueryParam('offset', $offset);
135  }
136 
142  {
143  return $this->withQueryParam('sort', $sort);
144  }
145 }
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)
__construct(string $projectKey, $body=null, array $headers=[], ClientInterface $client=null)
mapFromResponse(?ResponseInterface $response, string $resultType=null)