CredentialsParser.php 4.1 KB

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