AbstractService.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace OAuth\Common\Service;
  3. use OAuth\Common\Consumer\CredentialsInterface;
  4. use OAuth\Common\Http\Client\ClientInterface;
  5. use OAuth\Common\Http\Uri\Uri;
  6. use OAuth\Common\Http\Uri\UriInterface;
  7. use OAuth\Common\Exception\Exception;
  8. use OAuth\Common\Storage\TokenStorageInterface;
  9. /**
  10. * Abstract OAuth service, version-agnostic
  11. */
  12. abstract class AbstractService implements ServiceInterface
  13. {
  14. /** @var Credentials */
  15. protected $credentials;
  16. /** @var ClientInterface */
  17. protected $httpClient;
  18. /** @var TokenStorageInterface */
  19. protected $storage;
  20. /**
  21. * @param CredentialsInterface $credentials
  22. * @param ClientInterface $httpClient
  23. * @param TokenStorageInterface $storage
  24. */
  25. public function __construct(
  26. CredentialsInterface $credentials,
  27. ClientInterface $httpClient,
  28. TokenStorageInterface $storage
  29. ) {
  30. $this->credentials = $credentials;
  31. $this->httpClient = $httpClient;
  32. $this->storage = $storage;
  33. }
  34. /**
  35. * @param UriInterface|string $path
  36. * @param UriInterface $baseApiUri
  37. *
  38. * @return UriInterface
  39. *
  40. * @throws Exception
  41. */
  42. protected function determineRequestUriFromPath($path, UriInterface $baseApiUri = null)
  43. {
  44. if ($path instanceof UriInterface) {
  45. $uri = $path;
  46. } elseif (stripos($path, 'http://') === 0 || stripos($path, 'https://') === 0) {
  47. $uri = new Uri($path);
  48. } else {
  49. if (null === $baseApiUri) {
  50. throw new Exception(
  51. 'An absolute URI must be passed to ServiceInterface::request as no baseApiUri is set.'
  52. );
  53. }
  54. $uri = clone $baseApiUri;
  55. if (false !== strpos($path, '?')) {
  56. $parts = explode('?', $path, 2);
  57. $path = $parts[0];
  58. $query = $parts[1];
  59. $uri->setQuery($query);
  60. }
  61. if ($path[0] === '/') {
  62. $path = substr($path, 1);
  63. }
  64. $uri->setPath($uri->getPath() . $path);
  65. }
  66. return $uri;
  67. }
  68. /**
  69. * Accessor to the storage adapter to be able to retrieve tokens
  70. *
  71. * @return TokenStorageInterface
  72. */
  73. public function getStorage()
  74. {
  75. return $this->storage;
  76. }
  77. /**
  78. * @return string
  79. */
  80. public function service()
  81. {
  82. // get class name without backslashes
  83. $classname = get_class($this);
  84. return preg_replace('/^.*\\\\/', '', $classname);
  85. }
  86. }