app/Plugin/SiteKit42/Controller/Admin/DashboardController.php line 41

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\SiteKit42\Controller\Admin;
  13. use Eccube\Controller\AbstractController;
  14. use Plugin\SiteKit42\Service\Google_Site_Kit_Proxy_Client;
  15. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  18. class DashboardController extends AbstractController
  19. {
  20.     /**
  21.      * @var Google_Site_Kit_Proxy_Client
  22.      */
  23.     private $siteKitClient;
  24.     /**
  25.      * DashboardController constructor.
  26.      */
  27.     public function __construct(Google_Site_Kit_Proxy_Client $siteKitClient)
  28.     {
  29.         $this->siteKitClient $siteKitClient;
  30.     }
  31.     /**
  32.      * @Route("/%eccube_admin_route%/site_kit/dashboard", name="site_kit_dashboard")
  33.      * @Template("@SiteKit42/admin/dashboard.twig")
  34.      */
  35.     public function showGoogleSearchData()
  36.     {
  37.         $Member $this->getUser();
  38.         if (is_null($Member->getIdToken())) {
  39.             return $this->redirectToRoute('site_kit42_admin_config');
  40.         }
  41.         $jsonQuery $this->getJsonFromGoogleSearchData('query');
  42.         $jsonPage $this->getJsonFromGoogleSearchData('page');
  43.         $jsonDate $this->getJsonFromGoogleSearchData('date'null);
  44.         $arrayResponse json_decode($jsonDatetrue);
  45.         if (!array_key_exists('rows'$arrayResponse)) {
  46.             $arrayResponse['rows'] = [];
  47.         }
  48.         $arrayDate array_map(function ($row) {
  49.             $array[] = $row['keys'][0];
  50.             $array[] = $row['clicks'];
  51.             $array[] = $row['impressions'];
  52.             return $array;
  53.         }, $arrayResponse['rows']);
  54.         $header = [['Date''Clicks''Impression']];
  55.         $arrayDate array_merge($header$arrayDate);
  56.         return [
  57.             'json_query' => $this->formatJson($jsonQuery),
  58.             'json_page' => $this->formatJson($jsonPage),
  59.             'json_date' => $arrayDate,
  60.             'ownedSiteUrl' => $this->getSiteUrl(),
  61.         ];
  62.     }
  63.     private function getJsonFromGoogleSearchData($dimension$rowLimit '10')
  64.     {
  65.         $startDate date('Y-m-d'strtotime('-29 days'));
  66.         $endDate date('Y-m-d'strtotime('-1 days'));
  67.         $json = [
  68.             'dimensions' => [$dimension],
  69.             'startDate' => $startDate,
  70.             'endDate' => $endDate,
  71.             'rowLimit' => $rowLimit,
  72.         ];
  73.         $httpClient $this->siteKitClient->authorize();
  74.         $endpoint 'https://www.googleapis.com/webmasters/v3/sites/'.urlencode($this->getSiteUrl()).'/searchAnalytics/query';
  75.         $response $httpClient->request('POST'$endpoint, ['json' => $json]);
  76.         return $response->getBody()->getContents();
  77.     }
  78.     public function formatJson($responseBody)
  79.     {
  80.         $arrayResponse json_decode($responseBodytrue);
  81.         if (!array_key_exists('rows'$arrayResponse)) {
  82.             $arrayResponse['rows'] = [];
  83.         }
  84.         $arrayResponse['rows'] = array_map(function ($row) {
  85.             $row['ctr'] = sprintf('%.2f'round($row['ctr'], 2));
  86.             $row['position'] = sprintf('%.1f'round($row['position'], 1));
  87.             return $row;
  88.         }, $arrayResponse['rows']);
  89.         return $arrayResponse;
  90.     }
  91.     private function getSiteUrl()
  92.     {
  93.         return env('SITE_KIT_OWNED_SITE_URL'$this->generateUrl('homepage', [], UrlGeneratorInterface::ABSOLUTE_URL));
  94.     }
  95. }