1
1
<?php
2
2
3
- namespace FattureInCloud \OAuth2 ;
3
+ namespace FattureInCloud \OAuth2 \ OAuth2AuthorizationCode ;
4
4
5
+ use FattureInCloud \OAuth2 \OAuth2Error ;
6
+ use FattureInCloud \OAuth2 \OAuth2Manager ;
7
+ use FattureInCloud \OAuth2 \OAuth2TokenResponse ;
5
8
use GuzzleHttp \Client ;
6
9
7
10
/**
8
11
* The Manager for OAuth2 Authorization Code flow.
9
12
*/
10
- class OAuth2AuthorizationCodeManager
13
+ class OAuth2AuthorizationCodeManager extends OAuth2Manager
11
14
{
12
15
private const DEFAULT_BASE_URI = 'https://api-v2.fattureincloud.it ' ;
13
16
14
- /**
15
- * @var Client
16
- */
17
- private $ client ;
18
- /**
19
- * @var string
20
- */
21
- private $ clientId ;
22
17
/**
23
18
* @var string
24
19
*/
@@ -27,10 +22,6 @@ class OAuth2AuthorizationCodeManager
27
22
* @var string
28
23
*/
29
24
private $ redirectUri ;
30
- /**
31
- * @var string|null
32
- */
33
- private $ baseUri ;
34
25
35
26
/**
36
27
* @param string $clientId
@@ -41,51 +32,9 @@ class OAuth2AuthorizationCodeManager
41
32
*/
42
33
public function __construct (string $ clientId , string $ clientSecret , string $ redirectUri , string $ baseUri = self ::DEFAULT_BASE_URI , Client $ client = null )
43
34
{
44
- if ($ client === null ) {
45
- $ this ->client = new Client ();
46
- } else {
47
- $ this ->client = $ client ;
48
- }
49
- $ this ->clientId = $ clientId ;
35
+ parent ::__construct ($ clientId ,$ baseUri , $ client );
50
36
$ this ->clientSecret = $ clientSecret ;
51
37
$ this ->redirectUri = $ redirectUri ;
52
- $ this ->baseUri = $ baseUri ;
53
- }
54
-
55
- /**
56
- * @return Client
57
- */
58
- public function getClient (): Client
59
- {
60
- return $ this ->client ;
61
- }
62
-
63
- /**
64
- * @param Client|null $client
65
- */
66
- public function setClient (Client $ client = null ): void
67
- {
68
- if ($ client === null ) {
69
- $ this ->client = new Client ();
70
- } else {
71
- $ this ->client = $ client ;
72
- }
73
- }
74
-
75
- /**
76
- * @return string
77
- */
78
- public function getClientId (): string
79
- {
80
- return $ this ->clientId ;
81
- }
82
-
83
- /**
84
- * @param string $clientId
85
- */
86
- public function setClientId (string $ clientId ): void
87
- {
88
- $ this ->clientId = $ clientId ;
89
38
}
90
39
91
40
/**
@@ -120,26 +69,6 @@ public function setRedirectUri(string $redirectUri): void
120
69
$ this ->redirectUri = $ redirectUri ;
121
70
}
122
71
123
- /**
124
- * @return string
125
- */
126
- public function getBaseUri (): ?string
127
- {
128
- if ($ this ->baseUri === null ) {
129
- return self ::DEFAULT_BASE_URI ;
130
- } else {
131
- return $ this ->baseUri ;
132
- }
133
- }
134
-
135
- /**
136
- * @param string|null $baseUri
137
- */
138
- public function setBaseUri (?string $ baseUri ): void
139
- {
140
- $ this ->baseUri = $ baseUri ;
141
- }
142
-
143
72
/**
144
73
* @param array $scopes
145
74
* @param string $state
@@ -172,7 +101,8 @@ public function getParamsFromUrl(string $url): OAuth2AuthorizationCodeParams
172
101
}
173
102
174
103
/**
175
- * @param string $url
104
+ * @param string $code
105
+ * @return OAuth2Error|OAuth2TokenResponse
176
106
*/
177
107
public function fetchToken (string $ code )
178
108
{
@@ -184,11 +114,12 @@ public function fetchToken(string $code)
184
114
'redirect_uri ' => $ this ->redirectUri ,
185
115
'code ' => $ code
186
116
];
187
- return $ this ->executePost ($ tokenUri , $ body );
117
+ return $ this ->executeTokenPost ($ tokenUri , $ body );
188
118
}
189
119
190
120
/**
191
121
* @param string $refreshToken
122
+ * @return OAuth2Error|OAuth2TokenResponse
192
123
*/
193
124
public function refreshToken (string $ refreshToken )
194
125
{
@@ -199,19 +130,6 @@ public function refreshToken(string $refreshToken)
199
130
'client_secret ' => $ this ->clientSecret ,
200
131
'refresh_token ' => $ refreshToken ,
201
132
];
202
- return $ this ->executePost ($ tokenUri , $ body );
203
- }
204
-
205
- private function executePost (string $ uri , array $ body )
206
- {
207
- $ r = $ this ->client ->post ($ uri , ['json ' => $ body ]);
208
- $ statusCode = $ r ->getStatusCode ();
209
- $ resBodyJson = $ r ->getBody ()->getContents ();
210
- $ resBody = json_decode ($ resBodyJson , true );
211
- if ($ statusCode === 200 ) {
212
- return new OAuth2AuthorizationCodeTokenResponse ($ resBody ['token_type ' ], $ resBody ['access_token ' ], $ resBody ['refresh_token ' ], $ resBody ['expires_in ' ]);
213
- } else {
214
- return new OAuth2AuthorizationCodeError ($ statusCode , $ resBody ['error ' ], $ resBody ['error_description ' ]);
215
- }
133
+ return $ this ->executeTokenPost ($ tokenUri , $ body );
216
134
}
217
135
}
0 commit comments