1 | <?php |
||
2 | /** |
||
3 | * Created by PhpStorm. |
||
4 | * User: thiago |
||
5 | * Date: 31/03/18 |
||
6 | * Time: 18:47 |
||
7 | */ |
||
8 | |||
9 | namespace MyWonderland\Service; |
||
10 | |||
11 | use GuzzleHttp\Client; |
||
12 | use MyWonderland\Domain\Model\Artist; |
||
13 | use MyWonderland\Domain\Model\SpotifyMe; |
||
14 | use MyWonderland\Domain\Model\SpotifyToken; |
||
15 | |||
16 | class SpotifyService |
||
17 | { |
||
18 | const REDIRECT_URI = '/callback'; |
||
19 | const BASE_AUTH_URI = 'https://accounts.spotify.com/authorize/'; |
||
20 | const TOKEN_URI = 'https://accounts.spotify.com/api/token'; |
||
21 | const BASE_API = 'https://api.spotify.com/v1'; |
||
22 | |||
23 | /** |
||
24 | * @var RequestService |
||
25 | */ |
||
26 | protected $requestService; |
||
27 | |||
28 | |||
29 | /** |
||
30 | * SpotifyService constructor. |
||
31 | * @param RequestService $requestService |
||
32 | */ |
||
33 | public function __construct(RequestService $requestService) |
||
34 | { |
||
35 | $this->requestService = $requestService; |
||
36 | } |
||
37 | |||
38 | |||
39 | /** |
||
40 | * @param $spotifyClientId |
||
41 | * @param $spotifyCallBackState |
||
42 | * @param $baseUri |
||
43 | * @return string |
||
44 | */ |
||
45 | public function getAuthUri($spotifyClientId, $spotifyCallBackState, $baseUri) |
||
46 | { |
||
47 | $scopes = 'user-read-private user-read-email user-top-read'; |
||
48 | $queryString = "?client_id=$spotifyClientId" . |
||
49 | '&response_type=code' . |
||
50 | '&show_dialog=true' . |
||
51 | '&redirect_uri=' . rawurlencode($baseUri . self::REDIRECT_URI) . |
||
52 | ($scopes ? '&scope=' . rawurlencode($scopes) : '') . |
||
53 | "&state=$spotifyCallBackState"; |
||
54 | return self::BASE_AUTH_URI . $queryString; |
||
55 | } |
||
56 | |||
57 | |||
58 | /** |
||
59 | * @param $spotifyClientId |
||
60 | * @param $spotifyClientSecret |
||
61 | * @param $code |
||
62 | * @param $baseUri |
||
63 | * @return SpotifyToken |
||
64 | */ |
||
65 | public function requestToken($spotifyClientId, $spotifyClientSecret, $code, $baseUri) |
||
66 | { |
||
67 | $client = new Client(); |
||
68 | $requestBody = [ |
||
69 | 'form_params' => [ |
||
70 | 'grant_type' => 'authorization_code', |
||
71 | 'code' => $code, |
||
72 | 'redirect_uri' => $baseUri . self::REDIRECT_URI |
||
73 | ], |
||
74 | 'headers' => [ |
||
75 | 'Authorization' => 'Basic ' . |
||
76 | base64_encode("$spotifyClientId:$spotifyClientSecret") |
||
77 | ] |
||
78 | ]; |
||
79 | $response = $client->request('POST', self::TOKEN_URI, $requestBody, \json_encode($requestBody)); |
||
0 ignored issues
–
show
|
|||
80 | |||
81 | // @todo use guzzle options |
||
82 | $responseBody = \json_decode($response->getBody()->getContents(), true); |
||
83 | return new SpotifyToken( |
||
84 | $responseBody['access_token'], |
||
85 | $responseBody['token_type'], |
||
86 | $responseBody['expires_in'], |
||
87 | $responseBody['refresh_token'], |
||
88 | $responseBody['scope'] |
||
89 | ); |
||
90 | } |
||
91 | |||
92 | |||
93 | /** |
||
94 | * @param SpotifyToken $token |
||
95 | * @return SpotifyMe |
||
96 | */ |
||
97 | public function requestMe(SpotifyToken $token) |
||
98 | { |
||
99 | $uri = 'https://api.spotify.com/v1/me'; |
||
100 | $response = $this->requestService->requestContent('GET', $uri, [ |
||
101 | 'headers' => [ |
||
102 | 'Authorization' => 'Bearer ' . $token->getAccessToken() |
||
103 | ] |
||
104 | ], $uri . $token->getAccessToken()); |
||
105 | return new SpotifyMe( |
||
106 | $response['country'], |
||
107 | $response['display_name'], |
||
108 | $response['images'][0]['url'] |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | public function requestTopArtists(SpotifyToken $token) |
||
113 | { |
||
114 | $uri = self::BASE_API . '/me/top/artists'; |
||
115 | $response = $this->requestService->requestContent('GET', $uri, [ |
||
116 | 'headers' => [ |
||
117 | 'Authorization' => 'Bearer ' . $token->getAccessToken() |
||
118 | ] |
||
119 | ], $uri . $token->getAccessToken()); |
||
120 | $topArtists = []; |
||
121 | foreach ($response['items'] as $artist) { |
||
122 | $topArtists[] = new Artist($artist['name'], $artist['images'][1]['url']); |
||
123 | } |
||
124 | return $topArtists; |
||
125 | } |
||
126 | } |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.