<?php
namespace App\Service;
use Google\Service\MyBusinessAccountManagement;
use Google\Service\MyBusinessBusinessInformation;
use Google_Client;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Google_Service_MyBusiness;
use Symfony\Component\Security\Core\Security;
use Google_Service_Exception;
use Google_Service_MyBusiness_ReviewReply;
class GoogleApiService
{
private $params;
public $client;
public $accounts = [];
public $locations;
public function __construct(ParameterBagInterface $params, Security $security)
{
$this->params = $params;
$this->security = $security;
}
public function getClient()
{
$scope = 'https://www.googleapis.com/auth/plus.business.manage';
$client = new Google_Client();
$client->setAuthConfig($this->params->get('google_directory') . '/credentials.json');
$client->setRedirectUri($this->params->get('google_oauth2_callback'));
$client->addScope($scope);
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$this->client = $client;
return $this;
}
public function getAuthUrl() : string
{
return $this->client->createAuthUrl();
}
public function getGoogleMyBusinessAccounts()
{
try {
$accounts = new MyBusinessAccountManagement($this->client);
return $accounts->accounts->listAccounts();
} catch (Google_Service_Exception $e){
return [];
}
}
public function getGoogleMyBusinessLocations($account, $nextPageToken = null)
{
try{
$locations = new MyBusinessBusinessInformation($this->client);
$params = [
'orderBy' => 'title',
'pageSize' => 9,
'readMask' => 'name,languageCode,storeCode,title,phoneNumbers,categories,storefrontAddress,websiteUri,regularHours,specialHours,serviceArea,labels,adWordsLocationExtensions,latlng,openInfo,metadata,profile,relationshipData,moreHours,serviceItems'
];
if($nextPageToken !== null) {
$params = array_merge($params, [
'pageToken' => $nextPageToken
]);
}
$this->locations = $locations->accounts_locations->listAccountsLocations($account, $params);
return $this->locations;
} catch (Google_Service_Exception $e){
return null;
}
}
public function getAllGoogleMyBusinessLocations($account, $nextPageToken = null)
{
try{
$locations = new MyBusinessBusinessInformation($this->client);
$params = [
'orderBy' => 'title',
'pageSize' => 100,
'readMask' => 'name,languageCode,storeCode,title,phoneNumbers,categories,storefrontAddress,websiteUri,regularHours,specialHours,serviceArea,labels,adWordsLocationExtensions,latlng,openInfo,metadata,profile,relationshipData,moreHours,serviceItems'
];
if($nextPageToken !== null) {
$params = array_merge($params, [
'pageToken' => $nextPageToken
]);
}
$this->locations = $locations->accounts_locations->listAccountsLocations($account, $params);
return $this->locations;
} catch (Google_Service_Exception $e){
return null;
}
}
public function getLocationBatchGetReviews($account, $token)
{
try{
$googleMyBusiness = new Google_Service_MyBusiness($this->client);
return $this->locations;
} catch (Google_Service_Exception $e){
return null;
}
}
public function getGoogleMyBusinessSingleLocation($location)
{
try{
$googleMyBusiness = new Google_Service_MyBusiness($this->client);
return $googleMyBusiness->accounts_locations->get($location);
} catch (Google_Service_Exception $e){
// return $e->getMessage();
return null;
}
}
public function getGoogleMyBusinessLocationsAllReviews($account)
{
try{
$googleMyBusiness = new Google_Service_MyBusiness($this->client);
$this->locations = $googleMyBusiness->accounts_locations_reviews->listAccountsLocationsReviews($account);
return $this->locations;
} catch (Google_Service_Exception $e){
return null;
}
}
public function replyGoogleMyBusinessReview($review, $comment)
{
try{
$googleMyBusiness = new Google_Service_MyBusiness($this->client);
$reply = new Google_Service_MyBusiness_ReviewReply();
$reply->setComment($comment);
$reply = $googleMyBusiness->accounts_locations_reviews->updateReply($review, $reply);
return $reply;
} catch (Google_Service_Exception $e){
return null;
}
}
public function getGoogleMyBusinessLocationsCurrentPageReviews($account, $nextPageToken = null)
{
try{
$googleMyBusiness = new Google_Service_MyBusiness($this->client);
if($nextPageToken === null){
$this->locations = $googleMyBusiness->accounts_locations_reviews->listAccountsLocationsReviews($account, [
"pageSize" => 10
]);
}else{
$this->locations = $googleMyBusiness->accounts_locations_reviews->listAccountsLocationsReviews($account, [
"pageSize" => 10,
"pageToken" => $nextPageToken
]);
}
return $this->locations;
} catch (Google_Service_Exception $e){
return null;
}
}
public function getGoogleMyBusinessLocationsCurrentPageReviewsWithStarsFilter($account, $nextPageToken = null, $stars)
{
try{
$googleMyBusiness = new Google_Service_MyBusiness($this->client);
if($nextPageToken === null){
$this->locations = $googleMyBusiness->accounts_locations_reviews->listAccountsLocationsReviews($account, [
"pageSize" => 10
]);
}else{
$this->locations = $googleMyBusiness->accounts_locations_reviews->listAccountsLocationsReviews($account, [
"pageSize" => 10,
"pageToken" => $nextPageToken
]);
}
return $this->locations;
} catch (Google_Service_Exception $e){
return null;
}
}
public function findLocation($query, $account){
$googleMyBusiness = new Google_Service_MyBusiness($this->client);
$locations = $googleMyBusiness->accounts_locations->listAccountsLocations($account);
return $locations;
}
public function getReportInsights($name, $locationName, $access_token)
{
}
}