bundles/ConnectionApiSageBundle/Service/APIAuthenticationService.php line 139

Open in your IDE?
  1. <?php
  2. namespace ConnectionApiSageBundle\Service;
  3. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  4. /**
  5.  * Class APIAuthenticationService
  6.  * @package ConnectionApiSageBundle\Service
  7.  * @author Carlos Gutierrez <carlos.gutierrez@indabasolutions.com>
  8.  * 
  9.  * Handle authentication with Sage API 
  10.  */
  11. class APIAuthenticationService
  12. {
  13.     private $grantType;
  14.     private $clientId;
  15.     private $clientSecret;
  16.     private $authUrl;
  17.     private $tokenUrl;
  18.     private $scope;
  19.     private $credentials;
  20.     private $baseUrl;
  21.     private $resourceId;
  22.     private static $instances = [];
  23.     private $session;
  24.     public function __construct(
  25.         string $grantType
  26.         string $clientId,
  27.         string $clientSecret,
  28.         string $authUrl,
  29.         string $tokenUrl,
  30.         string $scope,
  31.         string $baseUrl,
  32.         string $resourceId
  33.         SessionInterface $session
  34.     )
  35.     {   
  36.         $this->grantType $grantType;
  37.         $this->clientId $clientId;
  38.         $this->clientSecret $clientSecret;
  39.         $this->authUrl $authUrl;
  40.         $this->tokenUrl $tokenUrl;
  41.         $this->scope $scope;
  42.         $this->baseUrl $baseUrl;
  43.         $this->resourceId $resourceId;
  44.         $this->url $this->baseUrl DIRECTORY_SEPARATOR $this->resourceId DIRECTORY_SEPARATOR;
  45.         $this->session $session;
  46.     }
  47.     /**
  48.      * __clone
  49.      *
  50.      * @return void
  51.      */
  52.     protected function __clone() { }
  53.     /**
  54.      * __wakeup
  55.      * 
  56.      */
  57.     public function __wakeup()
  58.     {
  59.         throw new \Exception("Cannot unserialize a singleton.");
  60.     }
  61.     /**
  62.      * getInstance Singleton
  63.      *
  64.      * @return Singleton
  65.      */
  66.     public static function getInstance(): Singleton
  67.     {
  68.         $cls = static::class;
  69.         if (!isset(self::$instances[$cls])) {
  70.             self::$instances[$cls] = new static();
  71.         }
  72.  
  73.         return self::$instances[$cls];
  74.     }
  75.     /**
  76.      * method Authentication
  77.      *
  78.      * @return array
  79.      */
  80.     public function authentication(): array
  81.     {
  82.         try {
  83.             $params = [ 
  84.                 'grant_type' => $this->grantType,
  85.                 'client_id' => $this->clientId,
  86.                 'client_secret' => $this->clientSecret,    
  87.                 'auth_url' => $this->authUrl,
  88.                 'scope' =>   $this->scope            
  89.             ];
  90.             // Request the credentials from the api 
  91.             $response $this->curlExec($params);
  92.             // Decode json and return
  93.             $this->credentials json_decode($responsetrue);
  94.             // set session
  95.             $this->session->set('_token'$this->credentials);
  96.             $this->session->set('expire_token', new \DateTime("now"));
  97.             // return array
  98.             return $this->credentials;
  99.         } catch (\Throwable $th) {
  100.             $this->session->set('_token', ['access_token' => '']);
  101.             $this->session->set('expire_token', (new \DateTime("now"))->modify('-1 day'));
  102.             return [];
  103.         }
  104.     }
  105.     /**
  106.      * isAuthentication
  107.      *
  108.      * @return boolean
  109.      */
  110.     public function isAuthentication()
  111.     {
  112.         if($this->session->get('_token') && $this->session->get('_token') !=='' ) {
  113.             return true;
  114.         }
  115.         return false;
  116.     }
  117.     /**
  118.      * getToken
  119.      *
  120.      * @return string
  121.      */
  122.     public function getToken(): string
  123.     {
  124.         if(!$this->isAuthentication()) {
  125.             $this->authentication();
  126.         }
  127.         
  128.         $this->getExpireToken();
  129.         return $this->session->get('_token')['access_token'];
  130.     }
  131.     /**
  132.      * curlExec
  133.      *
  134.      * @param [type] $params
  135.      * @return string
  136.      */
  137.     protected function curlExec($params): string
  138.     {
  139.         // Init curl
  140.         $curlInstance  curl_init();
  141.         // Set curl options
  142.         curl_setopt($curlInstanceCURLOPT_RETURNTRANSFER1);
  143.         curl_setopt($curlInstanceCURLOPT_URL$this->tokenUrl);
  144.         curl_setopt($curlInstanceCURLOPT_POSTtrue);
  145.         curl_setopt($curlInstanceCURLOPT_POSTFIELDS$params);
  146.         curl_setopt($curlInstanceCURLOPT_SSL_VERIFYPEERfalse);
  147.         // Execute curl
  148.         $response curl_exec($curlInstance);
  149.         // Close connection
  150.         curl_close($curlInstance);
  151.         return $response;
  152.     }
  153.     /**
  154.      * getExpireToken
  155.      *
  156.      * @return void
  157.      */
  158.     public function getExpireToken()
  159.     {
  160.         try {
  161.             $now = new \DateTime("now");
  162.             $diff $now->diff($this->session->get('expire_token'));
  163.             if($diff->== 1) {
  164.                 $this->authentication();
  165.             }
  166.         } catch (\Throwable $th) {
  167.             $this->authentication();
  168.         }
  169.     }
  170.     /**
  171.      * getBACCseUrl
  172.      *
  173.      * @return string
  174.      */
  175.     public function getBaseUrl(): string
  176.     {
  177.         return $this->url;
  178.     }
  179. }