commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
ByProjectKeyCartsGet.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 
41 {
46  public function __construct(string $projectKey, $body = null, array $headers = [], ClientInterface $client = null)
47  {
48  $uri = str_replace(['{projectKey}'], [$projectKey], '{projectKey}/carts');
49  parent::__construct($client, 'GET', $uri, $headers, is_object($body) || is_array($body) ? json_encode($body) : $body);
50  }
51 
57  public function mapFromResponse(?ResponseInterface $response, string $resultType = null)
58  {
59  if (is_null($response)) {
60  return null;
61  }
62  if (is_null($resultType)) {
63  switch ($response->getStatusCode()) {
64  case '200':
65  $resultType = CartPagedQueryResponseModel::class;
66 
67  break;
68  case '400':
69  $resultType = ErrorResponseModel::class;
70 
71  break;
72  case '401':
73  $resultType = ErrorResponseModel::class;
74 
75  break;
76  case '403':
77  $resultType = ErrorResponseModel::class;
78 
79  break;
80  case '500':
81  $resultType = ErrorResponseModel::class;
82 
83  break;
84  case '502':
85  $resultType = ErrorResponseModel::class;
86 
87  break;
88  case '503':
89  $resultType = ErrorResponseModel::class;
90 
91  break;
92  default:
93  $resultType = JsonObjectModel::class;
94 
95  break;
96  }
97  }
98 
99  return $resultType::of($this->responseData($response));
100  }
101 
108  public function execute(array $options = [], string $resultType = null)
109  {
110  try {
111  $response = $this->send($options);
112  } catch (ServerException $e) {
113  $response = $e->getResponse();
114  $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
115  throw $e;
116  } catch (ClientException $e) {
117  $response = $e->getResponse();
118  $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
119  throw $e;
120  }
121 
122  return $this->mapFromResponse($response, $resultType);
123  }
124 
131  public function executeAsync(array $options = [], string $resultType = null)
132  {
133  return $this->sendAsync($options)->then(
134  function (ResponseInterface $response) use ($resultType) {
135  return $this->mapFromResponse($response, $resultType);
136  },
137  function (RequestException $e) use ($resultType) {
138  $response = $e->getResponse();
139  if ($e instanceof ServerException) {
140  $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
141  }
142  if ($e instanceof ClientException) {
143  $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
144  }
145  throw $e;
146  }
147  );
148  }
149 
154  public function withExpand($expand): ByProjectKeyCartsGet
155  {
156  return $this->withQueryParam('expand', $expand);
157  }
158 
163  public function withSort($sort): ByProjectKeyCartsGet
164  {
165  return $this->withQueryParam('sort', $sort);
166  }
167 
172  public function withLimit($limit): ByProjectKeyCartsGet
173  {
174  return $this->withQueryParam('limit', $limit);
175  }
176 
181  public function withOffset($offset): ByProjectKeyCartsGet
182  {
183  return $this->withQueryParam('offset', $offset);
184  }
185 
190  public function withWithTotal($withTotal): ByProjectKeyCartsGet
191  {
192  return $this->withQueryParam('withTotal', $withTotal);
193  }
194 
199  public function withWhere($where): ByProjectKeyCartsGet
200  {
201  return $this->withQueryParam('where', $where);
202  }
203 
208  public function withPredicateVar(string $varName, $predicateVar): ByProjectKeyCartsGet
209  {
210  return $this->withQueryParam(sprintf('var.%s', $varName), $predicateVar);
211  }
212 }
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
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)