commercetools-sdk-php-v2  master
The platform, import-api and ml-api PHP sdks generated from our api reference.
ByProjectKeyImportOperationsByIdGet.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 $id, $body = null, array $headers = [], ClientInterface $client = null)
42  {
43  $uri = str_replace(['{projectKey}', '{id}'], [$projectKey, $id], '{projectKey}/import-operations/{id}');
44  parent::__construct($client, 'GET', $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 '200':
60  $resultType = ImportOperationModel::class;
61 
62  break;
63  case '404':
64  $resultType = ErrorResponseModel::class;
65 
66  break;
67  case '503':
68  $resultType = ErrorResponseModel::class;
69 
70  break;
71  default:
72  $resultType = JsonObjectModel::class;
73 
74  break;
75  }
76  }
77 
78  return $resultType::of($this->responseData($response));
79  }
80 
87  public function execute(array $options = [], string $resultType = null)
88  {
89  try {
90  $response = $this->send($options);
91  } catch (ServerException $e) {
92  $response = $e->getResponse();
93  $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
94  throw $e;
95  } catch (ClientException $e) {
96  $response = $e->getResponse();
97  $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
98  throw $e;
99  }
100 
101  return $this->mapFromResponse($response, $resultType);
102  }
103 
110  public function executeAsync(array $options = [], string $resultType = null)
111  {
112  return $this->sendAsync($options)->then(
113  function (ResponseInterface $response) use ($resultType) {
114  return $this->mapFromResponse($response, $resultType);
115  },
116  function (RequestException $e) use ($resultType) {
117  $response = $e->getResponse();
118  if ($e instanceof ServerException) {
119  $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
120  }
121  if ($e instanceof ClientException) {
122  $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
123  }
124  throw $e;
125  }
126  );
127  }
128 }
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 $id, $body=null, array $headers=[], ClientInterface $client=null)