Skip to content

Commit

Permalink
Add namespaces
Browse files Browse the repository at this point in the history
All classes in Stripe namespace
Use psr-4 autoloading in composer
  • Loading branch information
chadicus committed Jan 12, 2015
1 parent 962e05d commit ac93110
Show file tree
Hide file tree
Showing 70 changed files with 600 additions and 532 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
"squizlabs/php_codesniffer": "~2.0"
},
"autoload": {
"classmap": ["lib/Stripe/"]
"psr-4": { "Stripe\\" : "lib" }
}
}
File renamed without changes.
File renamed without changes
6 changes: 4 additions & 2 deletions lib/Stripe/Account.php → lib/Account.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

class Stripe_Account extends Stripe_SingletonApiResource
namespace Stripe;

class Account extends SingletonApiResource
{
/**
* @param string|null $apiKey
*
* @return Stripe_Account
* @return Account
*/
public static function retrieve($apiKey=null)
{
Expand Down
7 changes: 7 additions & 0 deletions lib/ApiConnectionError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Stripe;

class ApiConnectionError extends Error
{
}
7 changes: 7 additions & 0 deletions lib/ApiError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Stripe;

class ApiError extends Error
{
}
55 changes: 28 additions & 27 deletions lib/Stripe/ApiRequestor.php → lib/ApiRequestor.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

class Stripe_ApiRequestor
namespace Stripe;

class ApiRequestor
{
private $_apiKey;

Expand Down Expand Up @@ -40,7 +42,7 @@ public static function utf8($value)

private static function _encodeObjects($d)
{
if ($d instanceof Stripe_ApiResource) {
if ($d instanceof ApiResource) {
return self::utf8($d->id);
} else if ($d === true) {
return 'true';
Expand Down Expand Up @@ -105,25 +107,24 @@ public function request($method, $url, $params=null)
return array($resp, $myApiKey);
}


/**
* @param string $rbody A JSON string.
* @param int $rcode
* @param array $resp
*
* @throws Stripe_InvalidRequestError if the error is caused by the user.
* @throws Stripe_AuthenticationError if the error is caused by a lack of
* @throws InvalidRequestError if the error is caused by the user.
* @throws AuthenticationError if the error is caused by a lack of
* permissions.
* @throws Stripe_CardError if the error is the error code is 402 (payment
* @throws CardError if the error is the error code is 402 (payment
* required)
* @throws Stripe_ApiError otherwise.
* @throws ApiError otherwise.
*/
public function handleApiError($rbody, $rcode, $resp)
{
if (!is_array($resp) || !isset($resp['error'])) {
$msg = "Invalid response object from API: $rbody "
."(HTTP response code was $rcode)";
throw new Stripe_ApiError($msg, $rcode, $rbody, $resp);
throw new ApiError($msg, $rcode, $rbody, $resp);
}

$error = $resp['error'];
Expand All @@ -134,20 +135,20 @@ public function handleApiError($rbody, $rcode, $resp)
switch ($rcode) {
case 400:
if ($code == 'rate_limit') {
throw new Stripe_RateLimitError(
throw new RateLimitError(
$msg, $param, $rcode, $rbody, $resp
);
}
case 404:
throw new Stripe_InvalidRequestError(
throw new InvalidRequestError(
$msg, $param, $rcode, $rbody, $resp
);
case 401:
throw new Stripe_AuthenticationError($msg, $rcode, $rbody, $resp);
throw new AuthenticationError($msg, $rcode, $rbody, $resp);
case 402:
throw new Stripe_CardError($msg, $param, $code, $rcode, $rbody, $resp);
throw new CardError($msg, $param, $code, $rcode, $rbody, $resp);
default:
throw new Stripe_ApiError($msg, $rcode, $rbody, $resp);
throw new ApiError($msg, $rcode, $rbody, $resp);
}
}

Expand All @@ -168,7 +169,7 @@ private function _requestRaw($method, $url, $params)
. '"Stripe::setApiKey(<API-KEY>)". You can generate API keys from '
. 'the Stripe web interface. See https://stripe.com/api for '
. 'details, or email [email protected] if you have any questions.';
throw new Stripe_AuthenticationError($msg);
throw new AuthenticationError($msg);
}

$absUrl = $this->_apiBase.$url;
Expand All @@ -191,12 +192,12 @@ private function _requestRaw($method, $url, $params)
$headers[] = 'Stripe-Version: ' . Stripe::$apiVersion;
}
$hasFile = false;
$hasCurlFile = class_exists('CURLFile');
$hasCurlFile = class_exists('\CURLFile');
foreach ($params as $k => $v) {
if (is_resource($v)) {
$hasFile = true;
$params[$k] = self::_processResourceParam($v);
} else if ($hasCurlFile && $v instanceof CURLFile) {
} else if ($hasCurlFile && $v instanceof \CURLFile) {
$hasFile = true;
}
}
Expand All @@ -220,21 +221,21 @@ private function _requestRaw($method, $url, $params)
private function _processResourceParam($resource)
{
if (get_resource_type($resource) !== 'stream') {
throw new Stripe_ApiError(
throw new ApiError(
'Attempted to upload a resource that is not a stream'
);
}

$metaData = stream_get_meta_data($resource);
if ($metaData['wrapper_type'] !== 'plainfile') {
throw new Stripe_ApiError(
throw new ApiError(
'Only plainfile resource streams are supported'
);
}

if (class_exists('CURLFile')) {
if (class_exists('\CURLFile')) {
// We don't have the filename or mimetype, but the API doesn't care
return new CURLFile($metaData['uri']);
return new \CURLFile($metaData['uri']);
} else {
return '@'.$metaData['uri'];
}
Expand All @@ -247,7 +248,7 @@ private function _interpretResponse($rbody, $rcode)
} catch (Exception $e) {
$msg = "Invalid response body from API: $rbody "
. "(HTTP response code was $rcode)";
throw new Stripe_ApiError($msg, $rcode, $rbody);
throw new ApiError($msg, $rcode, $rbody);
}

if ($rcode < 200 || $rcode >= 300) {
Expand All @@ -263,7 +264,7 @@ private function _curlRequest($method, $absUrl, $headers, $params, $hasFile)
$opts = array();
if ($method == 'get') {
if ($hasFile) {
throw new Stripe_ApiError(
throw new ApiError(
"Issuing a GET request with a file parameter"
);
}
Expand All @@ -282,7 +283,7 @@ private function _curlRequest($method, $absUrl, $headers, $params, $hasFile)
$absUrl = "$absUrl?$encoded";
}
} else {
throw new Stripe_ApiError("Unrecognized method $method");
throw new ApiError("Unrecognized method $method");
}

$absUrl = self::utf8($absUrl);
Expand Down Expand Up @@ -331,7 +332,7 @@ private function _curlRequest($method, $absUrl, $headers, $params, $hasFile)
/**
* @param number $errno
* @param string $message
* @throws Stripe_ApiConnectionError
* @throws ApiConnectionError
*/
public function handleCurlError($errno, $message)
{
Expand Down Expand Up @@ -359,7 +360,7 @@ public function handleCurlError($errno, $message)
$msg .= " let us know at [email protected].";

$msg .= "\n\n(Network error [errno $errno]: $message)";
throw new Stripe_ApiConnectionError($msg);
throw new ApiConnectionError($msg);
}

/**
Expand Down Expand Up @@ -399,7 +400,7 @@ private function checkSslCert($url)
$url, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $sslContext
);
if (($errno !== 0 && $errno !== NULL) || $result === false) {
throw new Stripe_ApiConnectionError(
throw new ApiConnectionError(
'Could not connect to Stripe (' . $url . '). Please check your '.
'internet connection and try again. If this problem persists, '.
'you should check Stripe\'s service status at '.
Expand All @@ -414,7 +415,7 @@ private function checkSslCert($url)
openssl_x509_export($cert, $pemCert);

if (self::isBlackListed($pemCert)) {
throw new Stripe_ApiConnectionError(
throw new ApiConnectionError(
'Invalid server certificate. You tried to connect to a server that '.
'has a revoked SSL certificate, which means we cannot securely send '.
'data to that server. Please email [email protected] if you need '.
Expand Down
34 changes: 18 additions & 16 deletions lib/Stripe/ApiResource.php → lib/ApiResource.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

abstract class Stripe_ApiResource extends Stripe_Object
namespace Stripe;

abstract class ApiResource extends Object
{
public static function baseUrl()
{
Expand All @@ -15,11 +17,11 @@ protected static function _scopedRetrieve($class, $id, $apiKey=null)
}

/**
* @returns Stripe_ApiResource The refreshed resource.
* @returns ApiResource The refreshed resource.
*/
public function refresh()
{
$requestor = new Stripe_ApiRequestor($this->_apiKey, self::baseUrl());
$requestor = new ApiRequestor($this->_apiKey, self::baseUrl());
$url = $this->instanceUrl();

list($response, $apiKey) = $requestor->request(
Expand All @@ -39,12 +41,12 @@ public function refresh()
*/
public static function className($class)
{
// Useful for namespaces: Foo\Stripe_Charge
// Useful for namespaces: Foo\Charge
if ($postfixNamespaces = strrchr($class, '\\')) {
$class = substr($postfixNamespaces, 1);
}
// Useful for underscored 'namespaces': Foo_Stripe_Charge
if ($postfixFakeNamespaces = strrchr($class, 'Stripe_')) {
// Useful for underscored 'namespaces': Foo_Charge
if ($postfixFakeNamespaces = strrchr($class, '')) {
$class = $postfixFakeNamespaces;
}
if (substr($class, 0, strlen('Stripe')) == 'Stripe') {
Expand Down Expand Up @@ -77,9 +79,9 @@ public function instanceUrl()
if ($id === null) {
$message = "Could not determine which URL to request: "
. "$class instance has invalid ID: $id";
throw new Stripe_InvalidRequestError($message, null);
throw new InvalidRequestError($message, null);
}
$id = Stripe_ApiRequestor::utf8($id);
$id = ApiRequestor::utf8($id);
$base = $this->_lsb('classUrl', $class);
$extn = urlencode($id);
return "$base/$extn";
Expand All @@ -93,15 +95,15 @@ private static function _validateCall($method, $params=null, $apiKey=null)
. "would be: \"StripeCharge::create(array('amount' => 100, "
. "'currency' => 'usd', 'card' => array('number' => "
. "4242424242424242, 'exp_month' => 5, 'exp_year' => 2015)))\")";
throw new Stripe_Error($message);
throw new Error($message);
}

if ($apiKey && !is_string($apiKey)) {
$message = 'The second argument to Stripe API method calls is an '
. 'optional per-request apiKey, which must be a string. '
. '(HINT: you can set a global apiKey by '
. '"Stripe::setApiKey(<apiKey>)")';
throw new Stripe_Error($message);
throw new Error($message);
}
}

Expand All @@ -111,9 +113,9 @@ protected static function _scopedAll($class, $params=null, $apiKey=null)
$base = self::_scopedLsb($class, 'baseUrl');
$url = self::_scopedLsb($class, 'classUrl', $class);

$requestor = new Stripe_ApiRequestor($apiKey, $base);
$requestor = new ApiRequestor($apiKey, $base);
list($response, $apiKey) = $requestor->request('get', $url, $params);
return Stripe_Util::convertToStripeObject($response, $apiKey);
return Util::convertToStripeObject($response, $apiKey);
}

protected static function _scopedCreate($class, $params=null, $apiKey=null)
Expand All @@ -122,15 +124,15 @@ protected static function _scopedCreate($class, $params=null, $apiKey=null)
$base = self::_scopedLsb($class, 'baseUrl');
$url = self::_scopedLsb($class, 'classUrl', $class);

$requestor = new Stripe_ApiRequestor($apiKey, $base);
$requestor = new ApiRequestor($apiKey, $base);
list($response, $apiKey) = $requestor->request('post', $url, $params);
return Stripe_Util::convertToStripeObject($response, $apiKey);
return Util::convertToStripeObject($response, $apiKey);
}

protected function _scopedSave($class)
{
self::_validateCall('save');
$requestor = new Stripe_ApiRequestor($this->_apiKey, self::baseUrl());
$requestor = new ApiRequestor($this->_apiKey, self::baseUrl());
$params = $this->serializeParameters();

if (count($params) > 0) {
Expand All @@ -144,7 +146,7 @@ protected function _scopedSave($class)
protected function _scopedDelete($class, $params=null)
{
self::_validateCall('delete');
$requestor = new Stripe_ApiRequestor($this->_apiKey, self::baseUrl());
$requestor = new ApiRequestor($this->_apiKey, self::baseUrl());
$url = $this->instanceUrl();
list($response, $apiKey) = $requestor->request('delete', $url, $params);
$this->refreshFrom($response, $apiKey);
Expand Down
10 changes: 6 additions & 4 deletions lib/Stripe/ApplicationFee.php → lib/ApplicationFee.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

class Stripe_ApplicationFee extends Stripe_ApiResource
namespace Stripe;

class ApplicationFee extends ApiResource
{
/**
* This is a special case because the application fee endpoint has an
Expand All @@ -17,7 +19,7 @@ public static function className($class)
* @param string $id The ID of the application fee to retrieve.
* @param string|null $apiKey
*
* @return Stripe_ApplicationFee
* @return ApplicationFee
*/
public static function retrieve($id, $apiKey=null)
{
Expand All @@ -40,11 +42,11 @@ public static function all($params=null, $apiKey=null)
/**
* @param string|null $params
*
* @return Stripe_ApplicationFee The refunded application fee.
* @return ApplicationFee The refunded application fee.
*/
public function refund($params=null)
{
$requestor = new Stripe_ApiRequestor($this->_apiKey);
$requestor = new ApiRequestor($this->_apiKey);
$url = $this->instanceUrl() . '/refund';
list($response, $apiKey) = $requestor->request('post', $url, $params);
$this->refreshFrom($response, $apiKey);
Expand Down
Loading

0 comments on commit ac93110

Please sign in to comment.