You are here

class MatomoData in Matomo Reports 8

Utility class for data retrieved by reports.

Hierarchy

Expanded class hierarchy of MatomoData

3 files declare their use of MatomoData
MatomoReportsBlock.php in src/Plugin/Block/MatomoReportsBlock.php
MatomoReportsController.php in src/Controller/MatomoReportsController.php
MatomoReportsSettings.php in src/Form/MatomoReportsSettings.php

File

src/MatomoData.php, line 11

Namespace

Drupal\matomo_reports
View source
class MatomoData {

  /**
   * Return matomo token auth from global or user.
   *
   * @return string
   *   Matomo token auth.
   */
  public static function getToken() {
    $config = \Drupal::config('matomo_reports.matomoreportssettings');
    $current_user = \Drupal::currentUser();
    $user_data = \Drupal::service('user.data')
      ->get('matomo_reports', $current_user
      ->id());
    $user_token = $current_user
      ->id() && isset($user_data['matomo_reports_token_auth']) ? $user_data['matomo_reports_token_auth'] : '';
    $token_auth = $config
      ->get('matomo_reports_token_auth') ? $config
      ->get('matomo_reports_token_auth') : $user_token;
    return Html::escape($token_auth);
  }

  /**
   * Return server request results.
   *
   * @param string $query_url
   *   URL and query string to pass to matomo server.
   *
   * @return string
   *   Decoded server response.
   */
  public static function getResponse($query_url) {
    try {
      $response = \Drupal::httpClient()
        ->get($query_url);
      $data = (string) $response
        ->getBody();
      if (empty($data)) {
        return FALSE;
      }
      else {
        return Json::decode($data);
      }
    } catch (RequestException $e) {
      return FALSE;
    }
  }

  /**
   * Return a list of sites where statistics are accessible on matomo server.
   *
   * @param string $token_auth
   *   Matomo server token auth.
   *
   * @return array|string|bool
   *   Array of sites returned from Matomo reports API.
   */
  public static function getSites($token_auth) {
    $matomo_url = static::getUrl();
    if ($matomo_url) {
      return static::getResponse($matomo_url . 'index.php?module=API&method=SitesManager.getSitesWithAtLeastViewAccess&format=JSON&token_auth=' . $token_auth);
    }
    else {
      return FALSE;
    }
  }

  /**
   * Return Matomo server url.
   *
   * @return string
   *   Stored value of Matomo server URL.
   */
  public static function getUrl() {

    // Matomo Reports settings takes precedence over Matomo settings.
    $url = \Drupal::config('matomo_reports.matomoreportssettings')
      ->get('matomo_server_url');
    if ($url == '') {
      if (\Drupal::moduleHandler()
        ->moduleExists('matomo')) {

        // Get https url if available first.
        $url = \Drupal::config('matomo.settings')
          ->get('url_http');
        $url = \Drupal::config('matomo.settings')
          ->get('url_https') ? \Drupal::config('matomo.settings')
          ->get('url_https') : $url;
      }
    }
    if ($url == '') {
      \Drupal::messenger()
        ->addWarning(t('Matomo server url is missing or wrong. Please ask your administrator to check Matomo Reports configuration.'));
    }
    return $url;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MatomoData::getResponse public static function Return server request results.
MatomoData::getSites public static function Return a list of sites where statistics are accessible on matomo server.
MatomoData::getToken public static function Return matomo token auth from global or user.
MatomoData::getUrl public static function Return Matomo server url.