commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
Loading...
Searching...
No Matches
ByProjectKeyVariantsBulkPost.php
1<?php
2
3declare(strict_types=1);
10
20use GuzzleHttp\ClientInterface;
21use GuzzleHttp\Exception\ClientException;
22use GuzzleHttp\Exception\RequestException;
23use GuzzleHttp\Exception\ServerException;
24use GuzzleHttp\Promise\PromiseInterface;
25
26use Psr\Http\Message\ResponseInterface;
27
34{
39 public function __construct(string $projectKey, $body = null, array $headers = [], ?ClientInterface $client = null)
40 {
41 $uri = str_replace(['{projectKey}'], [self::encodePathParam($projectKey)], '{projectKey}/variants/bulk');
42 parent::__construct($client, 'POST', $uri, $headers, is_object($body) || is_array($body) ? json_encode($body) : $body);
43 }
44
50 public function mapFromResponse(?ResponseInterface $response, ?string $resultType = null)
51 {
52 if (is_null($response)) {
53 return null;
54 }
55 if (is_null($resultType)) {
56 switch ($response->getStatusCode()) {
57 case '207':
58 $resultType = VariantBulkUpdateResponseModel::class;
59
60 break;
61 default:
62 $resultType = JsonObjectModel::class;
63
64 break;
65 }
66 }
67
68 return $resultType::of($this->responseData($response));
69 }
70
77 public function execute(array $options = [], ?string $resultType = null)
78 {
79 try {
80 $response = $this->send($options);
81 } catch (ServerException $e) {
82 $response = $e->getResponse();
83 $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
84 throw $e;
85 } catch (ClientException $e) {
86 $response = $e->getResponse();
87 $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
88 throw $e;
89 }
90
91 return $this->mapFromResponse($response, $resultType);
92 }
93
100 public function executeAsync(array $options = [], ?string $resultType = null)
101 {
102 return $this->sendAsync($options)->then(
103 function (ResponseInterface $response) use ($resultType) {
104 return $this->mapFromResponse($response, $resultType);
105 },
106 function (RequestException $e) use ($resultType) {
107 $response = $e->getResponse();
108 if ($e instanceof ServerException) {
109 $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
110 }
111 if ($e instanceof ClientException) {
112 $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
113 }
114 throw $e;
115 }
116 );
117 }
118
123 public function withVersionControl($versionControl): ByProjectKeyVariantsBulkPost
124 {
125 return $this->withQueryParam('versionControl', $versionControl);
126 }
127}
execute(array $options=[], ?string $resultType=null)
mapFromResponse(?ResponseInterface $response, ?string $resultType=null)
withVersionControl($versionControl)
__construct(string $projectKey, $body=null, array $headers=[], ?ClientInterface $client=null)
executeAsync(array $options=[], ?string $resultType=null)
responseData(ResponseInterface $response)
withQueryParam(string $parameterName, $value)