55use XQueue \AddressCheck \API \AddressCheckResult ;
66use XQueue \AddressCheck \API \AddressCheckException ;
77
8+ /**
9+ * An abstract API service class
10+ *
11+ * Defines all request methods to be used by the services and performs the request
12+ */
813abstract class AbstractAddressCheckService
914{
15+ /**
16+ * @var string The base URI of the API service
17+ */
1018 private $ baseUri = 'https://adc.maileon.com/svc/2.0 ' ;
1119
20+ /**
21+ * @var string The user name for authentification
22+ */
1223 private $ username ;
24+
25+ /**
26+ * @var string The password for authentification
27+ */
1328 private $ password ;
1429
30+ /**
31+ * @var string The MimeType to define the result format
32+ */
1533 private $ mimeType = 'application/json ' ;
1634
35+ /**
36+ * @var string A language tag according to RFC 5646
37+ */
38+ private $ acceptLanguage ;
39+
40+ /**
41+ * @var bool Wether or not to activate debugging
42+ */
1743 private $ debug = false ;
44+
45+ /**
46+ * @var resource CURL's STDERR verbose output
47+ */
1848 private $ verboseOut ;
1949
50+ /**
51+ * @var string The host name of the used proxy
52+ */
2053 private $ proxyHost ;
54+
55+ /**
56+ * @var int The port number of the used proxy
57+ */
2158 private $ proxyPort = 80 ;
2259
23- private $ timeout ;
60+ /**
61+ * @var int The connection's timeout limit
62+ */
63+ private $ timeout = 10 ;
2464
65+ /**
66+ * Creates a new service
67+ *
68+ * @param array $config The configuration list
69+ */
2570 public function __construct (array $ config = array ())
2671 {
2772 if ( array_key_exists ('BASE_URI ' , $ config ) ) {
@@ -57,57 +102,126 @@ public function __construct(array $config = array())
57102 }
58103 }
59104
105+ /**
106+ * Set the base URI
107+ *
108+ * @param string $baseUri Base URI to the API service
109+ */
60110 public function setBaseUri ($ baseUri )
61111 {
62112 $ this ->baseUri = $ baseUri ;
63113 }
64114
115+ /**
116+ * Set the user name
117+ *
118+ * @param string $username The username for authentification
119+ */
65120 public function setUsername ($ username )
66121 {
67122 $ this ->username = $ username ;
68123 }
69124
125+ /**
126+ * Set the password
127+ *
128+ * @param string $password The password for authentification
129+ */
70130 public function setPassword ($ password )
71131 {
72132 $ this ->password = $ password ;
73133 }
74134
135+ /**
136+ * Set the MimeType
137+ *
138+ * @param string $mimeType The MimeType to send and receive body data in
139+ */
75140 public function setMimeType ($ mimeType )
76141 {
77142 $ this ->mimeType = $ mimeType ;
78143 }
79144
145+ /**
146+ * Set a language tag
147+ *
148+ * @param string $acceptLanguage A language tag according to RFC 5646
149+ */
150+ protected function setAcceptLanguage ($ acceptLanguage )
151+ {
152+ $ this ->acceptLanguage = $ acceptLanguage ;
153+ }
154+
155+ /**
156+ * Set the connection's timeout
157+ *
158+ * @param int $timeout The connections's timeout limit
159+ */
80160 public function setTimeout ($ timeout )
81161 {
82162 $ this ->timeout = $ timeout ;
83163 }
84164
165+ /**
166+ * Set the proxy host
167+ *
168+ * @param string $proxyHost The host name of the used proxy
169+ */
85170 public function setProxyHost ($ proxyHost )
86171 {
87172 $ this ->proxyHost = $ proxyHost ;
88173 }
89174
175+ /**
176+ * Set the proxy port
177+ *
178+ * @param int $proxyPort The port of the used proxy
179+ */
90180 public function setProxyPort ($ proxyPort )
91181 {
92182 $ this ->proxyPort = $ proxyPort ;
93183 }
94184
185+ /**
186+ * Activates or deactivates debugging
187+ *
188+ * @param bool $debug Activates or deactivates debugging
189+ */
95190 public function setDebug ($ debug )
96191 {
97192 $ this ->debug = $ debug ;
98193 }
99194
195+ /**
196+ * Returns wether debugging is active
197+ *
198+ * @return bool True if debug is active or false if debug is inactive
199+ */
100200 public function isDebug ()
101201 {
102202 return $ this ->debug ;
103203 }
104204
205+ /**
206+ * Sends a GET request
207+ *
208+ * @param string $resourcePath The path of the resouce
209+ * @param array $queryParameters A list of all URL parameters
210+ * @return AddressCheckResult The result of the API call
211+ */
105212 public function get ($ resourcePath , $ queryParameters = array ())
106213 {
107214 $ curlSession = $ this ->prepareSession ($ resourcePath , $ queryParameters );
108215 return $ this ->performRequest ($ curlSession );
109216 }
110217
218+ /**
219+ * Prepares a CURL request
220+ *
221+ * @param string $resourcePath The path of the resource
222+ * @param array $queryParameters A list of all URL parameters
223+ * @return \CurlHandle|false Returns the CURL handle on success or false on error
224+ */
111225 private function prepareSession ($ resourcePath , $ queryParameters )
112226 {
113227 $ requestUrl = $ this ->constructRequestUrl ($ resourcePath , $ queryParameters );
@@ -141,6 +255,13 @@ private function prepareSession($resourcePath, $queryParameters)
141255 return $ curlSession ;
142256 }
143257
258+ /**
259+ * Constructs the complete URL to the API service
260+ *
261+ * @param string $resourcePath The path of the resource
262+ * @param array $queryParameters A list of all URL parameters
263+ * @return string The complete URL
264+ */
144265 private function constructRequestUrl ($ resourcePath , $ queryParameters )
145266 {
146267 $ requestUrl = $ this ->baseUri . "/ " . $ resourcePath ;
@@ -164,22 +285,41 @@ private function constructRequestUrl($resourcePath, $queryParameters)
164285 return $ requestUrl ;
165286 }
166287
288+ /**
289+ * Creates the headers for the request
290+ *
291+ * @throws AddressCheckException if user name and password aren't set
292+ * @return array A list of all headers
293+ */
167294 private function constructHeaders ()
168295 {
169296 if ( empty ($ this ->username ) || empty ($ this ->password ) ) {
170297 throw new AddressCheckException ("Authorization not set " );
171298 }
172299
173- $ headers = array (
174- "Content-type: " . $ this ->mimeType ,
175- "Accept: " . $ this ->mimeType ,
176- "Authorization: Basic " . base64_encode ($ this ->username .": " .$ this ->password ),
177- "Expect: "
178- );
300+ $ headers = [
301+ "Authorization: Basic " . base64_encode ($ this ->username .": " .$ this ->password )
302+ ];
303+
304+ if ( !empty ($ this ->mimeType ) ) {
305+ $ headers [] = "Content-type: " . $ this ->mimeType ;
306+ $ headers [] = "Accept: " . $ this ->mimeType ;
307+ }
308+
309+ if ( !empty ($ this ->acceptLanguage ) ) {
310+ $ headers [] = "Accept-Language: " . $ this ->acceptLanguage ;
311+ }
179312
180313 return $ headers ;
181314 }
182315
316+ /**
317+ * Executes the CURL request
318+ *
319+ * @param \CurlHandle $curlSession The CURL handle to be executed
320+ * @throws AddressCheckException if an error occured
321+ * @return AddressCheckResult The request's result as an object
322+ */
183323 private function performRequest ($ curlSession )
184324 {
185325 $ response = curl_exec ($ curlSession );
@@ -207,6 +347,13 @@ private function performRequest($curlSession)
207347 }
208348 }
209349
350+ /**
351+ * Prints debug information for the running CURL request
352+ *
353+ * @param \CurlHandle $curlSession The CURL request
354+ * @param AddressCheckResult $result The result of the CURL request
355+ * @param AddressCheckException $exception A thrown exception
356+ */
210357 private function printDebugInformation ($ curlSession , $ result = null , $ exception = null )
211358 {
212359 rewind ($ this ->verboseOut );
0 commit comments