GooglePlusProvider::getUserInformation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 13
loc 13
ccs 0
cts 10
cp 0
rs 9.4286
c 1
b 0
f 1
cc 1
eloc 7
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Sleepness\UberOAuthRestBundle\Provider;
4
5
use Sleepness\UberOAuthRestBundle\Provider\BaseOAuthProvider as BaseProvider;
6
use Sleepness\UberOAuthRestBundle\Model\User as UserDto;
7
8
class GooglePlusProvider extends BaseProvider
9
{
10
    const ACCESS_TOKEN_URL = 'https://www.googleapis.com/oauth2/v3/token';
11
    const INFOS_URL = 'https://www.googleapis.com/oauth2/v3/userinfo';
12
    const PROVIDER_NAME = 'gp';
13
    const SCOPE = 'https://www.googleapis.com/auth/userinfo.email';
14
15
    /**
16
     * @param $accessToken
17
     *
18
     * @return mixed|\Psr\Http\Message\ResponseInterface
19
     */
20 View Code Duplication
    public function getUserInformation($accessToken)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
    {
22
        $parameters = array(
23
            'access_token' => $accessToken->access_token,
24
            'scope' => self::SCOPE,
25
        );
26
27
        $url = $this->normalizeUrl(self::INFOS_URL, $parameters);
28
29
        $response = $this->doRequest($url, self::GET);
30
31
        return $response;
32
    }
33
34
    /**
35
     * @param $code
36
     *
37
     * @return mixed|\Psr\Http\Message\ResponseInterface
38
     */
39
    public function getAccessToken($code)
40
    {
41
        $parameters = [
42
            'code' => $code,
43
            'client_id' => $this->credentials['client_id'],
44
            'client_secret' => $this->credentials['client_secret'],
45
            'redirect_uri' => $this->credentials['redirect_uri'],
46
            'grant_type' => 'authorization_code',
47
        ];
48
49
        $response = $this->doRequest(
50
            self::ACCESS_TOKEN_URL,
51
            self::POST,
52
            [
53
                'body' => http_build_query($parameters, '', '&'),
54
                'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
55
            ]
56
        );
57
58
        return $response;
59
    }
60
61
    /**
62
     * @param $code
63
     *
64
     * @return UserDto
65
     */
66
    public function getUser($code)
67
    {
68
        $accessToken = $this->getAccessToken($code);
69
        $userInformation = $this->getUserInformation($accessToken);
70
71
        $user = new UserDto();
72
        $user->socialId = $userInformation->sub;
73
        $user->nickName = $userInformation->name ?: null;
74
        $user->email = $userInformation->email ?: null;
75
        $user->firstName = $userInformation->given_name ?: null;
76
        $user->lastName = $userInformation->family_name ?: null;
77
78
        return $user;
79
    }
80
}
81