Skip to content

Commit

Permalink
Run php-cs-fixer on code
Browse files Browse the repository at this point in the history
  • Loading branch information
oschwald committed Jul 6, 2017
1 parent fabc93e commit f648c08
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 141 deletions.
2 changes: 1 addition & 1 deletion src/Exception/AuthenticationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace MaxMind\Exception;

/**
* This class represents an error authenticating
* This class represents an error authenticating.
*/
class AuthenticationException extends InvalidRequestException
{
Expand Down
10 changes: 5 additions & 5 deletions src/Exception/HttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
class HttpException extends WebServiceException
{
/**
* The URI queried
* The URI queried.
*/
private $uri;

/**
* @param string $message A message describing the error.
* @param int $httpStatus The HTTP status code of the response
* @param string $uri The URI used in the request.
* @param \Exception $previous The previous exception, if any.
* @param string $message a message describing the error
* @param int $httpStatus the HTTP status code of the response
* @param string $uri the URI used in the request
* @param \Exception $previous the previous exception, if any
*/
public function __construct(
$message,
Expand Down
12 changes: 6 additions & 6 deletions src/Exception/InvalidRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
class InvalidRequestException extends HttpException
{
/**
* The code returned by the MaxMind web service
* The code returned by the MaxMind web service.
*/
private $error;

/**
* @param string $message The exception message
* @param int $error The error code returned by the MaxMind web service
* @param int $httpStatus The HTTP status code of the response
* @param string $uri The URI queries
* @param \Exception $previous The previous exception, if any.
* @param string $message the exception message
* @param int $error the error code returned by the MaxMind web service
* @param int $httpStatus the HTTP status code of the response
* @param string $uri the URI queries
* @param \Exception $previous the previous exception, if any
*/
public function __construct(
$message,
Expand Down
153 changes: 83 additions & 70 deletions src/WebService/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* This class is not intended to be used directly by an end-user of a
* MaxMind web service. Please use the appropriate client API for the service
* that you are using.
* @package MaxMind\WebService
*
* @internal
*/
class Client
Expand All @@ -35,22 +35,21 @@ class Client
private $userId;

/**
* @param int $userId Your MaxMind user ID
* @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.
* * `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.
* * `timeout` - The timeout to use for the request.
* * `proxy` - The HTTP proxy to use. May include a schema, port,
* username, and password, e.g., `http://username:[email protected]:10`.
* @param int $userId your MaxMind user ID
* @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.
* * `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.
* * `timeout` - The timeout to use for the request.
* * `proxy` - The HTTP proxy to use. May include a schema, port,
* username, and password, e.g., `http://username:[email protected]:10`.
*/
public function __construct(
$userId,
$licenseKey,
$options = array()
$options = []
) {
$this->userId = $userId;
$this->licenseKey = $licenseKey;
Expand Down Expand Up @@ -83,19 +82,21 @@ public function __construct(

/**
* @param string $service name of the service querying
* @param string $path the URI path to use
* @param array $input the data to be posted as JSON
* @param string $path the URI path to use
* @param array $input the data to be posted as JSON
*
* @throws InvalidInputException when the request has missing or invalid
* data
* @throws AuthenticationException when there is an issue authenticating the
* request
* @throws InsufficientFundsException when your account is out of funds
* @throws InvalidRequestException when the request is invalid for some
* other reason, e.g., invalid JSON in the POST.
* @throws HttpException when an unexpected HTTP error occurs
* @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
* @throws InvalidInputException when the request has missing or invalid
* data.
* @throws AuthenticationException when there is an issue authenticating the
* request.
* @throws InsufficientFundsException when your account is out of funds.
* @throws InvalidRequestException when the request is invalid for some
* other reason, e.g., invalid JSON in the POST.
* @throws HttpException when an unexpected HTTP error occurs.
* @throws WebServiceException when some other error occurs. This also
* serves as the base class for the above exceptions.
*/
public function post($service, $path, $input)
{
Expand All @@ -109,10 +110,11 @@ public function post($service, $path, $input)

$request = $this->createRequest(
$path,
array('Content-Type: application/json')
['Content-Type: application/json']
);

list($statusCode, $contentType, $body) = $request->post($body);

return $this->handleResponse(
$statusCode,
$contentType,
Expand All @@ -137,15 +139,15 @@ public function get($service, $path)
);
}


private function userAgent()
{
$curlVersion = curl_version();
return $this->userAgentPrefix . 'MaxMind-WS-API/' . Client::VERSION . ' PHP/' . PHP_VERSION .

return $this->userAgentPrefix . 'MaxMind-WS-API/' . self::VERSION . ' PHP/' . PHP_VERSION .
' curl/' . $curlVersion['version'];
}

private function createRequest($path, $headers = array())
private function createRequest($path, $headers = [])
{
array_push(
$headers,
Expand All @@ -156,32 +158,34 @@ private function createRequest($path, $headers = array())

return $this->httpRequestFactory->request(
$this->urlFor($path),
array(
[
'caBundle' => $this->caBundle,
'connectTimeout' => $this->connectTimeout,
'headers' => $headers,
'proxy' => $this->proxy,
'timeout' => $this->timeout,
'userAgent' => $this->userAgent(),
)
]
);
}

/**
* @param integer $statusCode the HTTP status code of the response
* @param int $statusCode the HTTP status code of the response
* @param string $contentType the Content-Type of the response
* @param string $body the response body
* @param string $service the name of the service
* @param string $path the path used in the request
* @param string $body 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
* @throws InsufficientFundsException when your account is out of funds
* @throws InvalidRequestException when the request is invalid for some
* other reason, e.g., invalid JSON in the POST.
* @throws HttpException when an unexpected HTTP error occurs
* @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
* @throws AuthenticationException when there is an issue authenticating the
* request.
* @throws InsufficientFundsException when your account is out of funds.
* @throws InvalidRequestException when the request is invalid for some
* other reason, e.g., invalid JSON in the POST.
* @throws HttpException when an unexpected HTTP error occurs.
* @throws WebServiceException when some other error occurs. This also
* serves as the base class for the above exceptions
*/
private function handleResponse(
$statusCode,
Expand All @@ -194,9 +198,10 @@ private function handleResponse(
$this->handle4xx($statusCode, $contentType, $body, $service, $path);
} elseif ($statusCode >= 500) {
$this->handle5xx($statusCode, $service, $path);
} elseif ($statusCode != 200) {
} elseif ($statusCode !== 200) {
$this->handleUnexpectedStatus($statusCode, $service, $path);
}

return $this->handleSuccess($body, $service);
}

Expand All @@ -223,20 +228,22 @@ private function jsonErrorDescription()
}

/**
* @param string $path The path to use in the URL
* @return string The constructed URL
* @param string $path the path to use in the URL
*
* @return string the constructed URL
*/
private function urlFor($path)
{
return 'https://' . $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 $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
*
* @throws AuthenticationException
* @throws HttpException
* @throws InsufficientFundsException
Expand All @@ -259,7 +266,7 @@ private function handle4xx(
if (!strstr($contentType, 'json')) {
throw new HttpException(
"Received a $statusCode error for $service with " .
"the following body: " . $body,
'the following body: ' . $body,
$statusCode,
$this->urlFor($path)
);
Expand Down Expand Up @@ -294,10 +301,11 @@ private function handle4xx(
}

/**
* @param string $message The error message from the web service
* @param string $code The error code from the web service
* @param int $statusCode The HTTP status code
* @param string $path The path used in the request
* @param string $message the error message from the web service
* @param string $code the error code from the web service
* @param int $statusCode the HTTP status code
* @param string $path the path used in the request
*
* @throws AuthenticationException
* @throws InvalidRequestException
* @throws InsufficientFundsException
Expand Down Expand Up @@ -353,9 +361,10 @@ private function handleWebServiceError(
}

/**
* @param int $statusCode The HTTP status code
* @param string $service The service name
* @param string $path The URI path used in the request
* @param int $statusCode the HTTP status code
* @param string $service the service name
* @param string $path the URI path used in the request
*
* @throws HttpException
*/
private function handle5xx($statusCode, $service, $path)
Expand All @@ -368,9 +377,10 @@ private function handle5xx($statusCode, $service, $path)
}

/**
* @param int $statusCode The HTTP status code
* @param string $service The service name
* @param string $path The URI path used in the request
* @param int $statusCode the HTTP status code
* @param string $service the service name
* @param string $path the URI path used in the request
*
* @throws HttpException
*/
private function handleUnexpectedStatus($statusCode, $service, $path)
Expand All @@ -384,18 +394,20 @@ private function handleUnexpectedStatus($statusCode, $service, $path)
}

/**
* @param string $body The successful request body
* @param string $service The service name
* @return array The decoded request body
* @param string $body the successful request body
* @param string $service the service name
*
* @throws WebServiceException if the request body cannot be decoded as
* JSON
* JSON
*
* @return array the decoded request body
*/
private function handleSuccess($body, $service)
{
if (strlen($body) == 0) {
if (strlen($body) === 0) {
throw new WebServiceException(
"Received a 200 response for $service but did not " .
"receive a HTTP body."
'receive a HTTP body.'
);
}

Expand All @@ -417,14 +429,14 @@ private function getCaBundle()

// On OS X, when the SSL version is "SecureTransport", the system's
// keychain will be used.
if ($curlVersion['ssl_version'] ==='SecureTransport') {
if ($curlVersion['ssl_version'] === 'SecureTransport') {
return;
}
$cert = CaBundle::getSystemCaRootBundlePath();

// Check if the cert is inside a phar. If so, we need to copy the cert
// to a temp file so that curl can see it.
if (substr($cert, 0, 7) == 'phar://') {
if (substr($cert, 0, 7) === 'phar://') {
$tempDir = sys_get_temp_dir();
$newCert = tempnam($tempDir, 'geoip2-');
if ($newCert === false) {
Expand Down Expand Up @@ -452,6 +464,7 @@ function () use ($newCert) {
if (!file_exists($cert)) {
throw new \RuntimeException("CA cert does not exist at $cert");
}

return $cert;
}
}
7 changes: 4 additions & 3 deletions src/WebService/Http/CurlRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/**
* This class is for internal use only. Semantic versioning does not not apply.
* @package MaxMind\WebService\Http
*
* @internal
*/
class CurlRequest implements Request
Expand All @@ -26,6 +26,7 @@ public function __construct($url, $options)

/**
* @param $body
*
* @return array
*/
public function post($body)
Expand Down Expand Up @@ -62,7 +63,6 @@ private function createCurl()
$opts[CURLOPT_SSL_VERIFYPEER] = true;
$opts[CURLOPT_RETURNTRANSFER] = true;


$opts[CURLOPT_HTTPHEADER] = $this->options['headers'];
$opts[CURLOPT_USERAGENT] = $this->options['userAgent'];
$opts[CURLOPT_PROXY] = $this->options['proxy'];
Expand All @@ -84,6 +84,7 @@ private function createCurl()
}

curl_setopt_array($curl, $opts);

return $curl;
}

Expand All @@ -104,6 +105,6 @@ private function execute($curl)
$contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
curl_close($curl);

return array($statusCode, $contentType, $body);
return [$statusCode, $contentType, $body];
}
}
Loading

0 comments on commit f648c08

Please sign in to comment.