99
1010class Connection
1111{
12- private $ apiUrl = 'https://panel.sendcloud.sc/api/v2/ ' ;
13- private $ apiKey ;
14- private $ apiSecret ;
15- private $ partnerId ;
12+ private string $ apiUrl = 'https://panel.sendcloud.sc/api/v2/ ' ;
13+ private string $ apiKey ;
14+ private string $ apiSecret ;
15+ private ?string $ partnerId = null ;
16+ private ?int $ maxResponseSizeInBytes = null ;
1617
17- /**
18- * Contains the HTTP client (Guzzle)
19- * @var Client
20- */
21- private $ client ;
22-
23- /**
24- * Array of inserted middleWares
25- * @var array
26- */
27- protected $ middleWares = [];
18+ private ?Client $ client = null ;
19+ protected array $ middleWares = [];
2820
2921 public function __construct (string $ apiKey , string $ apiSecret , ?string $ partnerId = null )
3022 {
@@ -35,7 +27,7 @@ public function __construct(string $apiKey, string $apiSecret, ?string $partnerI
3527
3628 public function client (): Client
3729 {
38- if ($ this ->client ) {
30+ if ($ this ->client instanceof Client ) {
3931 return $ this ->client ;
4032 }
4133
@@ -63,7 +55,7 @@ public function client(): Client
6355 return $ this ->client ;
6456 }
6557
66- public function insertMiddleWare ($ middleWare )
58+ public function insertMiddleWare ($ middleWare ): void
6759 {
6860 $ this ->middleWares [] = $ middleWare ;
6961 }
@@ -73,13 +65,6 @@ public function apiUrl(): string
7365 return $ this ->apiUrl ;
7466 }
7567
76- /**
77- * Perform a GET request
78- * @param string $url
79- * @param array $params
80- * @return array
81- * @throws SendCloudApiException
82- */
8368 public function get ($ url , $ params = []): array
8469 {
8570 try {
@@ -94,14 +79,6 @@ public function get($url, $params = []): array
9479 }
9580 }
9681
97- /**
98- * Perform a POST request
99- * @param string $url
100- * @param mixed $body
101- * @param array $query
102- * @return array
103- * @throws SendCloudApiException
104- */
10582 public function post ($ url , $ body , $ query = []): array
10683 {
10784 try {
@@ -116,14 +93,6 @@ public function post($url, $body, $query = []): array
11693 }
11794 }
11895
119- /**
120- * Perform PUT request
121- * @param string $url
122- * @param mixed $body
123- * @param array $query
124- * @return array
125- * @throws SendCloudApiException
126- */
12796 public function put ($ url , $ body , $ query = []): array
12897 {
12998 try {
@@ -138,14 +107,7 @@ public function put($url, $body, $query = []): array
138107 }
139108 }
140109
141- /**
142- * Perform DELETE request
143- * @param string $url
144- * @param array $query
145- * @return array
146- * @throws SendCloudApiException
147- */
148- public function delete ($ url , $ query = [])
110+ public function delete ($ url , $ query = []): array
149111 {
150112 try {
151113 $ result = $ this ->client ()->delete ($ url , ['query ' => $ query ]);
@@ -159,18 +121,20 @@ public function delete($url, $query = [])
159121 }
160122 }
161123
162- /**
163- * @param ResponseInterface $response
164- * @return array Parsed JSON result
165- * @throws SendCloudApiException
166- */
167- public function parseResponse (ResponseInterface $ response )
124+ public function parseResponse (ResponseInterface $ response ): array
168125 {
169126 try {
170127 // Rewind the response (middlewares might have read it already)
171128 $ response ->getBody ()->rewind ();
172129
173130 $ responseBody = $ response ->getBody ()->getContents ();
131+
132+ if (! is_null ($ this ->maxResponseSizeInBytes )) {
133+ if (strlen ($ responseBody ) > $ this ->maxResponseSizeInBytes ) {
134+ throw new MaximumResponseSizeException (sprintf ('Response size exceeded maximum of %d bytes ' , $ this ->maxResponseSizeInBytes ));
135+ }
136+ }
137+
174138 $ resultArray = json_decode ($ responseBody , true );
175139
176140 if (! is_array ($ resultArray )) {
@@ -196,39 +160,34 @@ public function parseResponse(ResponseInterface $response)
196160 }
197161
198162 /**
199- * Returns the selected environment
200- *
201- * @return string
202163 * @deprecated
203164 */
204- public function getEnvironment ()
165+ public function getEnvironment (): string
205166 {
206167 return 'live ' ;
207168 }
208169
209170 /**
210- * Set the environment for the client
211- *
212- * @param string $environment
213- * @throws SendCloudApiException
214171 * @deprecated
215172 */
216- public function setEnvironment ($ environment )
173+ public function setEnvironment ($ environment ): void
217174 {
218175 if ($ environment === 'test ' ) {
219176 throw new SendCloudApiException ('SendCloud test environment is no longer available ' );
220177 }
221178 }
222179
223- /**
224- * Download a resource.
225- *
226- * @param string $url
227- * @param array $headers
228- * @return string
229- * @throws SendCloudApiException
230- */
231- public function download ($ url , array $ headers = ['Accept ' => 'application/pdf ' ])
180+ public function setMaxResponseSizeInBytes (?int $ maxResponseSizeInBytes ): void
181+ {
182+ $ this ->maxResponseSizeInBytes = $ maxResponseSizeInBytes ;
183+ }
184+
185+ public function getMaxResponseSizeInBytes (): ?int
186+ {
187+ return $ this ->maxResponseSizeInBytes ;
188+ }
189+
190+ public function download ($ url , array $ headers = ['Accept ' => 'application/pdf ' ]): string
232191 {
233192 try {
234193 $ result = $ this ->client ()->get ($ url , ['headers ' => $ headers ]);
0 commit comments