Memory.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace OAuth\Common\Storage;
  3. use OAuth\Common\Token\TokenInterface;
  4. use OAuth\Common\Storage\Exception\TokenNotFoundException;
  5. use OAuth\Common\Storage\Exception\AuthorizationStateNotFoundException;
  6. /*
  7. * Stores a token in-memory only (destroyed at end of script execution).
  8. */
  9. class Memory implements TokenStorageInterface
  10. {
  11. /**
  12. * @var object|TokenInterface
  13. */
  14. protected $tokens;
  15. /**
  16. * @var array
  17. */
  18. protected $states;
  19. public function __construct()
  20. {
  21. $this->tokens = array();
  22. $this->states = array();
  23. }
  24. /**
  25. * {@inheritDoc}
  26. */
  27. public function retrieveAccessToken($service)
  28. {
  29. if ($this->hasAccessToken($service)) {
  30. return $this->tokens[$service];
  31. }
  32. throw new TokenNotFoundException('Token not stored');
  33. }
  34. /**
  35. * {@inheritDoc}
  36. */
  37. public function storeAccessToken($service, TokenInterface $token)
  38. {
  39. $this->tokens[$service] = $token;
  40. // allow chaining
  41. return $this;
  42. }
  43. /**
  44. * {@inheritDoc}
  45. */
  46. public function hasAccessToken($service)
  47. {
  48. return isset($this->tokens[$service]) && $this->tokens[$service] instanceof TokenInterface;
  49. }
  50. /**
  51. * {@inheritDoc}
  52. */
  53. public function clearToken($service)
  54. {
  55. if (array_key_exists($service, $this->tokens)) {
  56. unset($this->tokens[$service]);
  57. }
  58. // allow chaining
  59. return $this;
  60. }
  61. /**
  62. * {@inheritDoc}
  63. */
  64. public function clearAllTokens()
  65. {
  66. $this->tokens = array();
  67. // allow chaining
  68. return $this;
  69. }
  70. /**
  71. * {@inheritDoc}
  72. */
  73. public function retrieveAuthorizationState($service)
  74. {
  75. if ($this->hasAuthorizationState($service)) {
  76. return $this->states[$service];
  77. }
  78. throw new AuthorizationStateNotFoundException('State not stored');
  79. }
  80. /**
  81. * {@inheritDoc}
  82. */
  83. public function storeAuthorizationState($service, $state)
  84. {
  85. $this->states[$service] = $state;
  86. // allow chaining
  87. return $this;
  88. }
  89. /**
  90. * {@inheritDoc}
  91. */
  92. public function hasAuthorizationState($service)
  93. {
  94. return isset($this->states[$service]) && null !== $this->states[$service];
  95. }
  96. /**
  97. * {@inheritDoc}
  98. */
  99. public function clearAuthorizationState($service)
  100. {
  101. if (array_key_exists($service, $this->states)) {
  102. unset($this->states[$service]);
  103. }
  104. // allow chaining
  105. return $this;
  106. }
  107. /**
  108. * {@inheritDoc}
  109. */
  110. public function clearAllAuthorizationStates()
  111. {
  112. $this->states = array();
  113. // allow chaining
  114. return $this;
  115. }
  116. }