MatomoData.php in Matomo Reports 8
File
src/MatomoData.php
View source
<?php
namespace Drupal\matomo_reports;
use Drupal\Component\Utility\Html;
use Drupal\Component\Serialization\Json;
class MatomoData {
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);
}
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;
}
}
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;
}
}
public static function getUrl() {
$url = \Drupal::config('matomo_reports.matomoreportssettings')
->get('matomo_server_url');
if ($url == '') {
if (\Drupal::moduleHandler()
->moduleExists('matomo')) {
$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;
}
}
Classes
Name |
Description |
MatomoData |
Utility class for data retrieved by reports. |