Renaming Namespaces and Organizing Classes

- Updated OpenId classes to be in API namespace
- Updated PP Naming Convention to PayPal Naming Convention
- FormatConverter Class got its own namespace
- Handlers are grouped in Handler namespace
- Samples and Tests Updated Accordingly
This commit is contained in:
japatel
2014-12-17 17:15:01 -06:00
parent 4cb951f8b2
commit 9c0827643b
246 changed files with 1067 additions and 1057 deletions

View File

@@ -2,8 +2,8 @@
namespace PayPal\Rest;
use PayPal\Core\PPConfigManager;
use PayPal\Core\PPCredentialManager;
use PayPal\Core\PayPalConfigManager;
use PayPal\Core\PayPalCredentialManager;
/**
* Class ApiContext
@@ -26,7 +26,7 @@ class ApiContext
/**
* This is a placeholder for holding credential for the request
* If the value is not set, it would get the value from @\PayPal\Core\PPCredentialManager
* If the value is not set, it would get the value from @\PayPal\Core\PayPalCredentialManager
*
* @var \Paypal\Auth\OAuthTokenCredential
*/
@@ -53,14 +53,14 @@ class ApiContext
public function getCredential()
{
if ($this->credential == null) {
return PPCredentialManager::getInstance()->getCredentialObject();
return PayPalCredentialManager::getInstance()->getCredentialObject();
}
return $this->credential;
}
public function getRequestHeaders()
{
$result = PPConfigManager::getInstance()->get('http.headers');
$result = PayPalConfigManager::getInstance()->get('http.headers');
$headers = array();
foreach ($result as $header => $value) {
$headerName = ltrim($header, 'http.headers');
@@ -75,7 +75,7 @@ class ApiContext
if (!(substr($name, 0, strlen('http.headers')) === 'http.headers')) {
$name = 'http.headers.' . $name;
}
PPConfigManager::getInstance()->addConfigs(array($name => $value));
PayPalConfigManager::getInstance()->addConfigs(array($name => $value));
}
/**
@@ -112,7 +112,7 @@ class ApiContext
*/
public function setConfig(array $config)
{
PPConfigManager::getInstance()->addConfigs($config);
PayPalConfigManager::getInstance()->addConfigs($config);
}
/**
@@ -122,7 +122,7 @@ class ApiContext
*/
public function getConfig()
{
return PPConfigManager::getInstance()->getConfigHashmap();
return PayPalConfigManager::getInstance()->getConfigHashmap();
}
/**
@@ -133,7 +133,7 @@ class ApiContext
*/
public function get($searchKey)
{
return PPConfigManager::getInstance()->get($searchKey);
return PayPalConfigManager::getInstance()->get($searchKey);
}
/**

View File

@@ -1,104 +0,0 @@
<?php
/**
* API handler for OAuth Token Request REST API calls
*/
namespace PayPal\Rest;
use PayPal\Common\PPUserAgent;
use PayPal\Core\PPConstants;
use PayPal\Core\PPHttpConfig;
use PayPal\Exception\PPConfigurationException;
use PayPal\Exception\PPInvalidCredentialException;
use PayPal\Exception\PPMissingCredentialException;
use PayPal\Handler\IPPHandler;
/**
* Class OauthHandler
*/
class OauthHandler implements IPPHandler
{
/**
* Private Variable
*
* @var \Paypal\Rest\ApiContext $apiContext
*/
private $apiContext;
/**
* Construct
*
* @param \Paypal\Rest\ApiContext $apiContext
*/
public function __construct($apiContext)
{
$this->apiContext = $apiContext;
}
/**
* @param PPHttpConfig $httpConfig
* @param string $request
* @param mixed $options
* @return mixed|void
* @throws PPConfigurationException
* @throws PPInvalidCredentialException
* @throws PPMissingCredentialException
*/
public function handle($httpConfig, $request, $options)
{
$config = $this->apiContext->getConfig();
$httpConfig->setUrl(
rtrim(trim($this->_getEndpoint($config)), '/') .
(isset($options['path']) ? $options['path'] : '')
);
$headers = array(
"User-Agent" => PPUserAgent::getValue(PPConstants::SDK_NAME, PPConstants::SDK_VERSION),
"Authorization" => "Basic " . base64_encode($options['clientId'] . ":" . $options['clientSecret']),
"Accept" => "*/*"
);
$httpConfig->setHeaders($headers);
// Add any additional Headers that they may have provided
$headers = $this->apiContext->getRequestHeaders();
foreach ($headers as $key => $value) {
$httpConfig->addHeader($key, $value);
}
}
/**
* Get HttpConfiguration object for OAuth API
*
* @param array $config
*
* @return PPHttpConfig
* @throws \PayPal\Exception\PPConfigurationException
*/
private static function _getEndpoint($config)
{
if (isset($config['oauth.EndPoint'])) {
$baseEndpoint = $config['oauth.EndPoint'];
} else if (isset($config['service.EndPoint'])) {
$baseEndpoint = $config['service.EndPoint'];
} else if (isset($config['mode'])) {
switch (strtoupper($config['mode'])) {
case 'SANDBOX':
$baseEndpoint = PPConstants::REST_SANDBOX_ENDPOINT;
break;
case 'LIVE':
$baseEndpoint = PPConstants::REST_LIVE_ENDPOINT;
break;
default:
throw new PPConfigurationException('The mode config parameter must be set to either sandbox/live');
}
} else {
// Defaulting to Sandbox
$baseEndpoint = PPConstants::REST_SANDBOX_ENDPOINT;
}
$baseEndpoint = rtrim(trim($baseEndpoint), '/') . "/v1/oauth2/token";
return $baseEndpoint;
}
}

View File

@@ -1,123 +0,0 @@
<?php
/**
* API handler for all REST API calls
*/
namespace PayPal\Rest;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Common\PPUserAgent;
use PayPal\Core\PPConstants;
use PayPal\Core\PPCredentialManager;
use PayPal\Core\PPHttpConfig;
use PayPal\Exception\PPConfigurationException;
use PayPal\Exception\PPInvalidCredentialException;
use PayPal\Exception\PPMissingCredentialException;
use PayPal\Handler\IPPHandler;
/**
* Class RestHandler
*/
class RestHandler implements IPPHandler
{
/**
* Private Variable
*
* @var \Paypal\Rest\ApiContext $apiContext
*/
private $apiContext;
/**
* Construct
*
* @param \Paypal\Rest\ApiContext $apiContext
*/
public function __construct($apiContext)
{
$this->apiContext = $apiContext;
}
/**
* @param PPHttpConfig $httpConfig
* @param string $request
* @param mixed $options
* @return mixed|void
* @throws PPConfigurationException
* @throws PPInvalidCredentialException
* @throws PPMissingCredentialException
*/
public function handle($httpConfig, $request, $options)
{
$credential = $this->apiContext->getCredential();
$config = $this->apiContext->getConfig();
if ($credential == null) {
// Try picking credentials from the config file
$credMgr = PPCredentialManager::getInstance($config);
$credValues = $credMgr->getCredentialObject();
if (!is_array($credValues)) {
throw new PPMissingCredentialException("Empty or invalid credentials passed");
}
$credential = new OAuthTokenCredential($credValues['clientId'], $credValues['clientSecret']);
}
if ($credential == null || !($credential instanceof OAuthTokenCredential)) {
throw new PPInvalidCredentialException("Invalid credentials passed");
}
$httpConfig->setUrl(
rtrim(trim($this->_getEndpoint($config)), '/') .
(isset($options['path']) ? $options['path'] : '')
);
if (!array_key_exists("User-Agent", $httpConfig->getHeaders())) {
$httpConfig->addHeader("User-Agent", PPUserAgent::getValue(PPConstants::SDK_NAME, PPConstants::SDK_VERSION));
}
if (!is_null($credential) && $credential instanceof OAuthTokenCredential && is_null($httpConfig->getHeader('Authorization'))) {
$httpConfig->addHeader('Authorization', "Bearer " . $credential->getAccessToken($config), false);
}
if ($httpConfig->getMethod() == 'POST' || $httpConfig->getMethod() == 'PUT') {
$httpConfig->addHeader('PayPal-Request-Id', $this->apiContext->getRequestId());
}
// Add any additional Headers that they may have provided
$headers = $this->apiContext->getRequestHeaders();
foreach ($headers as $key => $value) {
$httpConfig->addHeader($key, $value);
}
}
/**
* End Point
*
* @param array $config
*
* @return string
* @throws \PayPal\Exception\PPConfigurationException
*/
private function _getEndpoint($config)
{
if (isset($config['service.EndPoint'])) {
return $config['service.EndPoint'];
} else if (isset($config['mode'])) {
switch (strtoupper($config['mode'])) {
case 'SANDBOX':
return PPConstants::REST_SANDBOX_ENDPOINT;
break;
case 'LIVE':
return PPConstants::REST_LIVE_ENDPOINT;
break;
default:
throw new PPConfigurationException('The mode config parameter must be set to either sandbox/live');
break;
}
} else {
// Defaulting to Sandbox
return PPConstants::REST_SANDBOX_ENDPOINT;
}
}
}