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