1 | <?php |
||
13 | abstract class Token implements TokenInterface |
||
14 | { |
||
15 | /** |
||
16 | * @var int |
||
17 | */ |
||
18 | protected $id; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $hash; |
||
24 | |||
25 | /** |
||
26 | * @var int |
||
27 | */ |
||
28 | protected $expireIn; |
||
29 | |||
30 | /** |
||
31 | * @var AccountInterface |
||
32 | */ |
||
33 | protected $account; |
||
34 | |||
35 | /** |
||
36 | * @var ApplicationInterface |
||
37 | */ |
||
38 | protected $application; |
||
39 | |||
40 | /** |
||
41 | * @see TokenInterface::__construct() |
||
42 | */ |
||
43 | public function __construct( |
||
44 | ApplicationInterface $application, |
||
45 | AccountInterface $account = null, |
||
46 | $expireIn = TokenInterface::DEFAULT_TTL, |
||
47 | $hash = null |
||
48 | ) { |
||
49 | $this->application = $application; |
||
50 | $this->account = $account; |
||
51 | $this->expireIn = $expireIn; |
||
52 | |||
53 | $this->hash = $hash ?: (new MessageDigestPasswordEncoder())->encodePassword( |
||
54 | sprintf('[%s\o/%s]', $application->getSecret(), $account->getPassword() ?: time()), |
||
|
|||
55 | uniqid(mt_rand(), true) |
||
56 | ); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @see TokenInterface::__toString() |
||
61 | */ |
||
62 | public function __toString() |
||
63 | { |
||
64 | return $this->hash; |
||
65 | } |
||
66 | |||
67 | public function getId() |
||
71 | |||
72 | /** |
||
73 | * @see TokenInterface::getHash() |
||
74 | */ |
||
75 | public function getHash() |
||
79 | |||
80 | /** |
||
81 | * @see TokenInterface::getExpireIn() |
||
82 | */ |
||
83 | public function getExpireIn() |
||
87 | |||
88 | /** |
||
89 | * @see TokenInterface::getAccount() |
||
90 | */ |
||
91 | public function getAccount() |
||
95 | |||
96 | /** |
||
97 | * @see TokenInterface::getApplication() |
||
98 | */ |
||
99 | public function getApplication() |
||
103 | |||
104 | /** |
||
105 | * @see TokenInterface::getRoles() |
||
106 | */ |
||
107 | public function getRoles() |
||
111 | } |
||
112 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: