|
1
|
|
|
<?php |
|
2
|
|
|
namespace MyWonderland\Controller; |
|
3
|
|
|
use MyWonderland\Domain\Manager\StorageManager; |
|
4
|
|
|
use MyWonderland\Service\SpotifyService; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Created by PhpStorm. |
|
8
|
|
|
* User: thiago |
|
9
|
|
|
* Date: 31/03/18 |
|
10
|
|
|
* Time: 12:00 |
|
11
|
|
|
*/ |
|
12
|
|
|
class SpotifyAuthController extends AbstractController |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var SpotifyService |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $spotifyService; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* SpotifyAuthController constructor. |
|
21
|
|
|
* @param StorageManager $storageManager |
|
22
|
|
|
* @param \Twig_Environment $twig |
|
23
|
|
|
* @param SpotifyService $spotifyService |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(StorageManager $storageManager, \Twig_Environment $twig, SpotifyService $spotifyService) |
|
26
|
|
|
{ |
|
27
|
|
|
parent::__construct($storageManager, $twig); |
|
28
|
|
|
$this->spotifyService = $spotifyService; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
public function auth() |
|
33
|
|
|
{ |
|
34
|
|
|
header('Location: ' . $this->spotifyService->getAuthUri( |
|
35
|
|
|
getenv('SPOTIFY_CLIENT_ID'), |
|
36
|
|
|
getenv('SPOTIFY_CALLBACK_STATE'), |
|
37
|
|
|
getenv('BASE_URI') |
|
38
|
|
|
)); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
public function callback($state, $code = null, $error = null) |
|
43
|
|
|
{ |
|
44
|
|
|
// @todo check state against SPOTIFY_CALLBACK_STATE using a middleware |
|
45
|
|
|
|
|
46
|
|
|
if ($error !== null) { |
|
47
|
|
|
header('Location: /'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$this->storeManager->set('token', $this->spotifyService->requestToken( |
|
51
|
|
|
getenv('SPOTIFY_CLIENT_ID'), |
|
52
|
|
|
getenv('SPOTIFY_CLIENT_SECRET'), |
|
53
|
|
|
$code, |
|
54
|
|
|
getenv('BASE_URI') |
|
55
|
|
|
)); |
|
56
|
|
|
header('Location: /report'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
public function logout() { |
|
61
|
|
|
$this->storeManager->unset('token'); |
|
62
|
|
|
header('Location: /'); |
|
63
|
|
|
} |
|
64
|
|
|
} |