OAuthServiceFactory

The Gravity Forms Salesforce Add-On OAuth ServiceFactory class.

Defined (1)

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

/lib/PHPoAuthLib/src/OAuth/ServiceFactory.php  
  1. class ServiceFactory 
  2. /** 
  3. *@var ClientInterface 
  4. */ 
  5. protected $httpClient; 
  6.  
  7. /** 
  8. * @var array 
  9. */ 
  10. protected $serviceClassMap = array( 
  11. 'OAuth1' => array(),  
  12. 'OAuth2' => array() 
  13. ); 
  14.  
  15. /** 
  16. * @var array 
  17. */ 
  18. protected $serviceBuilders = array( 
  19. 'OAuth2' => 'buildV2Service',  
  20. 'OAuth1' => 'buildV1Service',  
  21. ); 
  22.  
  23. /** 
  24. * @param ClientInterface $httpClient 
  25. * @return ServiceFactory 
  26. */ 
  27. public function setHttpClient(ClientInterface $httpClient) 
  28. $this->httpClient = $httpClient; 
  29.  
  30. return $this; 
  31.  
  32. /** 
  33. * Register a custom service to classname mapping. 
  34. * @param string $serviceName Name of the service 
  35. * @param string $className Class to instantiate 
  36. * @return ServiceFactory 
  37. * @throws Exception If the class is nonexistent or does not implement a valid ServiceInterface 
  38. */ 
  39. public function registerService($serviceName, $className) 
  40. if (!class_exists($className)) { 
  41. throw new Exception(sprintf('Service class %s does not exist.', $className)); 
  42.  
  43. $reflClass = new \ReflectionClass($className); 
  44.  
  45. foreach (array('OAuth2', 'OAuth1') as $version) { 
  46. if ($reflClass->implementsInterface('OAuth\\' . $version . '\\Service\\ServiceInterface')) { 
  47. $this->serviceClassMap[$version][ucfirst($serviceName)] = $className; 
  48.  
  49. return $this; 
  50.  
  51. throw new Exception(sprintf('Service class %s must implement ServiceInterface.', $className)); 
  52.  
  53. /** 
  54. * Builds and returns oauth services 
  55. * It will first try to build an OAuth2 service and if none found it will try to build an OAuth1 service 
  56. * @param string $serviceName Name of service to create 
  57. * @param CredentialsInterface $credentials 
  58. * @param TokenStorageInterface $storage 
  59. * @param array|null $scopes If creating an oauth2 service, array of scopes 
  60. * @param UriInterface|null $baseApiUri 
  61. * @return ServiceInterface 
  62. */ 
  63. public function createService( 
  64. $serviceName,  
  65. CredentialsInterface $credentials,  
  66. TokenStorageInterface $storage,  
  67. $scopes = array(),  
  68. UriInterface $baseApiUri = null 
  69. ) { 
  70. if (!$this->httpClient) { 
  71. // for backwards compatibility. 
  72. $this->httpClient = new StreamClient(); 
  73.  
  74. foreach ($this->serviceBuilders as $version => $buildMethod) { 
  75. $fullyQualifiedServiceName = $this->getFullyQualifiedServiceName($serviceName, $version); 
  76.  
  77. if (class_exists($fullyQualifiedServiceName)) { 
  78. return $this->$buildMethod($fullyQualifiedServiceName, $credentials, $storage, $scopes, $baseApiUri); 
  79.  
  80. return null; 
  81.  
  82. /** 
  83. * Gets the fully qualified name of the service 
  84. * @param string $serviceName The name of the service of which to get the fully qualified name 
  85. * @param string $type The type of the service to get (either OAuth1 or OAuth2) 
  86. * @return string The fully qualified name of the service 
  87. */ 
  88. private function getFullyQualifiedServiceName($serviceName, $type) 
  89. $serviceName = ucfirst($serviceName); 
  90.  
  91. if (isset($this->serviceClassMap[$type][$serviceName])) { 
  92. return $this->serviceClassMap[$type][$serviceName]; 
  93.  
  94. return '\\OAuth\\' . $type . '\\Service\\' . $serviceName; 
  95.  
  96. /** 
  97. * Builds v2 services 
  98. * @param string $serviceName The fully qualified service name 
  99. * @param CredentialsInterface $credentials 
  100. * @param TokenStorageInterface $storage 
  101. * @param array|null $scopes Array of scopes for the service 
  102. * @param UriInterface|null $baseApiUri 
  103. * @return ServiceInterface 
  104. * @throws Exception 
  105. */ 
  106. private function buildV2Service( 
  107. $serviceName,  
  108. CredentialsInterface $credentials,  
  109. TokenStorageInterface $storage,  
  110. array $scopes,  
  111. UriInterface $baseApiUri = null 
  112. ) { 
  113. return new $serviceName( 
  114. $credentials,  
  115. $this->httpClient,  
  116. $storage,  
  117. $this->resolveScopes($serviceName, $scopes),  
  118. $baseApiUri 
  119. ); 
  120.  
  121. /** 
  122. * Resolves scopes for v2 services 
  123. * @param string $serviceName The fully qualified service name 
  124. * @param array $scopes List of scopes for the service 
  125. * @return array List of resolved scopes 
  126. */ 
  127. private function resolveScopes($serviceName, array $scopes) 
  128. $reflClass = new \ReflectionClass($serviceName); 
  129. $constants = $reflClass->getConstants(); 
  130.  
  131. $resolvedScopes = array(); 
  132. foreach ($scopes as $scope) { 
  133. $key = strtoupper('SCOPE_' . $scope); 
  134.  
  135. if (array_key_exists($key, $constants)) { 
  136. $resolvedScopes[] = $constants[$key]; 
  137. } else { 
  138. $resolvedScopes[] = $scope; 
  139.  
  140. return $resolvedScopes; 
  141.  
  142. /** 
  143. * Builds v1 services 
  144. * @param string $serviceName The fully qualified service name 
  145. * @param CredentialsInterface $credentials 
  146. * @param TokenStorageInterface $storage 
  147. * @param array $scopes 
  148. * @param UriInterface $baseApiUri 
  149. * @return ServiceInterface 
  150. * @throws Exception 
  151. */ 
  152. private function buildV1Service( 
  153. $serviceName,  
  154. CredentialsInterface $credentials,  
  155. TokenStorageInterface $storage,  
  156. $scopes,  
  157. UriInterface $baseApiUri = null 
  158. ) { 
  159. if (!empty($scopes)) { 
  160. throw new Exception( 
  161. 'Scopes passed to ServiceFactory::createService but an OAuth1 service was requested.' 
  162. ); 
  163.  
  164. return new $serviceName($credentials, $this->httpClient, $storage, new Signature($credentials), $baseApiUri);