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\Connection\Session as LastFmSession; |
15
|
|
|
use Core23\LastFm\Connection\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
|
|
|
/** |
30
|
|
|
* SessionManager constructor. |
31
|
|
|
* |
32
|
|
|
* @param Session $session |
33
|
|
|
*/ |
34
|
|
|
public function __construct(Session $session) |
35
|
|
|
{ |
36
|
|
|
$this->session = $session; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function isAuthenticated(): bool |
43
|
|
|
{ |
44
|
|
|
return (bool) $this->session->get(static::SESSION_LASTFM_TOKEN); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function getUsername(): ?string |
51
|
|
|
{ |
52
|
|
|
return $this->session->get(static::SESSION_LASTFM_NAME); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function store(SessionInterface $lastFmSession): void |
59
|
|
|
{ |
60
|
|
|
$this->session->set(static::SESSION_LASTFM_NAME, $lastFmSession->getName()); |
61
|
|
|
$this->session->set(static::SESSION_LASTFM_TOKEN, $lastFmSession->getKey()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function clear(): void |
68
|
|
|
{ |
69
|
|
|
$this->session->remove(static::SESSION_LASTFM_NAME); |
70
|
|
|
$this->session->remove(static::SESSION_LASTFM_TOKEN); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function getSession(): ?SessionInterface |
77
|
|
|
{ |
78
|
|
|
if (!$this->isAuthenticated()) { |
79
|
|
|
return null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return new LastFmSession( |
83
|
|
|
$this->session->get(static::SESSION_LASTFM_NAME), |
84
|
|
|
$this->session->get(static::SESSION_LASTFM_TOKEN) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|