commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
ByProjectKeyHead.php
1 <?php
2 
3 declare(strict_types=1);
10 
18 use GuzzleHttp\ClientInterface;
19 use GuzzleHttp\Exception\ClientException;
20 use GuzzleHttp\Exception\RequestException;
21 use GuzzleHttp\Exception\ServerException;
22 use GuzzleHttp\Promise\PromiseInterface;
23 
24 use Psr\Http\Message\ResponseInterface;
25 
32 {
37  public function __construct(string $projectKey, $body = null, array $headers = [], ClientInterface $client = null)
38  {
39  $uri = str_replace(['{projectKey}'], [$projectKey], '{projectKey}');
40  parent::__construct($client, 'HEAD', $uri, $headers, is_object($body) || is_array($body) ? json_encode($body) : $body);
41  }
42 
48  public function mapFromResponse(?ResponseInterface $response, string $resultType = null)
49  {
50  if (is_null($response)) {
51  return null;
52  }
53  if (is_null($resultType)) {
54  switch ($response->getStatusCode()) {
55  default:
56  $resultType = JsonObjectModel::class;
57 
58  break;
59  }
60  }
61 
62  return $resultType::of($this->responseData($response));
63  }
64 
71  public function execute(array $options = [], string $resultType = null)
72  {
73  try {
74  $response = $this->send($options);
75  } catch (ServerException $e) {
76  $response = $e->getResponse();
77  $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
78  throw $e;
79  } catch (ClientException $e) {
80  $response = $e->getResponse();
81  $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
82  throw $e;
83  }
84 
85  return $this->mapFromResponse($response, $resultType);
86  }
87 
94  public function executeAsync(array $options = [], string $resultType = null)
95  {
96  return $this->sendAsync($options)->then(
97  function (ResponseInterface $response) use ($resultType) {
98  return $this->mapFromResponse($response, $resultType);
99  },
100  function (RequestException $e) use ($resultType) {
101  $response = $e->getResponse();
102  if ($e instanceof ServerException) {
103  $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
104  }
105  if ($e instanceof ClientException) {
106  $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
107  }
108  throw $e;
109  }
110  );
111  }
112 }
__construct(string $projectKey, $body=null, array $headers=[], ClientInterface $client=null)
execute(array $options=[], string $resultType=null)
executeAsync(array $options=[], string $resultType=null)
mapFromResponse(?ResponseInterface $response, 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)