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