AbstractSocialGrantType::createAccessToken()   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 4
crap 1
1
<?php
2
3
namespace Svycka\SocialUser\OAuth2\GrantType;
4
5
use OAuth2\GrantType\GrantTypeInterface;
6
use OAuth2\RequestInterface;
7
use OAuth2\ResponseInterface;
8
use OAuth2\ResponseType\AccessTokenInterface;
9
use Svycka\SocialUser\UserProfileInterface;
10
11
/**
12
 * @author Vytautas Stankus <[email protected]>
13
 * @license MIT
14
 */
15
abstract class AbstractSocialGrantType implements GrantTypeInterface
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $userInfo;
21
22 13
    public function validateRequest(RequestInterface $request, ResponseInterface $response)
23
    {
24 13
        $token = $request->request("token");
25
26 13
        if (!$token) {
27 2
            $response->setError(400, 'invalid_request', 'Missing parameter: "token" is required');
28 2
            return null;
29
        }
30
31 11
        $socialUser = $this->getTokenInfo($token);
32
33 11
        if (!$socialUser) {
34 6
            $response->setError(401, 'invalid_grant', 'Invalid or expired token');
35 6
            return null;
36
        }
37
38 5
        $user_id = $this->getLocalUser($socialUser);
39
40 5
        if (!$user_id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $user_id of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
41 2
            $response->setError(401, 'invalid_grant', 'Unable to identify or create user');
42 2
            return null;
43
        }
44
45 3
        $this->userInfo = [
46 3
            'user_id' => $user_id
47
        ];
48
49 3
        return true;
50
    }
51
52 3
    public function getClientId()
53
    {
54 3
        return null;
55
    }
56
57 3
    public function getUserId()
58
    {
59 3
        return $this->userInfo['user_id'];
60
    }
61
62 3
    public function getScope()
63
    {
64 3
        return isset($this->userInfo['scope']) ? $this->userInfo['scope'] : null;
65
    }
66
67 3
    public function createAccessToken(AccessTokenInterface $accessToken, $client_id, $user_id, $scope)
68
    {
69 3
        return $accessToken->createAccessToken($client_id, $user_id, $scope);
70
    }
71
72
    /**
73
     * @param string $token
74
     *
75
     * @return UserProfileInterface|null
76
     */
77
    abstract protected function getTokenInfo($token);
78
79
    /**
80
     * @param UserProfileInterface $socialUser
81
     *
82
     * @return int|null
83
     */
84
    abstract protected function getLocalUser(UserProfileInterface $socialUser);
85
}
86