Passed
Push — main ( bae415...a3d595 )
by Dylan
06:33
created

App::setAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Lifeboat;
4
5
use Lifeboat\Utils\Curl;
6
use Lifeboat\Utils\URL;
7
use Lifeboat\Utils\Utils;
8
9
/**
10
 * Class App
11
 * @package Lifeboat
12
 */
13
class App extends Connector {
14
15
    const CODE_URL = '/oauth/code';
16
17
    private string $_app_id;
18
    private string $_app_secret;
19
    private string $_api_challenge;
20
21
    public function __construct(string $app_id, string $app_secret, $auth_domain = self::AUTH_DOMAIN)
22
    {
23
        $this->setAppID($app_id);
24
        $this->setAppSecret($app_secret);
25
        $this->_auth_domain = rtrim($auth_domain, '/');
26
    }
27
28
    /**
29
     * @return string
30
     */
31
    public function getAppID(): string
32
    {
33
        return $this->_app_id;
34
    }
35
36
    /**
37
     * @param string $id
38
     * @return $this
39
     */
40
    public function setAppID(string $id): App
41
    {
42
        $this->_app_id = $id;
43
        return $this;
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function getAppSecret(): string
50
    {
51
        return $this->_app_secret;
52
    }
53
54
    /**
55
     * @param string $token
56
     * @return $this
57
     */
58
    public function setAccessToken(string $token): App
59
    {
60
        $this->_access_token = $token;
61
        return $this;
62
    }
63
64
    /**
65
     * @param string $secret
66
     * @return $this
67
     */
68
    public function setAppSecret(string $secret): App
69
    {
70
        $this->_app_secret = $secret;
71
        return $this;
72
    }
73
74
    /**
75
     * @param string $challenge
76
     * @return $this
77
     */
78
    public function setAPIChallenge(string $challenge): App
79
    {
80
        $this->_api_challenge = $challenge;
81
        return $this;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getAPIChallenge(): string
88
    {
89
        if (!$this->_api_challenge) $this->_api_challenge = Utils::create_random_string(128);
90
        return $this->_api_challenge;
91
    }
92
93
    /**
94
     * @param string $process_url
95
     * @param string $error_url
96
     * @param string $challenge
97
     * @return string
98
     */
99
    public function getAuthURL(string $process_url, string $error_url, string $challenge): string
100
    {
101
        $url    = URL::setGetVar('app_id', $this->getAppID(), $this->auth_url(self::CODE_URL));
102
        $url    = URL::setGetVar('process_url', urlencode($process_url), $url);
103
        $url    = URL::setGetVar('error_url', urlencode($error_url), $url);
104
105
        return URL::setGetVar('challenge', Utils::pack($challenge), $url);
106
    }
107
108
    /**
109
     * @param string $code
110
     * @return string
111
     */
112
    public function fetchAccessToken(string $code): string
113
    {
114
        $curl = new Curl($this->auth_url(self::TOKEN_URL), [
115
            'challenge'     => $this->getAPIChallenge(),
116
            'code'          => $code,
117
            'app_secret'    => $this->getAppSecret()
118
        ]);
119
120
        $curl->setMethod('POST');
121
        $response = $curl->curl();
122
        $json = $response->getJSON();
123
124
        if (!$response->isValid() || !$json || !array_key_exists('access_token', $json)) {
125
            return $json['access_token'];
126
        } else {
127
            return '';
128
        }
129
    }
130
131
    /**
132
     * @return string
133
     */
134
    public function getAccessToken(): string
135
    {
136
        return $this->_access_token;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_access_token could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
137
    }
138
}
139