| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of graze/gigya-client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @license https://github.com/graze/gigya-client/blob/master/LICENSE.md | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @link    https://github.com/graze/gigya-client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Graze\Gigya\Auth\OAuth2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use DateInterval; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use DateTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Graze\Gigya\Gigya; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Graze\Gigya\Response\ErrorCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class GigyaCodeGrant implements GrantInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** @var Gigya */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $gigya; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $code; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $redirectUri; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** @var AccessToken|null */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param Gigya  $gigya | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param string $code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param string $redirectUri | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 5 |  |     public function __construct(Gigya $gigya, $code, $redirectUri) | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 5 |  |         $this->gigya = $gigya; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 5 |  |         $this->code = $code; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 5 |  |         $this->redirectUri = $redirectUri; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @return AccessToken|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 4 |  |     public function getToken() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 4 |  |         if (is_null($this->token)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 4 |  |             $response = $this->gigya->socialize()->getToken([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 4 |  |                 'code'         => $this->code, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 4 |  |                 'redirect_uri' => $this->redirectUri, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 4 |  |                 'grant_type'   => 'authorization_code', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 4 |  |             ], ['auth' => 'credentials']); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 55 | 4 | View Code Duplication |             if ($response->getErrorCode() == ErrorCode::OK) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 4 |  |                 $data = $response->getData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 4 |  |                 $token = $data->get('access_token'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 4 |  |                 $expires = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 4 |  |                 if ($data->has('expires_in')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 3 |  |                     $expires = (new DateTime())->add(new DateInterval(sprintf('PT%dS', $data->get('expires_in', 0)))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 3 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 4 |  |                 $this->token = new AccessToken($token, $expires); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 4 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 4 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 4 |  |         return $this->token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |  | 
            
                        
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.