1: <?php
2: namespace Worldline\Connect\Sdk;
3:
4: use UnexpectedValueException;
5: use Worldline\Connect\Sdk\Domain\ShoppingCartExtension;
6:
7: /**
8: * Class CommunicatorConfiguration
9: *
10: * @package Worldline\Connect\Sdk
11: */
12: class CommunicatorConfiguration
13: {
14: /**
15: * @var string
16: */
17: private string $authorizationId;
18:
19: /**
20: * @var string
21: */
22: private string $authorizationSecret;
23:
24: /**
25: * @var string
26: */
27: private string $apiEndpoint;
28:
29: /**
30: * @var int
31: */
32: private int $connectTimeout;
33:
34: /**
35: * @var int
36: */
37: private int $readTimeout;
38:
39: /**
40: * @var ProxyConfiguration|null
41: */
42: private ?ProxyConfiguration $proxyConfiguration;
43:
44: /**
45: * @var string
46: */
47: private string $integrator;
48:
49: /**
50: * @var ShoppingCartExtension|null
51: */
52: private ?ShoppingCartExtension $shoppingCartExtension = null;
53:
54: /**
55: * @param string $authorizationId
56: * @param string $authorizationSecret
57: * @param string $apiEndpoint
58: * @param string $integrator
59: * @param ProxyConfiguration|null $proxyConfiguration
60: * @param int $connectTimeout
61: * @param int $readTimeout
62: */
63: public function __construct(
64: string $authorizationId,
65: string $authorizationSecret,
66: string $apiEndpoint,
67: string $integrator,
68: ?ProxyConfiguration $proxyConfiguration = null,
69: int $connectTimeout = -1,
70: int $readTimeout = -1
71: ) {
72: $this->validateApiEndpoint($apiEndpoint);
73: $this->validateIntegrator($integrator);
74: $this->authorizationId = $authorizationId;
75: $this->authorizationSecret = $authorizationSecret;
76: $this->apiEndpoint = $apiEndpoint;
77: $this->integrator = $integrator;
78: $this->proxyConfiguration = $proxyConfiguration;
79: $this->connectTimeout = $connectTimeout;
80: $this->readTimeout = $readTimeout;
81: }
82:
83: private function validateApiEndpoint(string $apiEndpoint): void
84: {
85: $url = parse_url($apiEndpoint);
86: if ($url === false) {
87: throw new UnexpectedValueException('apiEndpoint is not a valid URL');
88: } elseif (isset($url['path']) && $url['path'] !== '') {
89: throw new UnexpectedValueException('apiEndpoint should not contain a path');
90: } elseif (isset($url['user']) || isset($url['query']) || isset($url['fragment'])) {
91: throw new UnexpectedValueException('apiEndpoint should not contain user info, query or fragment');
92: }
93: }
94:
95: private function validateIntegrator(string $integrator): void
96: {
97: if (strlen(trim($integrator)) == 0) {
98: throw new UnexpectedValueException("integrator is required");
99: }
100: }
101:
102: /**
103: * @return string An id used for authorization. This can be the identifier for a secret API key, or something else.
104: */
105: public function getAuthorizationId(): string
106: {
107: return $this->authorizationId;
108: }
109:
110: /**
111: * @param string $authorizationId
112: *
113: * @return void
114: */
115: public function setAuthorizationId(string $authorizationId): void
116: {
117: $this->authorizationId = $authorizationId;
118: }
119:
120: /**
121: * @return string A secret used for authorization. This can be a secret API key, or something else.
122: */
123: public function getAuthorizationSecret(): string
124: {
125: return $this->authorizationSecret;
126: }
127:
128: /**
129: * @param string $authorizationSecret
130: *
131: * @return void
132: */
133: public function setAuthorizationSecret(string $authorizationSecret): void
134: {
135: $this->authorizationSecret = $authorizationSecret;
136: }
137:
138: /**
139: * This method is an alias for getAuthorizationId.
140: *
141: * @return string
142: */
143: public function getApiKeyId(): string
144: {
145: return $this->getAuthorizationId();
146: }
147:
148: /**
149: * This method is an alias for setAuthorizationId.
150: *
151: * @param string $apiKeyId
152: *
153: * @return void
154: */
155: public function setApiKeyId(string $apiKeyId): void
156: {
157: $this->setAuthorizationId($apiKeyId);
158: }
159:
160: /**
161: * This method is an alias for getAuthorizationSecret.
162: *
163: * @return string
164: */
165: public function getApiSecret(): string
166: {
167: return $this->getAuthorizationSecret();
168: }
169:
170: /**
171: * This method is an alias for setAuthorizationSecret.
172: *
173: * @param string $apiSecret
174: *
175: * @return void
176: */
177: public function setApiSecret(string $apiSecret): void
178: {
179: $this->setAuthorizationSecret($apiSecret);
180: }
181:
182: /**
183: * @return string
184: */
185: public function getApiEndpoint(): string
186: {
187: return $this->apiEndpoint;
188: }
189:
190: /**
191: * @param string $apiEndpoint
192: *
193: * @return void
194: */
195: public function setApiEndpoint(string $apiEndpoint): void
196: {
197: $this->validateApiEndpoint($apiEndpoint);
198: $this->apiEndpoint = $apiEndpoint;
199: }
200:
201: /**
202: * @return ProxyConfiguration|null
203: */
204: public function getProxyConfiguration(): ?ProxyConfiguration
205: {
206: return $this->proxyConfiguration;
207: }
208:
209: /**
210: * @param ProxyConfiguration|null $proxyConfiguration
211: *
212: * @return void
213: */
214: public function setProxyConfiguration(?ProxyConfiguration $proxyConfiguration = null): void
215: {
216: $this->proxyConfiguration = $proxyConfiguration;
217: }
218:
219: /**
220: * @return int
221: */
222: public function getConnectTimeout(): int
223: {
224: return $this->connectTimeout;
225: }
226:
227: /**
228: * @param int $connectTimeout
229: *
230: * @return void
231: */
232: public function setConnectTimeout(int $connectTimeout): void
233: {
234: $this->connectTimeout = $connectTimeout;
235: }
236:
237: /**
238: * @return int
239: */
240: public function getReadTimeout(): int
241: {
242: return $this->readTimeout;
243: }
244:
245: /**
246: * @param int $readTimeout
247: *
248: * @return void
249: */
250: public function setReadTimeout(int $readTimeout): void
251: {
252: $this->readTimeout = $readTimeout;
253: }
254:
255: /**
256: * @return string
257: */
258: public function getIntegrator(): string
259: {
260: return $this->integrator;
261: }
262:
263: /**
264: * @param string $integrator
265: *
266: * @return void
267: */
268: public function setIntegrator(string $integrator): void
269: {
270: $this->validateIntegrator($integrator);
271: $this->integrator = $integrator;
272: }
273:
274: /**
275: * @return ShoppingCartExtension|null
276: */
277: public function getShoppingCartExtension(): ?ShoppingCartExtension
278: {
279: return $this->shoppingCartExtension;
280: }
281:
282: /**
283: * @param ShoppingCartExtension|null $shoppingCartExtension
284: *
285: * @return void
286: */
287: public function setShoppingCartExtension(?ShoppingCartExtension $shoppingCartExtension = null): void
288: {
289: $this->shoppingCartExtension = $shoppingCartExtension;
290: }
291: }
292: