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\AuthService; |
15
|
|
|
use Core23\LastFmBundle\Session\SessionManager; |
16
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
19
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
20
|
|
|
use Symfony\Component\Routing\RouterInterface; |
21
|
|
|
|
22
|
|
|
final class CheckAuthAction |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var RouterInterface |
26
|
|
|
*/ |
27
|
|
|
private $router; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var SessionManager |
31
|
|
|
*/ |
32
|
|
|
private $sessionManager; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var AuthService |
36
|
|
|
*/ |
37
|
|
|
private $authService; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* CheckAuthAction constructor. |
41
|
|
|
* |
42
|
|
|
* @param RouterInterface $router |
43
|
|
|
* @param SessionManager $sessionManager |
44
|
|
|
* @param AuthService $authService |
45
|
|
|
*/ |
46
|
|
|
public function __construct(RouterInterface $router, SessionManager $sessionManager, AuthService $authService) |
47
|
|
|
{ |
48
|
|
|
$this->router = $router; |
49
|
|
|
$this->sessionManager = $sessionManager; |
50
|
|
|
$this->authService = $authService; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Request $request |
55
|
|
|
* |
56
|
|
|
* @return Response |
57
|
|
|
*/ |
58
|
|
|
public function __invoke(Request $request): Response |
59
|
|
|
{ |
60
|
|
|
$token = $request->query->get('token'); |
61
|
|
|
|
62
|
|
|
if (!$token) { |
63
|
|
|
return $this->redirectToRoute('core23_lastfm_auth'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Store session |
67
|
|
|
$lastFmSession = $this->authService->createSession($token); |
68
|
|
|
|
69
|
|
|
if (null === $lastFmSession) { |
70
|
|
|
return $this->redirectToRoute('core23_lastfm_error'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->sessionManager->store($lastFmSession); |
74
|
|
|
|
75
|
|
|
return $this->redirectToRoute('core23_lastfm_success'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns a RedirectResponse to the given route with the given parameters. |
80
|
|
|
* |
81
|
|
|
* @param string $route The name of the route |
82
|
|
|
* @param array $parameters An array of parameters |
83
|
|
|
* @param int $status The status code to use for the Response |
84
|
|
|
* |
85
|
|
|
* @return RedirectResponse |
86
|
|
|
*/ |
87
|
|
|
private function redirectToRoute($route, array $parameters = [], $status = 302): RedirectResponse |
88
|
|
|
{ |
89
|
|
|
return new RedirectResponse($this->generateUrl($route, $parameters), $status); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Generates a URL from the given parameters. |
94
|
|
|
* |
95
|
|
|
* @param string $route The name of the route |
96
|
|
|
* @param array $parameters An array of parameters |
97
|
|
|
* @param int $referenceType The type of reference (one of the constants in UrlGeneratorInterface) |
98
|
|
|
* |
99
|
|
|
* @return string The generated URL |
100
|
|
|
*/ |
101
|
|
|
private function generateUrl($route, array $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string |
102
|
|
|
{ |
103
|
|
|
return $this->router->generate($route, $parameters, $referenceType); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|