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