Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
digitalascetic/google-api-client / Service / GoogleApiService.php
Size: Mime:
<?php
/**
 * Created by PhpStorm.
 * User: eduarddezacastellano
 * Date: 07/09/2018
 * Time: 09:26
 */

namespace DigitalAscetic\GoogleApiClientBundle\Service;

use Google_Client;

class GoogleApiService
{
    const SERVICE_NAME = 'ascetic_google_api_client';

    /** @var array $config */
    protected $config;

    /** @var Google_Client $client */
    private $client;

    /**
     * GoogleApiService constructor.
     * @param array $config
     */
    public function __construct(array $config)
    {
        $this->config = $config;
    }

    /**
     * @param string|null $host
     * @return Google_Client|string
     */
    private function createGoogleClient(string $host = null)
    {
        $client = new Google_Client();
        $client->setApplicationName($this->config['application_name']);

        if (!empty($this->config['scopes'])) {
            $client->setScopes($this->config['scopes']);
        }

        $client->setAuthConfig($this->config['client_secret_file_path']);
        $client->setAccessType('offline');        // offline access
        $client->setIncludeGrantedScopes(true);   // incremental auth

        if (!empty($host)) {
            $client->setRedirectUri($this->config['protocol'] . '://' . $host);
        } else if (!empty($this->config['oauth_redirect_uri'])) {
            $client->setRedirectUri($this->config['oauth_redirect_uri']);
        } else {
            $client->setRedirectUri($this->config['protocol'] . '://' . $_SERVER["HTTP_HOST"]);
        }

        $this->client = $client;

        return $client;
    }


    /**
     * @param string|null $host
     * @return Google_Client
     */
    public function getClient(string $host = null)
    {
        if (!empty($host)) {
            return $this->createGoogleClient($host);
        }

        return isset($this->client) ? $this->client : $this->createGoogleClient($host);
    }

}