|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Token Provider Class |
|
6
|
|
|
* @category Ticaje |
|
7
|
|
|
* @author Max Demian <[email protected]> |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Ticaje\AeSdk\Infrastructure\Provider\Token; |
|
11
|
|
|
|
|
12
|
|
|
use Ticaje\AConnector\Interfaces\Provider\Token\TokenProviderInterface as MiddlewareTokenProviderInterface; |
|
13
|
|
|
|
|
14
|
|
|
use Ticaje\AeSdk\Infrastructure\Interfaces\Provider\Token\TokenProviderInterface; |
|
15
|
|
|
use Ticaje\AeSdk\Infrastructure\Interfaces\Provider\Token\TokenResponderInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class Token |
|
19
|
|
|
* @package Ticaje\AeSdk\Infrastructure\Provider\Token |
|
20
|
|
|
* This is a specific implementation of a token provider suited to AE needs. |
|
21
|
|
|
* This class can be used as a standalone service since it follows a Service Isolation pattern |
|
22
|
|
|
* The only condition to this is using a Dependency Container or some artifact that orchestrates the |
|
23
|
|
|
* bootstrapping of the dependency tree to high level modules using this lower level module. |
|
24
|
|
|
* This is fully compliant with Dependency Inversion principle. |
|
25
|
|
|
*/ |
|
26
|
|
|
class Token implements TokenProviderInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var MiddlewareTokenProviderInterface $middleWare */ |
|
29
|
|
|
private $middleWare; |
|
30
|
|
|
|
|
31
|
|
|
private $responder; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Token constructor. |
|
35
|
|
|
* @param MiddlewareTokenProviderInterface $middleWare |
|
36
|
|
|
* @param TokenResponderInterface $tokenResponder |
|
37
|
|
|
* We're gonna use anymore a framework coupled dependency as so far, Ticaje\Connector is a Magento based extension |
|
38
|
|
|
* this library is relying on right now, this is inconsistent with agnostic approach. |
|
39
|
|
|
* From now on Ticaje\Connector is gonna be a framework independent library, so the glue amongst libraries will be defined |
|
40
|
|
|
* in the light of a DC container or similar artifact. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct( |
|
43
|
|
|
MiddlewareTokenProviderInterface $middleWare, |
|
44
|
|
|
TokenResponderInterface $tokenResponder |
|
45
|
|
|
) { |
|
46
|
|
|
$this->middleWare = $middleWare; |
|
47
|
|
|
$this->responder = $tokenResponder; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @inheritDoc |
|
52
|
|
|
* It is the responsibility of consumer to pass through AE compliant params |
|
53
|
|
|
*/ |
|
54
|
|
|
public function setParams(array $params): MiddlewareTokenProviderInterface |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->middleWare->setParams($this->buildRequest($params)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return mixed |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getAccessToken(): TokenResponderInterface |
|
63
|
|
|
{ |
|
64
|
|
|
$response = $this->responder->process($this->middleWare->getAccessToken()); |
|
65
|
|
|
return $response; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param $params |
|
70
|
|
|
* @return array |
|
71
|
|
|
* This should be abstracted away into a builder/dto class |
|
72
|
|
|
* Anyway, this is not so bad since it's compliant with AE business constraints |
|
73
|
|
|
*/ |
|
74
|
|
|
private function buildRequest($params) |
|
75
|
|
|
{ |
|
76
|
|
|
$result = [ |
|
77
|
|
|
'code' => $params['code'], |
|
78
|
|
|
'grant_type' => 'authorization_code', |
|
79
|
|
|
'client_id' => $params['client_id'], |
|
80
|
|
|
'client_secret' => $params['client_secret'], |
|
81
|
|
|
'sp' => 'ae', |
|
82
|
|
|
'redirect_url' => $params['redirect_url'] |
|
83
|
|
|
]; |
|
84
|
|
|
return $result; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|