Completed
Push — master ( d601ba...2d4509 )
by Cesar
21s queued 15s
created

VaultService::getDataUserIdToken()   A

Complexity

Conditions 3
Paths 12

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 21
nc 12
nop 1
dl 0
loc 27
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
namespace App\Service\Paypal;
4
5
use Exception;
6
use PayPal\Api\OpenIdUserinfo;
7
8
/**
9
 * Class VaultService.php
10
 * @package App\Service\Paypal
11
 */
12
class VaultService extends IdentityService
13
{
14
    /**
15
     * @param string $clientId
16
     * @return string | null
17
     */
18
    public function getDataUserIdToken(string $clientId)
19
    {
20
        try {
21
            $ch = curl_init();
22
            curl_setopt($ch, CURLOPT_URL, 'https://api-m.sandbox.paypal.com/v1/oauth2/token');
23
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
24
            curl_setopt($ch, CURLOPT_POST, 1);
25
            curl_setopt(
26
                $ch,
27
                CURLOPT_POSTFIELDS,
28
                "grant_type=client_credentials&response_type=id_token&target_customer_id=" . $clientId
29
            );
30
            curl_setopt($ch, CURLOPT_USERPWD, $this->clientId . ':' . $this->clientSecret);
31
            $headers[] = 'Content-Type: application/x-www-form-urlencoded';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$headers was never initialized. Although not strictly required by PHP, it is generally a good practice to add $headers = array(); before regardless.
Loading history...
32
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
33
            $result = curl_exec($ch);
34
            curl_close($ch);
35
        } catch (Exception $e) {
36
            $this->logger->error('Error on PayPal::getDataUserIdToken = ' . $e->getMessage());
37
            return null;
38
        }
39
        $result = json_decode($result, true);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        $result = json_decode(/** @scrutinizer ignore-type */ $result, true);
Loading history...
40
41
        if (array_key_exists('id_token', $result)) {
42
            return $result['id_token'];
43
        }
44
        return null;
45
    }
46
}
47