|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* (c) Christian Gripp <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Core23\LastFmBundle\Session; |
|
13
|
|
|
|
|
14
|
|
|
use Core23\LastFm\Session\Session as LastFmSession; |
|
15
|
|
|
use Core23\LastFm\Session\SessionInterface; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
17
|
|
|
|
|
18
|
|
|
final class SessionManager implements SessionManagerInterface |
|
19
|
|
|
{ |
|
20
|
|
|
private const SESSION_LASTFM_NAME = '_CORE23_LASTFM_NAME'; |
|
21
|
|
|
|
|
22
|
|
|
private const SESSION_LASTFM_TOKEN = '_CORE23_LASTFM_TOKEN'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Session |
|
26
|
|
|
*/ |
|
27
|
|
|
private $session; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(Session $session) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->session = $session; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function isAuthenticated(): bool |
|
35
|
|
|
{ |
|
36
|
|
|
return (bool) $this->session->get(static::SESSION_LASTFM_TOKEN); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getUsername(): ?string |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->session->get(static::SESSION_LASTFM_NAME); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function store(SessionInterface $lastFmSession): void |
|
45
|
|
|
{ |
|
46
|
|
|
$this->session->set(static::SESSION_LASTFM_NAME, $lastFmSession->getName()); |
|
47
|
|
|
$this->session->set(static::SESSION_LASTFM_TOKEN, $lastFmSession->getKey()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function clear(): void |
|
51
|
|
|
{ |
|
52
|
|
|
$this->session->remove(static::SESSION_LASTFM_NAME); |
|
53
|
|
|
$this->session->remove(static::SESSION_LASTFM_TOKEN); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getSession(): ?SessionInterface |
|
57
|
|
|
{ |
|
58
|
|
|
if (!$this->isAuthenticated()) { |
|
59
|
|
|
return null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return new LastFmSession( |
|
63
|
|
|
$this->session->get(static::SESSION_LASTFM_NAME), |
|
64
|
|
|
$this->session->get(static::SESSION_LASTFM_TOKEN) |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|