<?php
namespace common\modules\eauth\components;

class VkOAuth2Service extends \nodge\eauth\services\VKontakteOAuth2Service
{
    use ServiceTrait;

    protected $name = 'vk';
    
    /*
     * Scopes MUST be declarated for use
     */
    const SCOPE_EMAIL = 'email';

    protected function fetchAttributes()
	{
		$tokenData = $this->getAccessTokenData();

		$info = $this->makeSignedRequest('users.get.json', [
			'query' => [
				'uids' => $tokenData['params']['user_id'],
				'fields' => 'uid, name, nickname, first_name, last_name, email', // uid, first_name and last_name is always available
			],
		]);
		$info = $info['response'][0];

        $profile = [
            'id' => $tokenData['params']['user_id'],
            'email' => $tokenData['params']['email'],
            'name' => $info['first_name'].' '.$info['last_name'],
            'locale' => 'ru',
        ];

		$this->attributes['id'] = $info['uid'];
		$this->attributes['name'] = $info['first_name'] . ' ' . $info['last_name'];
		$this->attributes['url'] = 'http://vk.com/id' . $info['uid'];
        $this->attributes['profile'] = $profile;

        $this->setSocialProfile($profile);
        
        return true;
	}
}
