Passed
Push — master ( 6466f2...326f15 )
by Antônio
01:59
created

ObjetoBoletosRegistrados::autenticarPorTipo()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace OBRSDK;
10
11
/**
12
 * Description of ObjetoBoletosRegistrados
13
 *
14
 * @author Antonio
15
 */
16
class ObjetoBoletosRegistrados {
17
18
    private $appId;
19
    private $appSecret;
20
    private $accessToken = null;
21
    private $autenticacao;
22
23
    /**
24
     *
25
     * @var IAccessTokenCallback
26
     */
27
    private $accessTokenUpdateCallback;
28
29
    public function __construct(array $config, \OBRSDK\Entidades\AccessToken $objAccessToken = null) {
30
        if (!isset($config['appId']) || !isset($config['appSecret'])) {
31
            throw new Exceptions\ConfiguracaoInvalida("É necessário passar um 'appId' e um 'appSecret' nas configurações inicial");
32
        }
33
34
        $this->appId = $config['appId'];
35
        $this->appSecret = $config['appSecret'];
36
        if ($objAccessToken != null) {
37
            $this->accessToken = $objAccessToken;
38
        }
39
        $this->autenticacao = new Autenticacao($this);
40
    }
41
42
    public function getAppId() {
43
        return $this->appId;
44
    }
45
46
    public function getAppSecret() {
47
        return $this->appSecret;
48
    }
49
50
    /**
51
     * Pega o objeto de realizar autenticacao
52
     * 
53
     * @return \OBRSDK\Autenticacao
54
     */
55
    public function autenticar() {
56
        return $this->autenticacao;
57
    }
58
59
    /**
60
     * Pega o objeto de autenticacao AccessToken que contem 
61
     * informações do token de autenticacao atual como data que expire
62
     * refresh tokens, escopos utilizado e o proprio access_token
63
     * 
64
     * @return \OBRSDK\Entidades\AccessToken
65
     */
66
    public function getObjAccessToken() {
67
        return $this->accessToken;
68
    }
69
70
    /**
71
     * Seta o objeto AccessToken
72
     * @param \OBRSDK\Entidades\AccessToken $autenticacao
73
     */
74
    public function setObjAccessToken(Entidades\AccessToken $autenticacao) {
75
        $this->accessToken = $autenticacao;
76
        if ($this->accessTokenUpdateCallback != null) {
77
            $this->accessTokenUpdateCallback->novoAccessToken($this->accessToken);
78
        }
79
    }
80
81
    /**
82
     * Seta um objeto do tipo IAccessTokenCallback para receber sempre uma chamada 
83
     *  quando o token de acesso for atualizado pelo SDK
84
     * 
85
     * O Objeto do token atualizado é recebido na funcao 'novoAccessToken'
86
     * 
87
     * @param \OBRSDK\IAccessTokenCallback $accessTokenCallback
88
     */
89
    public function setAccessTokenUpdateCallback(IAccessTokenCallback $accessTokenCallback) {
90
        $this->accessTokenUpdateCallback = $accessTokenCallback;
91
    }
92
93
    /**
94
     * Verifica se o access token esta expirado, se tiver tenta pegar um novo 
95
     * access token via refresh token se o mesmo existir, se não existir refresh 
96
     * token, tenta autenticar por credenciais
97
     */
98
    public function verificarAccessToken() {
99
        // se nao existe accesstoken
100
        if ($this->accessToken == null) {
101
            // lança excessao por tentar fazer autenticacao sem accesstoken
102
            throw new Exceptions\PreenchimentoIncorreto("Não existe objeto AccessToken registrado no SDK");
103
        }
104
105
        // se o token estiver expirando
106
        if ($this->accessToken->getDataToken() + $this->accessToken->getExpireIn() - 10 <= time()) {
107
            // verifica o tipo disponivel para se autenticar
108
            $this->autenticarPorTipo();
109
        }
110
    }
111
112
    private function autenticarPorTipo() {
113
        // é feito a tentativa de criar um novo accessToken
114
        // se existe refreshtoken tenta gerar o novo access token atraves do refreshtoken
115
        if ($this->accessToken->getRefreshToken() != null && strlen($this->accessToken->getRefreshToken()) > 0) {
116
            $this->autenticar()->porRefreshToken($this->accessToken->getRefreshToken());
117
        } else {
118
            // se nao tem refresh token entao gera por credenciais
119
            $this->autenticar()->porCredenciais();
120
        }
121
    }
122
123
}
124