Facebook::getQuerystringIdentifier()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Svycka\SocialUser\OAuth2\GrantType;
4
5
use Facebook\Exceptions\FacebookSDKException;
6
use Facebook\Facebook as FacebookSDK;
7
use Svycka\SocialUser\Service\SocialUserService;
8
use Svycka\SocialUser\UserProfile;
9
use Svycka\SocialUser\UserProfileInterface;
10
11
/**
12
 * @author Vytautas Stankus <[email protected]>
13
 * @license MIT
14
 */
15
class Facebook extends AbstractSocialGrantType
16
{
17
    const PROVIDER_NAME = 'facebook';
18
19
    /**
20
     * @var SocialUserService
21
     */
22
    protected $socialUserService;
23
24
    /**
25
     * @var \Facebook\Facebook
26
     */
27
    protected $facebook;
28
29 7
    public function __construct(SocialUserService $socialUserService, FacebookSDK $facebook)
30
    {
31 7
        $this->socialUserService = $socialUserService;
32 7
        $this->facebook = $facebook;
33 7
    }
34
35 1
    public function getQuerystringIdentifier()
36
    {
37 1
        return 'facebook';
38
    }
39
40
    /**
41
     * @param string $token
42
     *
43
     * @return UserProfileInterface|null
44
     */
45 5
    protected function getTokenInfo($token)
46
    {
47
        try {
48
            // Get the Facebook\GraphNodes\GraphUser object for the current user.
49 5
            $response = $this->facebook->get('/me?fields=id,name,email,first_name,last_name', $token);
50 4
            $user = $response->getGraphUser();
51
52
            // check if we can get user identifier
53 4
            if (empty($user->getId())) {
54 1
                return null;
55
            }
56
57
            // do not accept tokens generated not for our application even if they are valid,
58
            // to protect against "man in the middle" attack
59 3
            $tokenMetadata = $this->facebook->getOAuth2Client()->debugToken($token);
60
            // this is not required, but lets be sure because facebook API changes very often
61 3
            $tokenMetadata->validateAppId($this->facebook->getApp()->getId());
62
63 2
            $userProfile = new UserProfile();
64 2
            $userProfile->setIdentifier($user->getId());
65 2
            $userProfile->setDisplayName($user->getName());
66 2
            $userProfile->setFirstName($user->getFirstName());
67 2
            $userProfile->setLastName($user->getLastName());
68 2
            $userProfile->setEmail($user->getEmail());
69
            // facebook doesn't allow login with not verified email
70 2
            if (!empty($user->getEmail())) {
71 2
                $userProfile->setEmailVerified(true);
72
            }
73
74 2
            return $userProfile;
75 2
        } catch (FacebookSDKException $e) {
76 2
            return null;
77
        }
78
    }
79
80
    /**
81
     * @param UserProfileInterface $socialUser
82
     *
83
     * @return int|null
84
     */
85 2
    protected function getLocalUser(UserProfileInterface $socialUser)
86
    {
87 2
        return $this->socialUserService->getLocalUser(self::PROVIDER_NAME, $socialUser);
88
    }
89
}
90