Oauth2TokenService::refreshToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
ccs 0
cts 6
cp 0
crap 2
1
<?php
2
3
/**
4
 * This service provides methods that help to manage API Rest communications.
5
 */
6
7
namespace Cdf\BiCoreBundle\Service\Api;
8
9
use Cdf\BiCoreBundle\Utils\Api\RestClient;
10
11
/**
12
 * It provides services to manage token with WSO2 API services.
13
 */
14
class Oauth2TokenService
15
{
16
17
    private string $access_token;
18
    private string $endpoint;
19
    private string $clientkey;
20
    private string $expires_in;
21
    private string $token_type;
22
23
24
    /**
25
     * Build an EcmTokenService instance
26
     */
27
    public function __construct(string $endpoint, string $clientkey)
28
    {
29
        $this->access_token = '';
30
        $this->endpoint = $endpoint;
31
        $this->clientkey = $clientkey;
32
    }
33
34
    /**
35
     * Return the ticket if any, otherwise compute it
36
     */
37
    public function getToken(): string
38
    {
39
        if ($this->access_token == '') {
40
            $this->refreshToken();
41
        }
42
        return $this->access_token;
43
    }
44
45
    public function getExpiresIn(): string
46
    {
47
        return $this->expires_in;
48
    }
49
50
    public function getTokenType(): string
51
    {
52
        return $this->token_type;
53
    }
54
55
    /**
56
     * Refresh the service ticket
57
     */
58
    public function refreshToken(): void
59
    {
60
        $rest = new RestClient($this->endpoint, $this->clientkey);
61
        $results = $rest->oauth2Principal();
62
        $this->access_token = $results->access_token;
63
        $this->expires_in = $results->expires_in;
64
        $this->token_type = $results->token_type;
65
    }
66
}
67