Skip to content
Snippets Groups Projects
Commit 182dc55d authored by Patrick Kenny's avatar Patrick Kenny
Browse files

Issue #3446602 by ptmkenny: Provide a configuration option to enable/disable logging

parent da844e72
No related branches found
No related tags found
1 merge request!12Allow the level of logging to be configured
Pipeline #170339 failed
credentials_path: ''
logging_level: 'standard'
......@@ -5,3 +5,6 @@ firebase_php.settings:
credentials_path:
type: string
label: 'Firebase credentials path'
logging_level:
type: string
label: 'Logging level'
<?php
declare(strict_types=1);
namespace Drupal\firebase_php\Enum;
/**
* Settings values for Firebase PHP.
*/
enum FirebasePhpSettings: string {
case CredentialsPath = 'credentials_path';
case LoggingLevel = 'logging_level';
}
<?php
declare(strict_types=1);
namespace Drupal\firebase_php\Enum;
/**
* Possible values for logging level for Firebase PHP.
*/
enum LoggingLevel: string {
case Debug = 'debug';
case Standard = 'standard';
case None = 'none';
}
......@@ -6,6 +6,8 @@ namespace Drupal\firebase_php\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\firebase_php\Enum\FirebasePhpSettings;
use Drupal\firebase_php\Enum\LoggingLevel;
/**
* Configures the Firebase PHP Admin SDK.
......@@ -14,8 +16,6 @@ class FirebasePhpConfigurationForm extends ConfigFormBase {
const FORM_ID = 'firebase_php.settings';
const KEY_CREDENTIALS_PATH = 'credentials_path';
/**
* {@inheritdoc}
*/
......@@ -36,13 +36,13 @@ class FirebasePhpConfigurationForm extends ConfigFormBase {
public function buildForm(array $form, FormStateInterface $form_state): array {
$config = $this->config(self::FORM_ID);
$form['firebase'] = [
$form[self::FORM_ID] = [
'#type' => 'details',
'#title' => $this->t('Configure Firebase'),
'#open' => TRUE,
];
$form['firebase'][self::KEY_CREDENTIALS_PATH] = [
$form[self::FORM_ID][FirebasePhpSettings::CredentialsPath->value] = [
'#type' => 'textfield',
'#title' => $this->t('Credentials Path'),
'#description' => $this->t(
......@@ -51,7 +51,20 @@ class FirebasePhpConfigurationForm extends ConfigFormBase {
'%rel' => '../firebase/foobar.json',
'%str' => 'private://firebase/foobar.json',
]),
'#default_value' => $config->get(self::KEY_CREDENTIALS_PATH),
'#default_value' => $config->get(FirebasePhpSettings::CredentialsPath->value),
'#required' => TRUE,
];
$form[self::FORM_ID][FirebasePhpSettings::LoggingLevel->value] = [
'#type' => 'select',
'#title' => $this->t('Logging Level'),
'#description' => $this->t('Choose the level of detail to be logged to Drupal.'),
'#options' => [
LoggingLevel::Debug->value,
LoggingLevel::Standard->value,
LoggingLevel::None->value,
],
'#default_value' => $config->get(FirebasePhpSettings::LoggingLevel->value),
'#required' => TRUE,
];
......@@ -64,7 +77,8 @@ class FirebasePhpConfigurationForm extends ConfigFormBase {
public function submitForm(array &$form, FormStateInterface $form_state): void {
$config = $this->config(self::FORM_ID);
$config
->set(self::KEY_CREDENTIALS_PATH, $form_state->getValue(self::KEY_CREDENTIALS_PATH))
->set(FirebasePhpSettings::CredentialsPath->value, $form_state->getValue(FirebasePhpSettings::CredentialsPath->value))
->set(FirebasePhpSettings::LoggingLevel->value, $form_state->getValue(FirebasePhpSettings::LoggingLevel->value))
->save();
parent::submitForm($form, $form_state);
......@@ -75,7 +89,7 @@ class FirebasePhpConfigurationForm extends ConfigFormBase {
*/
public function validateForm(array &$form, FormStateInterface $form_state): void {
$settings = $form_state->getValues();
$file_path = $settings[self::KEY_CREDENTIALS_PATH];
$file_path = $settings[FirebasePhpSettings::CredentialsPath->value];
// Does the file exist?
if (!is_file($file_path)) {
......
......@@ -7,10 +7,11 @@ namespace Drupal\firebase_php\Service;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Session\AccountInterface;
use Drupal\firebase_php\Enum\FirebasePhpSettings;
use Drupal\firebase_php\Enum\LoggingLevel;
use Drupal\firebase_php\Exception\FirebasePhpCredentialsNotFoundException;
use Drupal\firebase_php\Exception\FirebasePhpInvalidArgumentException;
use Drupal\firebase_php\FirebasePhpMessagingServiceInterface;
use Drupal\firebase_php\Form\FirebasePhpConfigurationForm;
use Kreait\Firebase\Contract\Messaging;
use Kreait\Firebase\Factory;
use Kreait\Firebase\Messaging\ApnsConfig;
......@@ -46,18 +47,25 @@ class FirebasePhpMessagingService implements FirebasePhpMessagingServiceInterfac
protected ConfigFactory $configFactory,
) {
$config = $this->configFactory->get('firebase_php.settings');
$credentials = $config->get(FirebasePhpConfigurationForm::KEY_CREDENTIALS_PATH);
$credentials = $config->get(FirebasePhpSettings::CredentialsPath->value);
if ($credentials === NULL) {
throw new FirebasePhpCredentialsNotFoundException(
"Failed to get Firebase credentials!",
);
}
$factory = (new Factory)
->withServiceAccount($credentials)
->withHttpLogger($this->logger)
->withHttpDebugLogger($this->loggerDebug,
);
$factory = (new Factory)->withServiceAccount($credentials);
$logging_level = LoggingLevel::from($config->get(FirebasePhpSettings::LoggingLevel->value));
switch ($logging_level) {
case LoggingLevel::Debug:
$factory->withHttpDebugLogger($this->loggerDebug);
case LoggingLevel::Standard:
$factory->withHttpLogger($this->logger);
break;
}
$this->messaging = $factory->createMessaging();
$this->config = $config;
}
......@@ -76,10 +84,6 @@ class FirebasePhpMessagingService implements FirebasePhpMessagingServiceInterfac
$message = $this->createMessageFromNotificationAndAddBadge($notification, $badge_count, $deviceToken, $data, $apns_data);
$this->messaging->send($message);
$this->logger->notice('Sending push notification for user @uid to token @token.', [
'@uid' => $this->account->id(),
'@token' => $deviceToken,
]);
}
/**
......@@ -89,10 +93,6 @@ class FirebasePhpMessagingService implements FirebasePhpMessagingServiceInterfac
$message = $this->createMessageFromNotificationAndAddBadge($notification, $badge_count, NULL, $data, $apns_data, $topic);
$this->messaging->send($message);
$this->logger->notice('Sending push notification for user @uid to topic @topic.', [
'@uid' => $this->account->id(),
'@token' => $topic,
]);
}
/**
......@@ -100,26 +100,7 @@ class FirebasePhpMessagingService implements FirebasePhpMessagingServiceInterfac
*/
public function sendMessageMultipleDevices(array $deviceTokens, Notification $notification, ?int $badge_count = NULL, array $data = [], array $apns_data = []): MulticastSendReport {
$message = $this->createMessageFromNotificationAndAddBadge($notification, $badge_count, NULL, $data, $apns_data);
$report = $this->messaging->sendMulticast($message, $deviceTokens);
$log_message = 'Successful sends: ' . $report->successes()->count() . PHP_EOL;
$log_message .= 'Failed sends: ' . $report->failures()->count() . PHP_EOL;
$this->logger->notice('Push notification results for user @uid: @log_message.', [
'@uid' => $this->account->id(),
'@log_message' => $log_message,
]);
if ($report->hasFailures()) {
foreach ($report->failures()->getItems() as $failure) {
$failure_message = '';
if ($failure instanceof \Throwable) {
$failure_message = $failure->error()->getMessage();
}
$this->logger->warning('Failed to send push notification to user @uid: @failure_message', [
'@uid' => $this->account->id(),
'@failure_message' => $failure_message,
]);
}
}
return $report;
return $this->messaging->sendMulticast($message, $deviceTokens);
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment