commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
Loading...
Searching...
No Matches
ByProjectKeySubscriptionsPost.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
37{
42 public function __construct(string $projectKey, $body = null, array $headers = [], ClientInterface $client = null)
43 {
44 $uri = str_replace(['{projectKey}'], [$projectKey], '{projectKey}/subscriptions');
45 parent::__construct($client, 'POST', $uri, $headers, is_object($body) || is_array($body) ? json_encode($body) : $body);
46 }
47
53 public function mapFromResponse(?ResponseInterface $response, string $resultType = null)
54 {
55 if (is_null($response)) {
56 return null;
57 }
58 if (is_null($resultType)) {
59 switch ($response->getStatusCode()) {
60 case '201':
61 $resultType = SubscriptionModel::class;
62
63 break;
64 case '400':
65 $resultType = ErrorResponseModel::class;
66
67 break;
68 case '401':
69 $resultType = ErrorResponseModel::class;
70
71 break;
72 case '403':
73 $resultType = ErrorResponseModel::class;
74
75 break;
76 case '500':
77 $resultType = ErrorResponseModel::class;
78
79 break;
80 case '502':
81 $resultType = ErrorResponseModel::class;
82
83 break;
84 case '503':
85 $resultType = ErrorResponseModel::class;
86
87 break;
88 default:
89 $resultType = JsonObjectModel::class;
90
91 break;
92 }
93 }
94
95 return $resultType::of($this->responseData($response));
96 }
97
104 public function execute(array $options = [], string $resultType = null)
105 {
106 try {
107 $response = $this->send($options);
108 } catch (ServerException $e) {
109 $response = $e->getResponse();
110 $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
111 throw $e;
112 } catch (ClientException $e) {
113 $response = $e->getResponse();
114 $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
115 throw $e;
116 }
117
118 return $this->mapFromResponse($response, $resultType);
119 }
120
127 public function executeAsync(array $options = [], string $resultType = null)
128 {
129 return $this->sendAsync($options)->then(
130 function (ResponseInterface $response) use ($resultType) {
131 return $this->mapFromResponse($response, $resultType);
132 },
133 function (RequestException $e) use ($resultType) {
134 $response = $e->getResponse();
135 if ($e instanceof ServerException) {
136 $e = ExceptionFactory::createServerException($e, $this, $response, $this->mapFromResponse($response, $resultType));
137 }
138 if ($e instanceof ClientException) {
139 $e = ExceptionFactory::createClientException($e, $this, $response, $this->mapFromResponse($response, $resultType));
140 }
141 throw $e;
142 }
143 );
144 }
145}
executeAsync(array $options=[], string $resultType=null)
__construct(string $projectKey, $body=null, array $headers=[], ClientInterface $client=null)
execute(array $options=[], string $resultType=null)
mapFromResponse(?ResponseInterface $response, string $resultType=null)
responseData(ResponseInterface $response)