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\Action; |
13
|
|
|
|
14
|
|
|
use Core23\LastFm\Service\AuthServiceInterface; |
15
|
|
|
use Core23\LastFmBundle\Session\SessionManagerInterface; |
16
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
19
|
|
|
use Symfony\Component\Routing\RouterInterface; |
20
|
|
|
|
21
|
|
|
final class CheckAuthAction |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var RouterInterface |
25
|
|
|
*/ |
26
|
|
|
private $router; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var SessionManagerInterface |
30
|
|
|
*/ |
31
|
|
|
private $sessionManager; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var AuthServiceInterface |
35
|
|
|
*/ |
36
|
|
|
private $authService; |
37
|
|
|
|
38
|
|
|
public function __construct(RouterInterface $router, SessionManagerInterface $sessionManager, AuthServiceInterface $authService) |
39
|
|
|
{ |
40
|
|
|
$this->router = $router; |
41
|
|
|
$this->sessionManager = $sessionManager; |
42
|
|
|
$this->authService = $authService; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function __invoke(Request $request): RedirectResponse |
46
|
|
|
{ |
47
|
|
|
$token = (string) $request->query->get('token', ''); |
48
|
|
|
|
49
|
|
|
if ('' === $token) { |
50
|
|
|
return new RedirectResponse($this->generateUrl('core23_lastfm_auth')); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Store session |
54
|
|
|
$lastFmSession = $this->authService->createSession($token); |
55
|
|
|
|
56
|
|
|
if (null === $lastFmSession) { |
57
|
|
|
return new RedirectResponse($this->generateUrl('core23_lastfm_error')); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->sessionManager->store($lastFmSession); |
61
|
|
|
|
62
|
|
|
return new RedirectResponse($this->generateUrl('core23_lastfm_success')); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Generates a URL from the given parameters. |
67
|
|
|
* |
68
|
|
|
* @param string $route The name of the route |
69
|
|
|
* @param array $parameters An array of parameters |
70
|
|
|
* @param int $referenceType The type of reference (one of the constants in UrlGeneratorInterface) |
71
|
|
|
* |
72
|
|
|
* @return string The generated URL |
73
|
|
|
*/ |
74
|
|
|
private function generateUrl(string $route, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string |
75
|
|
|
{ |
76
|
|
|
return $this->router->generate($route, $parameters, $referenceType); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|