commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
Loading...
Searching...
No Matches
ByProjectKeyCustomObjectsPost.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}/custom-objects');
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 '200':
62 $resultType = CustomObjectModel::class;
63
64 break;
65 case '201':
66 $resultType = CustomObjectModel::class;
67
68 break;
69 case '400':
70 $resultType = ErrorResponseModel::class;
71
72 break;
73 case '401':
74 $resultType = ErrorResponseModel::class;
75
76 break;
77 case '403':
78 $resultType = ErrorResponseModel::class;
79
80 break;
81 case '500':
82 $resultType = ErrorResponseModel::class;
83
84 break;
85 case '502':
86 $resultType = ErrorResponseModel::class;
87
88 break;
89 case '503':
90 $resultType = ErrorResponseModel::class;
91
92 break;
93 default:
94 $resultType = JsonObjectModel::class;
95
96 break;
97 }
98 }
99
100 return $resultType::of($this->responseData($response));
101 }
102
109 public function execute(array $options = [], string $resultType = null)
110 {
111 try {
112 $response = $this->send($options);
113 } catch (ServerException $e) {
114 $response = $e->getResponse();
115 $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
116 throw $e;
117 } catch (ClientException $e) {
118 $response = $e->getResponse();
119 $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
120 throw $e;
121 }
122
123 return $this->mapFromResponse($response, $resultType);
124 }
125
132 public function executeAsync(array $options = [], string $resultType = null)
133 {
134 return $this->sendAsync($options)->then(
135 function (ResponseInterface $response) use ($resultType) {
136 return $this->mapFromResponse($response, $resultType);
137 },
138 function (RequestException $e) use ($resultType) {
139 $response = $e->getResponse();
140 if ($e instanceof ServerException) {
141 $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
142 }
143 if ($e instanceof ClientException) {
144 $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
145 }
146 throw $e;
147 }
148 );
149 }
150
156 {
157 return $this->withQueryParam('expand', $expand);
158 }
159}
__construct(string $projectKey, $body=null, array $headers=[], ClientInterface $client=null)
execute(array $options=[], string $resultType=null)
mapFromResponse(?ResponseInterface $response, string $resultType=null)
withExpand($expand)
executeAsync(array $options=[], string $resultType=null)
responseData(ResponseInterface $response)
withQueryParam(string $parameterName, $value)