Passed
Push — master ( 4164b7...fac48b )
by Stavros
23:01 queued 07:51
created

AccessToken::getWebhookValidationToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 9.9332
1
<?php
2
3
namespace Stadem\VivaPayments\Services;
4
use Stadem\VivaPayments\Config\Config;
5
use Stadem\VivaPayments\Services\CurlWrapper;
6
7
class AccessToken
8
{
9
10
    private $accessToken;
11
    private $environment;
12
    private $statusCode;
13
14
    public function __construct(string $environment = null){
15
16
        if($environment===null){
17
            $config = new Config();
18
            $environment = $config->getEnvConfig('defaultProvider'); 
19
        }
20
21
        if ($environment !== 'vivaDEMO' && $environment !== 'vivaPROD') {
22
            throw new \InvalidArgumentException('Invalid environment value {'.$environment.'}. It should be either "vivaDEMO" or "vivaPROD".');
23
        }
24
        $this->environment = $environment;   
25
26
    }
27
28
    public function getAccessToken(): string
29
    {
30
        $config = new Config($this->environment);
31
        $url = $config->getEnvConfig('VIVA_ACCOUNT_URL');
32
        $curl = new CurlWrapper($url.'/connect/token');
33
        $curl->addHeader('Content-Type: application/x-www-form-urlencoded');
34
        $curl->addHeader('User-Agent: PHPGatewayRuntime/0.0.1');
35
        $curl->addHeader('Accept: */*');
36
        $curl->setBasicAuth($config->getEnvConfig('VIVA_CLIENT_ID'),$config->getEnvConfig('VIVA_CLIENT_SECRET'));
37
        $response = $curl->post(array('grant_type' => 'client_credentials'));   
38
        $this->statusCode = $curl->getStatusCode();
39
        $response = json_decode($response,true);
0 ignored issues
show
Bug introduced by
It seems like $response 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
        $response = json_decode(/** @scrutinizer ignore-type */ $response,true);
Loading history...
40
41
        $this->accessToken = $response['access_token'];
42
        return $this->accessToken;
43
    }
44
45
    public function getWebhookValidationToken() : array
46
    {
47
        $config = new Config($this->environment);
48
        $url = $config->getEnvConfig('VIVA_URL');
49
        $curl = new CurlWrapper($url.'/api/messages/config/token');
50
        $curl->addHeader('Content-Type: application/x-www-form-urlencoded');
51
        $curl->addHeader('User-Agent: PHPGatewayRuntime/0.0.1');
52
        $curl->addHeader('Accept: */*');
53
        $curl->setBasicAuth($config->getEnvConfig('VIVA_MERCHANT_ID'),$config->getEnvConfig('VIVA_API_KEY'));
54
55
        $response = $curl->get();   
56
57
        $response = json_decode($response,true);
0 ignored issues
show
Bug introduced by
It seems like $response 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

57
        $response = json_decode(/** @scrutinizer ignore-type */ $response,true);
Loading history...
58
        return $response;
59
    }
60
61
62
63
    public function getEnvironment(): string {
64
        return $this->environment;
65
    }
66
67
    
68
    public function getToken(): string {
69
        return $this->accessToken;
70
    }
71
72
73
    public function getStatusCode():int {
74
        return $this->statusCode;
75
    }
76
77
    public function refresh(): self
78
    {
79
        return $this;
80
    }
81
82
83
}
84