|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Gateway Class |
|
6
|
|
|
* @category Ticaje |
|
7
|
|
|
* @package Ticaje_Connector |
|
8
|
|
|
* @author Hector Luis Barrientos <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Ticaje\Connector\Gateway\Provider\Token; |
|
12
|
|
|
|
|
13
|
|
|
use Ticaje\Connector\Gateway\Provider\Base; |
|
14
|
|
|
use Ticaje\Connector\Interfaces\ClientInterface; |
|
15
|
|
|
use Ticaje\Connector\Interfaces\CredentialInterface; |
|
16
|
|
|
use Ticaje\Connector\Interfaces\Provider\Token\TokenProviderInterface; |
|
17
|
|
|
use Ticaje\Contract\Patterns\Interfaces\Decorator\Responder\ResponseInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Token |
|
21
|
|
|
* @package Ticaje\Connector\Gateway\Provider\Token |
|
22
|
|
|
*/ |
|
23
|
|
|
class Token extends Base implements TokenProviderInterface, CredentialInterface |
|
24
|
|
|
{ |
|
25
|
|
|
protected $accessToken; |
|
26
|
|
|
|
|
27
|
|
|
protected $verb; |
|
28
|
|
|
|
|
29
|
|
|
protected $endpoint; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Token constructor. |
|
33
|
|
|
* @param ClientInterface $connector |
|
34
|
|
|
* @param string $endpoint |
|
35
|
|
|
* @param string $verb |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct( |
|
38
|
|
|
ClientInterface $connector, |
|
39
|
|
|
string $endpoint, |
|
40
|
|
|
string $verb |
|
41
|
|
|
) { |
|
42
|
|
|
$this->verb = $verb; |
|
43
|
|
|
$this->endpoint = $endpoint; |
|
44
|
|
|
parent::__construct($connector); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @inheritDoc |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getAccessToken() |
|
51
|
|
|
{ |
|
52
|
|
|
if (!$this->accessToken) { |
|
53
|
|
|
$headers = [ |
|
54
|
|
|
ClientInterface::CONTENT_TYPE_KEY => ClientInterface::CONTENT_TYPE_FORM_URLENCODED, |
|
55
|
|
|
'Accept' => ClientInterface::CONTENT_TYPE_JSON, |
|
56
|
|
|
]; |
|
57
|
|
|
$params = $this->params; |
|
58
|
|
|
$endPoint = $this->endpoint; |
|
59
|
|
|
/** @var ResponseInterface $response */ |
|
60
|
|
|
$response = $this->connector->request($this->verb, $endPoint, $headers, $params); |
|
|
|
|
|
|
61
|
|
|
$this->accessToken = $response->content; |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
return $this->accessToken; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @inheritDoc |
|
68
|
|
|
* Perhaps using composition on this might be a cleaner way |
|
69
|
|
|
*/ |
|
70
|
|
|
public function setParams(array $params): CredentialInterface |
|
71
|
|
|
{ |
|
72
|
|
|
$this->params = $params; |
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|