GooglePlusProvider::getUser()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 3
Bugs 0 Features 3
Metric Value
dl 0
loc 14
ccs 0
cts 12
cp 0
rs 8.8571
c 3
b 0
f 3
cc 5
eloc 10
nc 16
nop 1
crap 30
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