| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright  Copyright (c) Flipbox Digital Limited | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license    https://github.com/flipbox/relay-hubspot/blob/master/LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @link       https://github.com/flipbox/relay-hubspot | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace Flipbox\Relay\HubSpot\Middleware; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Flipbox\Relay\Middleware\AbstractMiddleware; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Flipbox\Relay\HubSpot\AuthorizationInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Flipbox\Skeleton\Exceptions\InvalidConfigurationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Psr\Http\Message\RequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @author Flipbox Factory <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @since 1.0.0 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | class Authorization extends AbstractMiddleware | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @var AuthorizationInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     public $authorization; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @inheritdoc | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @throws InvalidConfigurationException | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function init() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         parent::init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         if (!$this->authorization instanceof AuthorizationInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             throw new InvalidConfigurationException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 "The class '%s' requires an authorization class that is an instance of '%s', '%s' given.", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 get_class($this), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 AuthorizationInterface::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 get_class($this->authorization) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * @inheritdoc | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function __invoke( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         RequestInterface $request, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         ResponseInterface $response, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         callable $next = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         parent::__invoke($request, $response, $next); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $request = $this->authorization->prepareAuthorizationRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         // Clone in case we need to refresh and resend | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $runner = clone $next; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return $this->authorization->handleAuthorizationResponse( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $next($request, $response), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $request, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $runner | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 67 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |  |