commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
Loading...
Searching...
No Matches
ByProjectKeyZonesPost.php
1<?php
2
3declare(strict_types=1);
10
22use GuzzleHttp\ClientInterface;
23use GuzzleHttp\Exception\ClientException;
24use GuzzleHttp\Exception\RequestException;
25use GuzzleHttp\Exception\ServerException;
26use GuzzleHttp\Promise\PromiseInterface;
27
28use Psr\Http\Message\ResponseInterface;
29
38{
43 public function __construct(string $projectKey, $body = null, array $headers = [], ClientInterface $client = null)
44 {
45 $uri = str_replace(['{projectKey}'], [$projectKey], '{projectKey}/zones');
46 parent::__construct($client, 'POST', $uri, $headers, is_object($body) || is_array($body) ? json_encode($body) : $body);
47 }
48
54 public function mapFromResponse(?ResponseInterface $response, string $resultType = null)
55 {
56 if (is_null($response)) {
57 return null;
58 }
59 if (is_null($resultType)) {
60 switch ($response->getStatusCode()) {
61 case '201':
62 $resultType = ZoneModel::class;
63
64 break;
65 case '400':
66 $resultType = ErrorResponseModel::class;
67
68 break;
69 case '401':
70 $resultType = ErrorResponseModel::class;
71
72 break;
73 case '403':
74 $resultType = ErrorResponseModel::class;
75
76 break;
77 case '500':
78 $resultType = ErrorResponseModel::class;
79
80 break;
81 case '502':
82 $resultType = ErrorResponseModel::class;
83
84 break;
85 case '503':
86 $resultType = ErrorResponseModel::class;
87
88 break;
89 default:
90 $resultType = JsonObjectModel::class;
91
92 break;
93 }
94 }
95
96 return $resultType::of($this->responseData($response));
97 }
98
105 public function execute(array $options = [], string $resultType = null)
106 {
107 try {
108 $response = $this->send($options);
109 } catch (ServerException $e) {
110 $response = $e->getResponse();
111 $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
112 throw $e;
113 } catch (ClientException $e) {
114 $response = $e->getResponse();
115 $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
116 throw $e;
117 }
118
119 return $this->mapFromResponse($response, $resultType);
120 }
121
128 public function executeAsync(array $options = [], string $resultType = null)
129 {
130 return $this->sendAsync($options)->then(
131 function (ResponseInterface $response) use ($resultType) {
132 return $this->mapFromResponse($response, $resultType);
133 },
134 function (RequestException $e) use ($resultType) {
135 $response = $e->getResponse();
136 if ($e instanceof ServerException) {
137 $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
138 }
139 if ($e instanceof ClientException) {
140 $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
141 }
142 throw $e;
143 }
144 );
145 }
146
151 public function withExpand($expand): ByProjectKeyZonesPost
152 {
153 return $this->withQueryParam('expand', $expand);
154 }
155}
__construct(string $projectKey, $body=null, array $headers=[], ClientInterface $client=null)
execute(array $options=[], string $resultType=null)
mapFromResponse(?ResponseInterface $response, string $resultType=null)
executeAsync(array $options=[], string $resultType=null)
withExpand($expand)
responseData(ResponseInterface $response)
withQueryParam(string $parameterName, $value)