1: <?php
2: namespace Genetsis\core;
3:
4: /**
5: * Abstract class which aims to be the parent class of the different types
6: * of tokens.
7: *
8: * @package Genetsis
9: * @category Bean
10: * @version 1.0
11: * @access public
12: * @since 2011-09-08
13: */
14: abstract class StoredToken
15: {
16: /** @var string The token name. */
17: protected $name = null;
18: /** @var string The token value. */
19: protected $value = null;
20: /** @var integer integer Number the seconds until the token expires. */
21: protected $expires_in = null;
22: /** @var integer Date when the token expires. As UNIX timestamp. */
23: protected $expires_at = null;
24: /** @var string Full path to the folder where cookies will be saved. */
25: protected $path = '/';
26:
27: /**
28: * @param string The token value.
29: * @param integer Number the seconds until the token expires.
30: * @param integer Date when the token expires. As UNIX timestamp.
31: * @param string Full path to the folder where cookies will be saved.
32: * Only if necessary.
33: */
34: public function __construct($value, $expires_in = 0, $expires_at = 0, $path = '/')
35: {
36: $this->setValue($value);
37: $this->setExpiresIn($expires_in);
38: $this->setExpiresAt($expires_at);
39: $this->setPath($path);
40:
41: $this->setName();
42: }
43:
44: /**
45: * Create an instance of an access token based on the name.
46: *
47: * @param string $name The token name.
48: * @param sting $value The token value.
49: * @param $expires_in Number the seconds until the token expires.
50: * @param $expires_at Date when the token expires. As UNIX timestamp.
51: * @param $path Full path to the folder where cookies will be saved.
52: * @return bool|AccessToken|ClientToken|RefreshToken An object of type {@link StoredToken} or FALSE if
53: * unable to create it.
54: */
55: public static function factory($name, $value, $expires_in, $expires_at, $path)
56: {
57: switch (trim((string)$name)) {
58: case iTokenTypes::ACCESS_TOKEN:
59: return new AccessToken ($value, $expires_in, $expires_at, $path);
60: case iTokenTypes::CLIENT_TOKEN:
61: return new ClientToken ($value, $expires_in, $expires_at, $path);
62: case iTokenTypes::REFRESH_TOKEN:
63: return new RefreshToken($value, $expires_in, $expires_at, $path);
64: }
65: return false;
66: }
67:
68: /**
69: * Returns the token name.
70: *
71: * We use it for serialization the token content.
72: *
73: * @return string The token name.
74: * @see iTokenTypes
75: */
76: public function getName()
77: {
78: return ((!isset($this->name) || ($this->name === null))
79: ? ''
80: : $this->name);
81: }
82:
83: /**
84: * Sets token name.
85: *
86: * We delegate this method to child classes.
87: *
88: * @return void
89: * @see iTokenTypes
90: */
91: abstract protected function setName();
92:
93: /**
94: * Returns the token value.
95: *
96: * @return string The token value. It could be empty.
97: */
98: public function getValue()
99: {
100: return ((!isset($this->value) || ($this->value === null))
101: ? ''
102: : $this->value);
103: }
104:
105: /**
106: * Sets token value.
107: *
108: * @param string Token value.
109: * @return void
110: */
111: public function setValue($value)
112: {
113: $this->value = trim((string)$value);
114: }
115:
116: /**
117: * Returns the number of seconds when token expires.
118: *
119: * @return integer The number of seconds.
120: */
121: public function getExpiresIn()
122: {
123: return ((!isset($this->expires_in) || ($this->expires_in === null))
124: ? 0
125: : $this->expires_in);
126: }
127:
128: /**
129: * Sets the number of seconds when token expires.
130: *
131: * @param integer The number of seconds it takes to die.
132: * @return void
133: */
134: public function setExpiresIn($expires_in)
135: {
136: $this->expires_in = (is_integer($expires_in)
137: ? $expires_in
138: : (int)$expires_in);
139: if ($this->expires_in < 0) {
140: $this->expires_in = 0;
141: }
142: }
143:
144: /**
145: * Returns the date when the "token" should be dead.
146: *
147: * @return integer UNIX timestamp with the date. Zero if not defined.
148: */
149: public function getExpiresAt()
150: {
151: return ((!isset($this->expires_at) || ($this->expires_at === null))
152: ? 0
153: : $this->expires_at);
154: }
155:
156: /**
157: * Sets the date when the "token" should be dead.
158: *
159: * @param integer UNIX timestamp.
160: * @return void
161: */
162: public function setExpiresAt($expires_at)
163: {
164: $this->expires_at = (is_integer($expires_at)
165: ? $expires_at
166: : (int)$expires_at);
167: if ($this->expires_at < 0) {
168: $this->expires_at = 0;
169: }
170: }
171:
172: /**
173: * Returns the path to cookie folder.
174: *
175: * @return string The full path to the folder where the cookies will be
176: * saved.
177: */
178: public function getPath()
179: {
180: return $this->path;
181: }
182:
183: /**
184: * Sets the path where the cookies will be saved.
185: *
186: * @param string Full path to the folder.
187: * @return void
188: * @todo Checks if path exists and is writable.
189: */
190: public function setPath($path)
191: {
192: $this->path = trim((string)$path);
193: }
194: }