BraintreeCredentialsParser

CredentialsParser registry.

Defined (1)

The class is defined in the following location(s).

/includes/lib/Braintree/lib/Braintree/CredentialsParser.php  
  1. class CredentialsParser 
  2. private $_clientId; 
  3. private $_clientSecret; 
  4. private $_accessToken; 
  5. private $_environment; 
  6. private $_merchantId; 
  7.  
  8. public function __construct($attribs) 
  9. foreach ($attribs as $kind => $value) { 
  10. if ($kind == 'clientId') { 
  11. $this->_clientId = $value; 
  12. if ($kind == 'clientSecret') { 
  13. $this->_clientSecret = $value; 
  14. if ($kind == 'accessToken') { 
  15. $this->_accessToken = $value; 
  16. $this->parse(); 
  17.  
  18. /** 
  19. * @access protected 
  20. * @static 
  21. * @var array valid environments, used for validation 
  22. */ 
  23. private static $_validEnvironments = [ 
  24. 'development',  
  25. 'integration',  
  26. 'sandbox',  
  27. 'production',  
  28. 'qa',  
  29. ]; 
  30.  
  31.  
  32. public function parse() 
  33. $environments = []; 
  34. if (!empty($this->_clientId)) { 
  35. $environments[] = ['clientId', $this->_parseClientCredential('clientId', $this->_clientId, 'client_id')]; 
  36. if (!empty($this->_clientSecret)) { 
  37. $environments[] = ['clientSecret', $this->_parseClientCredential('clientSecret', $this->_clientSecret, 'client_secret')]; 
  38. if (!empty($this->_accessToken)) { 
  39. $environments[] = ['accessToken', $this->_parseAccessToken()]; 
  40.  
  41. $checkEnv = $environments[0]; 
  42. foreach ($environments as $env) { 
  43. if ($env[1] !== $checkEnv[1]) { 
  44. throw new Exception\Configuration( 
  45. 'Mismatched credential environments: ' . $checkEnv[0] . ' environment is ' . $checkEnv[1] . 
  46. ' and ' . $env[0] . ' environment is ' . $env[1]); 
  47.  
  48. self::assertValidEnvironment($checkEnv[1]); 
  49. $this->_environment = $checkEnv[1]; 
  50.  
  51. public static function assertValidEnvironment($environment) { 
  52. if (!in_array($environment, self::$_validEnvironments)) { 
  53. throw new Exception\Configuration('"' . 
  54. $environment . '" is not a valid environment.'); 
  55.  
  56. private function _parseClientCredential($credentialType, $value, $expectedValuePrefix) 
  57. $explodedCredential = explode('$', $value); 
  58. if (sizeof($explodedCredential) != 3) { 
  59. throw new Exception\Configuration('Incorrect ' . $credentialType . ' format. Expected: type$environment$token'); 
  60.  
  61. $gotValuePrefix = $explodedCredential[0]; 
  62. $environment = $explodedCredential[1]; 
  63. $token = $explodedCredential[2]; 
  64.  
  65. if ($gotValuePrefix != $expectedValuePrefix) { 
  66. throw new Exception\Configuration('Value passed for ' . $credentialType . ' is not a ' . $credentialType); 
  67.  
  68. return $environment; 
  69.  
  70. private function _parseAccessToken() 
  71. $accessTokenExploded = explode('$', $this->_accessToken); 
  72. if (sizeof($accessTokenExploded) != 4) { 
  73. throw new Exception\Configuration('Incorrect accessToken syntax. Expected: type$environment$merchant_id$token'); 
  74.  
  75. $gotValuePrefix = $accessTokenExploded[0]; 
  76. $environment = $accessTokenExploded[1]; 
  77. $merchantId = $accessTokenExploded[2]; 
  78. $token = $accessTokenExploded[3]; 
  79.  
  80. if ($gotValuePrefix != 'access_token') { 
  81. throw new Exception\Configuration('Value passed for accessToken is not an accessToken'); 
  82.  
  83. $this->_merchantId = $merchantId; 
  84. return $environment; 
  85.  
  86. public function getClientId() 
  87. return $this->_clientId; 
  88.  
  89. public function getClientSecret() 
  90. return $this->_clientSecret; 
  91.  
  92. public function getAccessToken() 
  93. return $this->_accessToken; 
  94.  
  95. public function getEnvironment() 
  96. return $this->_environment; 
  97.  
  98. public function getMerchantId() 
  99. return $this->_merchantId;