src/Service/GoogleApiService.php line 178

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Google\Service\MyBusinessAccountManagement;
  4. use Google\Service\MyBusinessBusinessInformation;
  5. use Google_Client;
  6. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  7. use Google_Service_MyBusiness;
  8. use Symfony\Component\Security\Core\Security;
  9. use Google_Service_Exception;
  10. use Google_Service_MyBusiness_ReviewReply;
  11. class GoogleApiService
  12. {
  13.     private $params;
  14.     public $client;
  15.     public $accounts = [];
  16.     public $locations;
  17.     public function __construct(ParameterBagInterface $paramsSecurity $security)
  18.     {
  19.         $this->params $params;
  20.         $this->security $security;
  21.     }
  22.     
  23.     public function getClient()
  24.     {
  25.         $scope 'https://www.googleapis.com/auth/plus.business.manage';
  26.         $client = new Google_Client();
  27.         $client->setAuthConfig($this->params->get('google_directory') . '/credentials.json');
  28.         $client->setRedirectUri($this->params->get('google_oauth2_callback'));
  29.         $client->addScope($scope);
  30.         $client->setAccessType('offline');
  31.         $client->setApprovalPrompt('force');
  32.         $this->client $client;
  33.         return $this;
  34.     }
  35.     public function getAuthUrl() : string
  36.     {
  37.         return $this->client->createAuthUrl();
  38.     }
  39.     public function getGoogleMyBusinessAccounts()
  40.     {
  41.         try {
  42.             $accounts = new MyBusinessAccountManagement($this->client);
  43.             return $accounts->accounts->listAccounts();
  44.         } catch (Google_Service_Exception $e){
  45.             return [];
  46.         }
  47.     }
  48.     public function getGoogleMyBusinessLocations($account$nextPageToken null)
  49.     {
  50.         try{
  51.             $locations = new MyBusinessBusinessInformation($this->client);
  52.             $params = [
  53.                 'orderBy' => 'title',
  54.                 'pageSize' => 9
  55.                 'readMask' => 'name,languageCode,storeCode,title,phoneNumbers,categories,storefrontAddress,websiteUri,regularHours,specialHours,serviceArea,labels,adWordsLocationExtensions,latlng,openInfo,metadata,profile,relationshipData,moreHours,serviceItems'
  56.             ];
  57.             if($nextPageToken !== null) {
  58.                 $params array_merge($params, [
  59.                     'pageToken' => $nextPageToken
  60.                 ]);
  61.             }
  62.             $this->locations $locations->accounts_locations->listAccountsLocations($account$params);
  63.             return $this->locations;
  64.         } catch (Google_Service_Exception $e){
  65.             return null;
  66.         }
  67.     }
  68.     public function getAllGoogleMyBusinessLocations($account$nextPageToken null)
  69.     {
  70.         try{
  71.             $locations = new MyBusinessBusinessInformation($this->client);
  72.             $params = [
  73.                 'orderBy' => 'title',
  74.                 'pageSize' => 100
  75.                 'readMask' => 'name,languageCode,storeCode,title,phoneNumbers,categories,storefrontAddress,websiteUri,regularHours,specialHours,serviceArea,labels,adWordsLocationExtensions,latlng,openInfo,metadata,profile,relationshipData,moreHours,serviceItems'
  76.             ];
  77.             if($nextPageToken !== null) {
  78.                 $params array_merge($params, [
  79.                     'pageToken' => $nextPageToken
  80.                 ]);
  81.             }
  82.             $this->locations $locations->accounts_locations->listAccountsLocations($account$params);
  83.             
  84.             return $this->locations;
  85.         } catch (Google_Service_Exception $e){
  86.             return null;
  87.         }
  88.     }
  89.     public function getLocationBatchGetReviews($account$token)
  90.     {
  91.         try{
  92.             $googleMyBusiness = new Google_Service_MyBusiness($this->client);
  93.             return $this->locations;
  94.         } catch (Google_Service_Exception $e){
  95.             return null;
  96.         }
  97.     }
  98.     public function getGoogleMyBusinessSingleLocation($location)
  99.     {
  100.         try{
  101.             $googleMyBusiness = new Google_Service_MyBusiness($this->client);
  102.             return $googleMyBusiness->accounts_locations->get($location);
  103.         } catch (Google_Service_Exception $e){
  104.             // return $e->getMessage();
  105.             return null;
  106.         }
  107.     }
  108.     public function getGoogleMyBusinessLocationsAllReviews($account)
  109.     {
  110.         try{
  111.             $googleMyBusiness = new Google_Service_MyBusiness($this->client);
  112.             $this->locations $googleMyBusiness->accounts_locations_reviews->listAccountsLocationsReviews($account);
  113.     
  114.             return $this->locations;
  115.         } catch (Google_Service_Exception $e){
  116.             return null;
  117.         }
  118.     }
  119.     public function replyGoogleMyBusinessReview($review$comment)
  120.     {
  121.         try{
  122.             $googleMyBusiness = new Google_Service_MyBusiness($this->client);
  123.             $reply = new Google_Service_MyBusiness_ReviewReply();
  124.             $reply->setComment($comment);
  125.             $reply $googleMyBusiness->accounts_locations_reviews->updateReply($review$reply);
  126.             return $reply;
  127.         } catch (Google_Service_Exception $e){
  128.             return null;
  129.         }
  130.     }
  131.     public function getGoogleMyBusinessLocationsCurrentPageReviews($account$nextPageToken null)
  132.     {
  133.         try{
  134.             
  135.             $googleMyBusiness = new Google_Service_MyBusiness($this->client);
  136.             if($nextPageToken === null){
  137.                 $this->locations $googleMyBusiness->accounts_locations_reviews->listAccountsLocationsReviews($account, [
  138.                     "pageSize" => 10
  139.                 ]);
  140.             }else{
  141.                 $this->locations $googleMyBusiness->accounts_locations_reviews->listAccountsLocationsReviews($account, [
  142.                     "pageSize" => 10,
  143.                     "pageToken" => $nextPageToken
  144.                 ]);
  145.             }
  146.     
  147.             return $this->locations;
  148.             
  149.         } catch (Google_Service_Exception $e){
  150.             return null;
  151.         }
  152.     }
  153.     public function getGoogleMyBusinessLocationsCurrentPageReviewsWithStarsFilter($account$nextPageToken null$stars)
  154.     {
  155.         try{
  156.             
  157.             $googleMyBusiness = new Google_Service_MyBusiness($this->client);
  158.             if($nextPageToken === null){
  159.                 $this->locations $googleMyBusiness->accounts_locations_reviews->listAccountsLocationsReviews($account, [
  160.                     "pageSize" => 10
  161.                 ]);
  162.             }else{
  163.                 $this->locations $googleMyBusiness->accounts_locations_reviews->listAccountsLocationsReviews($account, [
  164.                     "pageSize" => 10,
  165.                     "pageToken" => $nextPageToken
  166.                 ]);
  167.             }
  168.     
  169.             return $this->locations;
  170.             
  171.         } catch (Google_Service_Exception $e){
  172.             return null;
  173.         }
  174.     }
  175.  
  176.     public function findLocation($query$account){
  177.         
  178.         $googleMyBusiness = new Google_Service_MyBusiness($this->client);
  179.         $locations $googleMyBusiness->accounts_locations->listAccountsLocations($account);
  180.         return $locations;
  181.     }
  182.     public function getReportInsights($name$locationName$access_token)
  183.     {
  184.     
  185.     }
  186. }