]> _ Git - cubist_matomo.git/commitdiff
wip #6234 @1 master
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 1 Sep 2023 08:17:19 +0000 (10:17 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 1 Sep 2023 08:17:19 +0000 (10:17 +0200)
src/Reporting.php

index 9c27e59b4738a8bca83cd1bfec2083def626f74e..1e1abb2c3b64080285f3b22f2b5bbf69b794e628 100644 (file)
@@ -9,7 +9,8 @@ use Illuminate\Support\Facades\Cache;
 // Matomo Reporting API Wrapper
 // API Reference: https://developer.matomo.org/api-reference/reporting-api
 
-class Reporting {
+class Reporting
+{
     const PERIOD_DAY = 'day';
     const PERIOD_WEEK = 'week';
     const PERIOD_MONTH = 'month';
@@ -90,7 +91,8 @@ class Reporting {
         $date = self::DATE_YESTERDAY,
         $period = self::PERIOD_DAY,
         $format = self::FORMAT_JSON
-    ) {
+    )
+    {
         $this->serverUrl = $baseUrl;
         $this->token = $token;
         $this->siteId = $siteId;
@@ -103,14 +105,16 @@ class Reporting {
     /**
      * @return int
      */
-    public function getCacheDuration(): int {
+    public function getCacheDuration(): int
+    {
         return $this->cacheDuration;
     }
 
     /**
      * @param int $cacheDuration
      */
-    public function setCacheDuration(int $cacheDuration): void {
+    public function setCacheDuration(int $cacheDuration): void
+    {
         $this->cacheDuration = max(1, $cacheDuration);
     }
 
@@ -119,7 +123,8 @@ class Reporting {
      *
      * @return string
      */
-    public function getToken(): string {
+    public function getToken(): string
+    {
         return $this->token;
     }
 
@@ -129,7 +134,8 @@ class Reporting {
      * @param string $token
      * @return $this
      */
-    public function setToken(string $token): Reporting {
+    public function setToken(string $token): Reporting
+    {
         $this->token = $token;
 
         return $this;
@@ -140,7 +146,8 @@ class Reporting {
      *
      * @return mixed
      */
-    public function getSiteId() {
+    public function getSiteId()
+    {
         return $this->siteId;
     }
 
@@ -150,7 +157,8 @@ class Reporting {
      * @param mixed $id
      * @return $this
      */
-    public function setSiteId($id = null): Reporting {
+    public function setSiteId($id = null): Reporting
+    {
         $this->siteId = $id;
         return $this;
     }
@@ -160,7 +168,8 @@ class Reporting {
      *
      * @return string
      */
-    public function getFormat(): string {
+    public function getFormat(): string
+    {
         return $this->format;
     }
 
@@ -178,7 +187,8 @@ class Reporting {
      *        FORMAT_ORIGINAL
      * @return $this
      */
-    public function setFormat(string $format): Reporting {
+    public function setFormat(string $format): Reporting
+    {
         $this->format = $format;
         return $this;
     }
@@ -188,7 +198,8 @@ class Reporting {
      *
      * @return string
      */
-    public function getLanguage(): string {
+    public function getLanguage(): string
+    {
         return $this->language;
     }
 
@@ -198,7 +209,8 @@ class Reporting {
      * @param string $language
      * @return $this
      */
-    public function setLanguage(string $language): Reporting {
+    public function setLanguage(string $language): Reporting
+    {
         $this->language = $language;
         return $this;
     }
@@ -208,7 +220,8 @@ class Reporting {
      *
      * @return string|null
      */
-    public function getDate(): string|null {
+    public function getDate(): string|null
+    {
         return $this->date;
     }
 
@@ -220,7 +233,8 @@ class Reporting {
      *        DATE_YESTERDAY
      * @return $this
      */
-    public function setDate(string $date = null): Reporting {
+    public function setDate(string $date = null): Reporting
+    {
         $this->date = $date;
         return $this;
     }
@@ -230,7 +244,8 @@ class Reporting {
      *
      * @return string
      */
-    public function getPeriod(): string {
+    public function getPeriod(): string
+    {
         return $this->period;
     }
 
@@ -245,7 +260,8 @@ class Reporting {
      *        PERIOD_RANGE
      * @return $this
      */
-    public function setPeriod(string $period): Reporting {
+    public function setPeriod(string $period): Reporting
+    {
         $this->period = $period;
         return $this;
     }
@@ -253,21 +269,24 @@ class Reporting {
     /**
      * @return string
      */
-    public function getServerUrl(): string {
+    public function getServerUrl(): string
+    {
         return $this->serverUrl;
     }
 
     /**
      * @return string
      */
-    public function getLastApiUrl(): string {
+    public function getLastApiUrl(): string
+    {
         return $this->lastApiUrl;
     }
 
     /**
      * @return array
      */
-    public function getAllSites() {
+    public function getAllSites()
+    {
         if (null === $this->allSites) {
             $a = $this->apiCall('SitesManager.getAllSites');
             $this->allSites = [];
@@ -285,7 +304,8 @@ class Reporting {
      * @param bool $ecommerce
      * @return int|null
      */
-    public function createSite($name, $url, $search = true, $ecommerce = false) {
+    public function createSite($name, $url, $search = true, $ecommerce = false)
+    {
         $res = $this->apiCall('SitesManager.addSite', ['siteName' => $name, 'urls' => $url, 'ecommerce' => $ecommerce, 'siteSearch' => $search]);
         return $res['value'] ?? null;
     }
@@ -297,7 +317,8 @@ class Reporting {
      * @param false $ecommerce
      * @return int|null
      */
-    public function createSiteIfNotExistsOrReturnId($name, $url, $search = true, $ecommerce = false) {
+    public function createSiteIfNotExistsOrReturnId($name, $url, $search = true, $ecommerce = false)
+    {
         $id = $this->getSiteIdByURL($url);
         if (null === $id) {
             return $this->createSite($name, $url, $search, $ecommerce);
@@ -305,7 +326,8 @@ class Reporting {
         return $id;
     }
 
-    public function deleteSite($siteId) {
+    public function deleteSite($siteId)
+    {
         if (isset($this->allSites[$siteId])) {
             unset($this->allSites[$siteId]);
         }
@@ -316,52 +338,63 @@ class Reporting {
      * @param $url
      * @return int|null
      */
-    public function getSiteIdByURL($url) {
+    public function getSiteIdByURL($url)
+    {
         $res = $this->apiCall('SitesManager.getSitesIdFromSiteUrl', ['url' => $url]);
         return $res[0]['idsite'] ?? null;
     }
 
-    public function sum($table) {
+    public function sum($table)
+    {
 
     }
 
-    public function getVisits($params = []) {
+    public function getVisits($params = [])
+    {
         return $this->apiCall('VisitsSummary.get', $params);
     }
 
-    public function getPageViews($params = []) {
+    public function getPageViews($params = [])
+    {
         return $this->apiCall('Actions.get', $params);
     }
 
-    public function getPageUrls($params = []) {
+    public function getPageUrls($params = [])
+    {
         return $this->apiCall('Actions.getPageUrls', $params);
     }
 
-    public function getSearchKeywords($params = []) {
+    public function getSearchKeywords($params = [])
+    {
         return $this->apiCall('Actions.getSiteSearchKeywords', $params);
     }
 
-    public function getEventsByCategory($params = []) {
+    public function getEventsByCategory($params = [])
+    {
         // Note: Passing the parameter 'expanded' => 1 will include the sub-table data
         return $this->apiCall('Events.getCategory', $params + ['secondaryDimension' => 'eventName', 'expanded' => 0]);
     }
 
-    public function getOutlinks($params = []) {
+    public function getOutlinks($params = [])
+    {
         return $this->apiCall('Actions.getOutlinks', $params);
     }
 
-    public function getCountries($params = []) {
+    public function getCountries($params = [])
+    {
         return $this->apiCall('UserCountry.getCountry', $params);
     }
 
-    public function getVisitsSummary($params = []) {
+    public function getVisitsSummary($params = [])
+    {
         return $this->apiCall('VisitsSummary.get', $params);
     }
 
     /**
      * @throws \Exception|RequestException
      */
-    public function apiCall($method, $params = [], $options = []) {
+    public function apiCall($method, $params = [], $options = [])
+    {
         $default = [
             'token_auth' => $this->getToken(),
             'idSite' => $this->getSiteId(),
@@ -378,12 +411,7 @@ class Reporting {
         $options = array_merge($defaultOptions, $options);
         $cacheKey = 'matomo_api_call' . hash('sha256', print_r($options, true));
         return Cache::remember($cacheKey, $this->getCacheDuration(), function () use ($options, $method) {
-            try {
-                $response = $this->client->get('/', $options);
-            } catch (RequestException $e) {
-                // Just return the error instead of the full message because it contains the URL, which reveals the auth token.
-                throw new \Exception("Error: API call for method '{$method}' failed. {$e->getHandlerContext()['error']}");
-            }
+            $response = $this->client->get('/', $options);
 
             $API_response = json_decode($response->getBody()->getContents(), true);