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