Completed
Pull Request — master (#4)
by Cesar
01:51
created

IdentityService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 74
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAccessTokenFromAuthToken() 0 15 2
A refreshToken() 0 11 2
A getUserInfo() 0 11 2
A getUserInfoFromRefreshToken() 0 11 2
1
<?php
2
3
namespace App\Service\Paypal;
4
5
use Exception;
6
use PayPal\Api\OpenIdTokeninfo;
7
use PayPal\Api\OpenIdUserinfo;
8
9
/**
10
 * Class IdentityService
11
 * @package App\Service\Paypal
12
 */
13
class IdentityService extends AbstractPaypalService
14
{
15
    /**
16
     * getAccessTokenFromAuthToken
17
     *
18
     * @param $authToken
19
     * @return OpenIdTokeninfo|null
20
     */
21
    public function getAccessTokenFromAuthToken($authToken) : ?OpenIdTokeninfo
22
    {
23
        try {
24
            $accessToken = OpenIdTokeninfo::createFromAuthorizationCode(
25
                ['code' => $authToken],
26
                $this->clientId,
27
                $this->clientSecret,
28
                $this->apiContext
29
            );
30
        } catch (Exception $e) {
31
            $this->logger->error('Error on PayPal::getAccessTokenFromAuthToken = ' . $e->getMessage());
32
            return null;
33
        }
34
        return $accessToken;
35
    }
36
37
    /**
38
     * refreshToken
39
     *
40
     * @param string $refreshToken
41
     * @return OpenIdTokeninfo|null
42
     */
43
    public function refreshToken(string $refreshToken) : ?OpenIdTokeninfo
44
    {
45
        try {
46
            $tokenInfo = new OpenIdTokeninfo();
47
            $tokenInfo = $tokenInfo->createFromRefreshToken(['refresh_token' => $refreshToken], $this->apiContext);
0 ignored issues
show
Documentation introduced by
$this->apiContext is of type object<PayPal\Rest\ApiContext>, but the function expects a object<PayPal\Api\APIContext>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
        } catch (Exception $e) {
49
            $this->logger->error('Error on PayPal::refreshToken = ' . $e->getMessage());
50
            return null;
51
        }
52
        return $tokenInfo;
53
    }
54
55
    /**
56
     * @param OpenIdTokeninfo $tokenInfo
57
     * @return OpenIdUserinfo|null
58
     */
59
    public function getUserInfo(OpenIdTokeninfo $tokenInfo) : ?OpenIdUserinfo
60
    {
61
        try {
62
            $params = ['access_token' => $tokenInfo->getAccessToken()];
63
            $userInfo = OpenIdUserinfo::getUserinfo($params, $this->apiContext);
64
        } catch (Exception $e) {
65
            $this->logger->error('Error on PayPal::getUserInfo = ' . $e->getMessage());
66
            return null;
67
        }
68
        return $userInfo;
69
    }
70
71
    /**
72
     * @param string $refreshToken
73
     * @return OpenIdUserinfo|null
74
     */
75
    public function getUserInfoFromRefreshToken(string $refreshToken) : ?OpenIdUserinfo
76
    {
77
        try {
78
            $tokenInfo = $this->refreshToken($refreshToken);
79
            $userInfo = $this->getUserInfo($tokenInfo);
0 ignored issues
show
Bug introduced by
It seems like $tokenInfo defined by $this->refreshToken($refreshToken) on line 78 can be null; however, App\Service\Paypal\IdentityService::getUserInfo() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
80
        } catch (Exception $e) {
81
            $this->logger->error('Error on PayPal::getUserInfoFromRefreshToken = ' . $e->getMessage());
82
            return null;
83
        }
84
        return $userInfo;
85
    }
86
}
87