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