1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\Gigya\Auth\OAuth2; |
4
|
|
|
|
5
|
|
|
use DateInterval; |
6
|
|
|
use DateTime; |
7
|
|
|
use Graze\Gigya\Gigya; |
8
|
|
|
use Graze\Gigya\Response\ErrorCode; |
9
|
|
|
|
10
|
|
|
class GigyaCodeGrant implements GrantInterface |
11
|
|
|
{ |
12
|
|
|
/** @var Gigya */ |
13
|
|
|
private $gigya; |
14
|
|
|
/** @var string */ |
15
|
|
|
private $code; |
16
|
|
|
/** @var string */ |
17
|
|
|
private $redirectUri; |
18
|
|
|
/** @var AccessToken|null */ |
19
|
|
|
private $token; |
20
|
|
|
/** @var string */ |
21
|
|
|
private $apiKey; |
22
|
|
|
/** @var string */ |
23
|
|
|
private $secret; |
24
|
|
|
/** @var string|null */ |
25
|
|
|
private $userKey; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Gigya $gigya |
29
|
|
|
* @param string $code |
30
|
|
|
* @param string $redirectUri |
31
|
|
|
* @param string $apiKey |
32
|
|
|
* @param string $secret |
33
|
|
|
* @param string|null $userKey |
34
|
|
|
*/ |
35
|
5 |
View Code Duplication |
public function __construct(Gigya $gigya, $code, $redirectUri, $apiKey, $secret, $userKey = null) |
|
|
|
|
36
|
|
|
{ |
37
|
5 |
|
$this->gigya = $gigya; |
38
|
5 |
|
$this->code = $code; |
39
|
5 |
|
$this->redirectUri = $redirectUri; |
40
|
5 |
|
$this->apiKey = $apiKey; |
41
|
5 |
|
$this->secret = $secret; |
42
|
5 |
|
$this->userKey = $userKey; |
43
|
5 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return AccessToken|null |
47
|
|
|
*/ |
48
|
4 |
|
public function getToken() |
49
|
|
|
{ |
50
|
4 |
|
if (is_null($this->token)) { |
51
|
4 |
|
$response = $this->gigya->socialize()->getToken([ |
52
|
4 |
|
'client_id' => $this->userKey ?: $this->apiKey, |
53
|
4 |
|
'client_secret' => $this->secret, |
54
|
4 |
|
'code' => $this->code, |
55
|
4 |
|
'redirect_uri' => $this->redirectUri, |
56
|
4 |
|
'grant_type' => 'authorization_code', |
57
|
4 |
|
], ['auth' => 'none']); |
58
|
4 |
View Code Duplication |
if ($response->getErrorCode() == ErrorCode::OK) { |
|
|
|
|
59
|
4 |
|
$data = $response->getData(); |
60
|
4 |
|
$token = $data->get('access_token'); |
61
|
4 |
|
$expiresIn = $data->get('expires_in', null); |
62
|
4 |
|
$expires = null; |
63
|
4 |
|
if (!is_null($expiresIn)) { |
64
|
3 |
|
$expires = (new DateTime())->add(new DateInterval(sprintf('PT%dS', $expiresIn))); |
65
|
|
|
} |
66
|
4 |
|
$this->token = new AccessToken($token, $expires); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
4 |
|
return $this->token; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.