src/Infrastructure/Service/GoogleHelper.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Infrastructure\Service;
  3. use Symfony\Component\DependencyInjection\ContainerInterface;
  4. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  5. use Symfony\Component\Routing\RouterInterface;
  6. class GoogleHelper
  7. {
  8.     private \Google_Client $client;
  9.     private RouterInterface $router;
  10.     private string $credentialsPath;
  11.     private string $appHostAddress;
  12.     public function __construct(ContainerInterface $containerRouterInterface $router)
  13.     {
  14.         $this->appHostAddress $_ENV['APP_HOST'];
  15.         $credentialsPath $container->getParameter('kernel.project_dir') . '/var/resources/google_credentials.json';
  16.         if (!file_exists($credentialsPath)) {
  17.             throw new NotFoundHttpException('Google credentials were not found');
  18.         }
  19.         $this->credentialsPath $credentialsPath;
  20.         $this->client = new \Google_Client();
  21.         $this->router $router;
  22.     }
  23.     public function login(): string
  24.     {
  25.         return $this->getInstance()->createAuthUrl();
  26.     }
  27.     public function getInstance(): \Google_Client
  28.     {
  29.         $this->client->setAuthConfig($this->credentialsPath);
  30.         $this->client->setAccessType('offline');
  31.         $this->client->setRedirectUri($this->appHostAddress $this->router->generate('google_login_handle'));
  32.         $this->client->setIncludeGrantedScopes(true);
  33.         $this->client->addScope([
  34.             \Google\Service\Oauth2::USERINFO_EMAIL,
  35.             \Google\Service\Oauth2::USERINFO_PROFILE
  36.         ]);
  37.         return $this->client;
  38.     }
  39. }