summaryrefslogtreecommitdiffstats
path: root/vendor/maxmind/web-service-common/src/WebService/Client.php
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--vendor/maxmind/web-service-common/src/WebService/Client.php180
1 files changed, 119 insertions, 61 deletions
diff --git a/vendor/maxmind/web-service-common/src/WebService/Client.php b/vendor/maxmind/web-service-common/src/WebService/Client.php
index 98e6e3b..1c02a6c 100644
--- a/vendor/maxmind/web-service-common/src/WebService/Client.php
+++ b/vendor/maxmind/web-service-common/src/WebService/Client.php
@@ -1,5 +1,7 @@
<?php
+declare(strict_types=1);
+
namespace MaxMind\WebService;
use Composer\CaBundle\CaBundle;
@@ -22,16 +24,56 @@ use MaxMind\WebService\Http\RequestFactory;
*/
class Client
{
- const VERSION = '0.2.0';
+ public const VERSION = '0.2.0';
+ /**
+ * @var string|null
+ */
private $caBundle;
+
+ /**
+ * @var float|null
+ */
private $connectTimeout;
+
+ /**
+ * @var string
+ */
private $host = 'api.maxmind.com';
+
+ /**
+ * @var bool
+ */
+ private $useHttps = true;
+
+ /**
+ * @var RequestFactory
+ */
private $httpRequestFactory;
+
+ /**
+ * @var string
+ */
private $licenseKey;
+
+ /**
+ * @var string|null
+ */
private $proxy;
+
+ /**
+ * @var float|null
+ */
private $timeout;
+
+ /**
+ * @var string
+ */
private $userAgentPrefix;
+
+ /**
+ * @var int
+ */
private $accountId;
/**
@@ -39,6 +81,7 @@ class Client
* @param string $licenseKey your MaxMind license key
* @param array $options an array of options. Possible keys:
* * `host` - The host to use when connecting to the web service.
+ * * `useHttps` - A boolean flag for sending the request via https.(True by default)
* * `userAgent` - The prefix of the User-Agent to use in the request.
* * `caBundle` - The bundle of CA root certificates to use in the request.
* * `connectTimeout` - The connect timeout to use for the request.
@@ -47,9 +90,9 @@ class Client
* username, and password, e.g., `http://username:password@127.0.0.1:10`.
*/
public function __construct(
- $accountId,
- $licenseKey,
- $options = []
+ int $accountId,
+ string $licenseKey,
+ array $options = []
) {
$this->accountId = $accountId;
$this->licenseKey = $licenseKey;
@@ -61,6 +104,9 @@ class Client
if (isset($options['host'])) {
$this->host = $options['host'];
}
+ if (isset($options['useHttps'])) {
+ $this->useHttps = $options['useHttps'];
+ }
if (isset($options['userAgent'])) {
$this->userAgentPrefix = $options['userAgent'] . ' ';
}
@@ -96,9 +142,9 @@ class Client
* @throws WebServiceException when some other error occurs. This also
* serves as the base class for the above exceptions.
*
- * @return array The decoded content of a successful response
+ * @return array|null The decoded content of a successful response
*/
- public function post($service, $path, $input)
+ public function post(string $service, string $path, array $input): ?array
{
$requestBody = json_encode($input);
if ($requestBody === false) {
@@ -113,7 +159,7 @@ class Client
['Content-Type: application/json']
);
- list($statusCode, $contentType, $responseBody) = $request->post($requestBody);
+ [$statusCode, $contentType, $responseBody] = $request->post($requestBody);
return $this->handleResponse(
$statusCode,
@@ -124,11 +170,13 @@ class Client
);
}
- public function get($service, $path)
+ public function get(string $service, string $path): ?array
{
- $request = $this->createRequest($path);
+ $request = $this->createRequest(
+ $path
+ );
- list($statusCode, $contentType, $responseBody) = $request->get();
+ [$statusCode, $contentType, $responseBody] = $request->get();
return $this->handleResponse(
$statusCode,
@@ -139,15 +187,15 @@ class Client
);
}
- private function userAgent()
+ private function userAgent(): string
{
$curlVersion = curl_version();
- return $this->userAgentPrefix . 'MaxMind-WS-API/' . self::VERSION . ' PHP/' . PHP_VERSION .
+ return $this->userAgentPrefix . 'MaxMind-WS-API/' . self::VERSION . ' PHP/' . \PHP_VERSION .
' curl/' . $curlVersion['version'];
}
- private function createRequest($path, $headers = [])
+ private function createRequest(string $path, array $headers = []): Http\Request
{
array_push(
$headers,
@@ -170,11 +218,11 @@ class Client
}
/**
- * @param int $statusCode the HTTP status code of the response
- * @param string $contentType the Content-Type of the response
- * @param string $responseBody the response body
- * @param string $service the name of the service
- * @param string $path the path used in the request
+ * @param int $statusCode the HTTP status code of the response
+ * @param string|null $contentType the Content-Type of the response
+ * @param string|null $responseBody the response body
+ * @param string $service the name of the service
+ * @param string $path the path used in the request
*
* @throws AuthenticationException when there is an issue authenticating the
* request
@@ -185,15 +233,15 @@ class Client
* @throws WebServiceException when some other error occurs. This also
* serves as the base class for the above exceptions
*
- * @return array The decoded content of a successful response
+ * @return array|null The decoded content of a successful response
*/
private function handleResponse(
- $statusCode,
- $contentType,
- $responseBody,
- $service,
- $path
- ) {
+ int $statusCode,
+ ?string $contentType,
+ ?string $responseBody,
+ string $service,
+ string $path
+ ): ?array {
if ($statusCode >= 400 && $statusCode <= 499) {
$this->handle4xx($statusCode, $contentType, $responseBody, $service, $path);
} elseif ($statusCode >= 500) {
@@ -208,20 +256,26 @@ class Client
/**
* @return string describing the JSON error
*/
- private function jsonErrorDescription()
+ private function jsonErrorDescription(): string
{
$errno = json_last_error();
+
switch ($errno) {
- case JSON_ERROR_DEPTH:
+ case \JSON_ERROR_DEPTH:
return 'The maximum stack depth has been exceeded.';
- case JSON_ERROR_STATE_MISMATCH:
+
+ case \JSON_ERROR_STATE_MISMATCH:
return 'Invalid or malformed JSON.';
- case JSON_ERROR_CTRL_CHAR:
+
+ case \JSON_ERROR_CTRL_CHAR:
return 'Control character error.';
- case JSON_ERROR_SYNTAX:
+
+ case \JSON_ERROR_SYNTAX:
return 'Syntax error.';
- case JSON_ERROR_UTF8:
+
+ case \JSON_ERROR_UTF8:
return 'Malformed UTF-8 characters.';
+
default:
return "Other JSON error ($errno).";
}
@@ -232,17 +286,17 @@ class Client
*
* @return string the constructed URL
*/
- private function urlFor($path)
+ private function urlFor(string $path): string
{
- return 'https://' . $this->host . $path;
+ return ($this->useHttps ? 'https://' : 'http://') . $this->host . $path;
}
/**
- * @param int $statusCode the HTTP status code
- * @param string $contentType the response content-type
- * @param string $body the response body
- * @param string $service the service name
- * @param string $path the path used in the request
+ * @param int $statusCode the HTTP status code
+ * @param string|null $contentType the response content-type
+ * @param string|null $body the response body
+ * @param string $service the service name
+ * @param string $path the path used in the request
*
* @throws AuthenticationException
* @throws HttpException
@@ -250,20 +304,20 @@ class Client
* @throws InvalidRequestException
*/
private function handle4xx(
- $statusCode,
- $contentType,
- $body,
- $service,
- $path
- ) {
- if (\strlen($body) === 0) {
+ int $statusCode,
+ ?string $contentType,
+ ?string $body,
+ string $service,
+ string $path
+ ): void {
+ if ($body === null || $body === '') {
throw new HttpException(
"Received a $statusCode error for $service with no body",
$statusCode,
$this->urlFor($path)
);
}
- if (!strstr($contentType, 'json')) {
+ if ($contentType === null || !strstr($contentType, 'json')) {
throw new HttpException(
"Received a $statusCode error for $service with " .
'the following body: ' . $body,
@@ -311,11 +365,11 @@ class Client
* @throws InsufficientFundsException
*/
private function handleWebServiceError(
- $message,
- $code,
- $statusCode,
- $path
- ) {
+ string $message,
+ string $code,
+ int $statusCode,
+ string $path
+ ): void {
switch ($code) {
case 'IP_ADDRESS_NOT_FOUND':
case 'IP_ADDRESS_RESERVED':
@@ -325,6 +379,7 @@ class Client
$statusCode,
$this->urlFor($path)
);
+
case 'ACCOUNT_ID_REQUIRED':
case 'ACCOUNT_ID_UNKNOWN':
case 'AUTHORIZATION_INVALID':
@@ -337,6 +392,7 @@ class Client
$statusCode,
$this->urlFor($path)
);
+
case 'OUT_OF_QUERIES':
case 'INSUFFICIENT_FUNDS':
throw new InsufficientFundsException(
@@ -345,6 +401,7 @@ class Client
$statusCode,
$this->urlFor($path)
);
+
case 'PERMISSION_REQUIRED':
throw new PermissionRequiredException(
$message,
@@ -352,6 +409,7 @@ class Client
$statusCode,
$this->urlFor($path)
);
+
default:
throw new InvalidRequestException(
$message,
@@ -369,7 +427,7 @@ class Client
*
* @throws HttpException
*/
- private function handle5xx($statusCode, $service, $path)
+ private function handle5xx(int $statusCode, string $service, string $path): void
{
throw new HttpException(
"Received a server error ($statusCode) for $service",
@@ -385,7 +443,7 @@ class Client
*
* @throws HttpException
*/
- private function handleUnexpectedStatus($statusCode, $service, $path)
+ private function handleUnexpectedStatus(int $statusCode, string $service, string $path): void
{
throw new HttpException(
'Received an unexpected HTTP status ' .
@@ -396,22 +454,22 @@ class Client
}
/**
- * @param int $statusCode the HTTP status code
- * @param string $body the successful request body
- * @param string $service the service name
+ * @param int $statusCode the HTTP status code
+ * @param string|null $body the successful request body
+ * @param string $service the service name
*
* @throws WebServiceException if a response body is included but not
* expected, or is not expected but not
* included, or is expected and included
* but cannot be decoded as JSON
*
- * @return array the decoded request body
+ * @return array|null the decoded request body
*/
- private function handleSuccess($statusCode, $body, $service)
+ private function handleSuccess(int $statusCode, ?string $body, string $service): ?array
{
// A 204 should have no response body
if ($statusCode === 204) {
- if (\strlen($body) !== 0) {
+ if ($body !== null && $body !== '') {
throw new WebServiceException(
"Received a 204 response for $service along with an " .
"unexpected HTTP body: $body"
@@ -422,7 +480,7 @@ class Client
}
// A 200 should have a valid JSON body
- if (\strlen($body) === 0) {
+ if ($body === null || $body === '') {
throw new WebServiceException(
"Received a 200 response for $service but did not " .
'receive a HTTP body.'
@@ -441,7 +499,7 @@ class Client
return $decodedContent;
}
- private function getCaBundle()
+ private function getCaBundle(): ?string
{
$curlVersion = curl_version();