|
| 1 | +<?php |
| 2 | +/*! |
| 3 | +* Hybridauth |
| 4 | +* https://hybridauth.github.io | https://github.com/hybridauth/hybridauth |
| 5 | +* (c) 2017 Hybridauth authors | https://hybridauth.github.io/license.html |
| 6 | +*/ |
| 7 | + |
| 8 | +namespace Hybridauth\Provider; |
| 9 | + |
| 10 | +use Hybridauth\Adapter\OAuth2; |
| 11 | +use Hybridauth\Exception\UnexpectedApiResponseException; |
| 12 | +use Hybridauth\Data; |
| 13 | +use Hybridauth\User; |
| 14 | + |
| 15 | +/** |
| 16 | + * Spotify OAuth2 provider adapter. |
| 17 | + */ |
| 18 | +class Spotify extends OAuth2 |
| 19 | +{ |
| 20 | + |
| 21 | + /** |
| 22 | + * {@inheritdoc} |
| 23 | + */ |
| 24 | + public $scope = 'user-read-email'; |
| 25 | + |
| 26 | + /** |
| 27 | + * {@inheritdoc} |
| 28 | + */ |
| 29 | + public $apiBaseUrl = 'https://api.spotify.com/v1/'; |
| 30 | + |
| 31 | + /** |
| 32 | + * {@inheritdoc} |
| 33 | + */ |
| 34 | + public $authorizeUrl = 'https://accounts.spotify.com/authorize'; |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritdoc} |
| 38 | + */ |
| 39 | + protected $accessTokenUrl = 'https://accounts.spotify.com/api/token'; |
| 40 | + |
| 41 | + /** |
| 42 | + * {@inheritdoc} |
| 43 | + */ |
| 44 | + public function getUserProfile() |
| 45 | + { |
| 46 | + $response = $this->apiRequest('me'); |
| 47 | + |
| 48 | + $data = new Data\Collection($response); |
| 49 | + |
| 50 | + if (!$data->exists('id')) { |
| 51 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
| 52 | + } |
| 53 | + |
| 54 | + $userProfile = new User\Profile(); |
| 55 | + |
| 56 | + $userProfile->identifier = $data->get('id'); |
| 57 | + $userProfile->displayName = $data->get('display_name'); |
| 58 | + $userProfile->email = $data->get('email'); |
| 59 | + $userProfile->emailVerified = $data->get('email'); |
| 60 | + $userProfile->profileURL = $data->filter('external_urls')->get('spotify'); |
| 61 | + $userProfile->photoURL = $data->filter('images')->get('url'); |
| 62 | + $userProfile->country = $data->get('country'); |
| 63 | + |
| 64 | + if ($data->exists('birthdate')) { |
| 65 | + $this->fetchBirthday($userProfile, $data->get('birthdate')); |
| 66 | + } |
| 67 | + |
| 68 | + return $userProfile; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Fetch use birthday |
| 73 | + */ |
| 74 | + protected function fetchBirthday($userProfile, $birthday) |
| 75 | + { |
| 76 | + $result = (new Data\Parser())->parseBirthday($birthday, '-'); |
| 77 | + |
| 78 | + $userProfile->birthDay = (int)$result[0]; |
| 79 | + $userProfile->birthMonth = (int)$result[1]; |
| 80 | + $userProfile->birthYear = (int)$result[2]; |
| 81 | + |
| 82 | + return $userProfile; |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments