Commit 47474f53 authored by Michael Iseard's avatar Michael Iseard
Browse files

Fix truncating logic and using dependency injection for LoggerService

parent dd3c6fcd
......@@ -56,6 +56,10 @@ class Admin {
* @var \Kudos\Service\Vendor\MollieVendor
*/
private $mollie;
/**
* @var \Kudos\Service\LoggerService
*/
private $logger;
/**
* Initialize the class and set its properties.
......@@ -69,15 +73,17 @@ class Admin {
PaymentService $payment,
ActivatorService $activator,
Settings $settings,
MollieVendor $mollie_vendor
MollieVendor $mollie_vendor,
LoggerService $logger
) {
$this->version = $version;
$this->mapper = $mapper;
$this->twig = $twig;
$this->payment = $payment;
$this->activator = $activator;
$this->settings = $settings;
$this->mollie = $mollie_vendor;
$this->version = $version;
$this->mapper = $mapper;
$this->twig = $twig;
$this->payment = $payment;
$this->activator = $activator;
$this->settings = $settings;
$this->mollie = $mollie_vendor;
$this->logger = $logger;
}
/**
......@@ -268,7 +274,7 @@ class Admin {
wp_register_style(
'kudos-donations-public',
Assets::get_asset_url( '/public/kudos-public.css' ),
['kudos-donations-root'],
[ 'kudos-donations-root' ],
$this->version
);
......@@ -536,9 +542,9 @@ class Admin {
break;
case 'kudos_sync_payments':
$mollie = $this->mollie;
$mollie = $this->mollie;
$updated = $mollie->sync_transactions();
if($updated) {
if ( $updated ) {
new AdminNotice(
sprintf(
/* translators: %s: Number of records. */
......@@ -551,7 +557,7 @@ class Admin {
);
break;
}
new AdminNotice(__('No transactions need updating', 'kudos-donations'));
new AdminNotice( __( 'No transactions need updating', 'kudos-donations' ) );
}
do_action( 'kudos_admin_actions_extra', $action );
......@@ -564,7 +570,7 @@ class Admin {
* Length defined by LoggerService::TRUNCATE_AT const.
*/
public function truncate_log() {
LoggerService::truncate();
$this->logger->truncate();
}
/**
......
......@@ -17,11 +17,18 @@ class LoggerService extends Logger {
* @var string
*/
public const TABLE = 'kudos_log';
/**
* @var \Kudos\Helpers\WpDb|\wpdb
*/
private $wpdb;
/**
* @param \Kudos\Helpers\WpDb $wpdb
*/
public function __construct( WpDb $wpdb ) {
$this->wpdb = $wpdb;
parent::__construct(
'kudos',
[ new DatabaseHandler( $wpdb ) ],
......@@ -80,13 +87,14 @@ class LoggerService extends Logger {
*
* @return bool|int
*/
public static function truncate() {
/** @var \wpdb $wpdb */
$wpdb = new WpDb();
public function truncate() {
$wpdb = $this->wpdb;
$table = self::get_table_name();
// Get ID of the oldest row to keep.
$last_row = $wpdb->get_row( $wpdb->prepare("
SELECT `id` FROM {$table}
ORDER BY `id` DESC
LIMIT %d,1
", (self::TRUNCATE_AT - 1) ) );
......@@ -94,9 +102,8 @@ class LoggerService extends Logger {
$last_id = $last_row->id;
return $wpdb->query($wpdb->prepare("
DELETE FROM {$table}
WHERE `id` > %d
WHERE `id` < %d
", $last_id));
}
return false;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment