1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
<?php
namespace Riimu\Kit\CSRF;
/**
* Provides convenience and efficiency by lazy loading only one token.
* @author Riikka Kalliomäki <riikka.kalliomaki@gmail.com>
* @copyright Copyright (c) 2015, Riikka Kalliomäki
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class SingleToken
{
/** @var CSRFHandler The handler used to generate the token */
private $csrf;
/** @var string The generated token */
private $token;
/**
* Creates a new instance of SingleToken.
* @param CSRFHandler $csrf The handler used to generate the token
*/
public function __construct(CSRFHandler $csrf)
{
$this->csrf = $csrf;
}
/**
* Returns the generated token.
*
* The token is generated the first time this method is called. Further
* calls to this method will always return the same token.
*
* @return string The generated token
*/
public function getToken()
{
if (!isset($this->token)) {
$this->token = $this->csrf->getToken();
}
return $this->token;
}
/**
* Returns the generated token.
* @return string The generated token
*/
public function __toString()
{
return $this->getToken();
}
}