Skip to content

Commit 51aab73

Browse files
authored
Merge pull request #190 from amimoto-ami/devin/1759972998-debug-logger-refactoring
refactor: Extract debug logging into dedicated Debug_Logger class
2 parents 92a4606 + e90ef69 commit 51aab73

File tree

7 files changed

+371
-126
lines changed

7 files changed

+371
-126
lines changed

c3-cloudfront-clear-cache.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
* Load classes and initialize services
2525
*/
2626
function c3_init() {
27-
new C3_CloudFront_Cache_Controller\Invalidation_Service();
28-
new C3_CloudFront_Cache_Controller\Cron_Service();
27+
$debug_logger = new C3_CloudFront_Cache_Controller\WP\Debug_Logger();
28+
new C3_CloudFront_Cache_Controller\Invalidation_Service( $debug_logger );
29+
new C3_CloudFront_Cache_Controller\Cron_Service( $debug_logger );
2930
new C3_CloudFront_Cache_Controller\Settings_Service();
3031
new C3_CloudFront_Cache_Controller\Views\Settings();
3132
new C3_CloudFront_Cache_Controller\Views\Debug_Settings();

classes/AWS/CloudFront_Service.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ class CloudFront_Service {
4343
*/
4444
private $hook_service;
4545

46+
/**
47+
* Debug logger service
48+
*
49+
* @var WP\Debug_Logger
50+
*/
51+
private $debug_logger;
52+
4653
/**
4754
* Inject a external services
4855
*
@@ -57,6 +64,8 @@ function __construct( ...$args ) {
5764
$this->hook_service = $value;
5865
} elseif ( $value instanceof WP\Environment ) {
5966
$this->env = $value;
67+
} elseif ( $value instanceof WP\Debug_Logger ) {
68+
$this->debug_logger = $value;
6069
}
6170
}
6271
}
@@ -69,6 +78,9 @@ function __construct( ...$args ) {
6978
if ( ! $this->options_service ) {
7079
$this->options_service = new WP\Options_Service();
7180
}
81+
if ( ! $this->debug_logger ) {
82+
$this->debug_logger = new WP\Debug_Logger();
83+
}
7284
}
7385

7486
/**
@@ -251,11 +263,11 @@ public function create_invalidation( $params ) {
251263
$distribution_id = $params['DistributionId'];
252264
$paths = $params['InvalidationBatch']['Paths']['Items'];
253265

254-
if ( $this->hook_service->apply_filters( 'c3_log_invalidation_params', $this->get_debug_setting( Constants::DEBUG_LOG_INVALIDATION_PARAMS ) ) ) {
255-
error_log( 'C3 CloudFront Invalidation Request - Distribution ID: ' . $distribution_id );
256-
error_log( 'C3 CloudFront Invalidation Request - Paths: ' . print_r( $paths, true ) );
257-
error_log( 'C3 CloudFront Invalidation Request - Full Params: ' . print_r( $params, true ) );
258-
}
266+
$this->debug_logger->log_invalidation_request( array(
267+
'distribution_id' => $distribution_id,
268+
'paths' => $paths,
269+
'full_params' => $params,
270+
) );
259271

260272
$result = $client->create_invalidation( $distribution_id, $paths );
261273
return $result;
@@ -379,15 +391,4 @@ public function get_invalidation_details( $invalidation_id ) {
379391
}
380392
}
381393

382-
/**
383-
* Get debug setting value
384-
*
385-
* @param string $setting_key Debug setting key.
386-
* @return boolean Debug setting value.
387-
*/
388-
private function get_debug_setting( $setting_key ) {
389-
$debug_options = get_option( Constants::DEBUG_OPTION_NAME, array() );
390-
$value = isset( $debug_options[ $setting_key ] ) ? $debug_options[ $setting_key ] : false;
391-
return $value;
392-
}
393394
}

classes/Cron_Service.php

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ class Cron_Service {
3535
private $transient_service;
3636

3737
/**
38-
* Log cron register task flag
38+
* Debug logger service
3939
*
40-
* @var boolean
40+
* @var WP\Debug_Logger
4141
*/
42-
private $log_cron_register_task;
42+
private $debug_logger;
4343

4444
/**
4545
* CloudFront service
@@ -56,7 +56,8 @@ class Cron_Service {
5656
function __construct( ...$args ) {
5757
$this->hook_service = new WP\Hooks();
5858
$this->transient_service = new WP\Transient_Service();
59-
$this->cf_service = new AWS\CloudFront_Service();
59+
$this->debug_logger = null;
60+
$this->cf_service = null;
6061

6162
if ( $args && ! empty( $args ) ) {
6263
foreach ( $args as $key => $value ) {
@@ -66,17 +67,24 @@ function __construct( ...$args ) {
6667
$this->transient_service = $value;
6768
} elseif ( $value instanceof AWS\CloudFront_Service ) {
6869
$this->cf_service = $value;
70+
} elseif ( $value instanceof WP\Debug_Logger ) {
71+
$this->debug_logger = $value;
6972
}
7073
}
7174
}
75+
if ( ! $this->debug_logger ) {
76+
$this->debug_logger = new WP\Debug_Logger();
77+
}
78+
if ( ! $this->cf_service ) {
79+
$this->cf_service = new AWS\CloudFront_Service( $this->debug_logger );
80+
}
7281
$this->hook_service->add_action(
7382
'c3_cron_invalidation',
7483
array(
7584
$this,
7685
'run_schedule_invalidate',
7786
)
7887
);
79-
$this->log_cron_register_task = $this->hook_service->apply_filters( 'c3_log_cron_invalidation_task', $this->get_debug_setting( Constants::DEBUG_LOG_CRON_REGISTER_TASK ) );
8088
}
8189

8290
/**
@@ -85,61 +93,38 @@ function __construct( ...$args ) {
8593
* @return boolean
8694
*/
8795
public function run_schedule_invalidate() {
88-
if ( $this->log_cron_register_task ) {
89-
error_log( '===== C3 Invalidation cron is started ===' );
90-
}
96+
$this->debug_logger->log_cron_start();
9197
if ( $this->hook_service->apply_filters( 'c3_disabled_cron_retry', false ) ) {
92-
if ( $this->log_cron_register_task ) {
93-
error_log( '===== C3 Invalidation cron has been SKIPPED [Disabled] ===' );
94-
}
98+
$this->debug_logger->log_cron_skip( '===== C3 Invalidation cron has been SKIPPED [Disabled] ===' );
9599
return false;
96100
}
97101
$invalidation_batch = $this->transient_service->load_invalidation_query();
98-
if ( $this->log_cron_register_task ) {
99-
error_log( print_r( $invalidation_batch, true ) );
100-
}
102+
$this->debug_logger->log_invalidation_params( '', array( $invalidation_batch ) );
101103
if ( ! $invalidation_batch || empty( $invalidation_batch ) ) {
102-
if ( $this->log_cron_register_task ) {
103-
error_log( '===== C3 Invalidation cron has been SKIPPED [No Target Item] ===' );
104-
}
104+
$this->debug_logger->log_cron_skip( '===== C3 Invalidation cron has been SKIPPED [No Target Item] ===' );
105105
return false;
106106
}
107107
$distribution_id = $this->cf_service->get_distribution_id();
108108
$query = array(
109109
'DistributionId' => esc_attr( $distribution_id ),
110110
'InvalidationBatch' => $invalidation_batch,
111111
);
112-
if ( $this->log_cron_register_task ) {
113-
error_log( print_r( $query, true ) );
114-
}
112+
$this->debug_logger->log_invalidation_params( '', array( $query ) );
115113

116114
/**
117115
* Execute the invalidation.
118116
*/
119117
$result = $this->cf_service->create_invalidation( $query );
120-
if ( $this->log_cron_register_task ) {
118+
if ( $this->debug_logger->should_log_cron_operations() ) {
121119
if ( is_wp_error( $result ) ) {
122120
error_log( 'C3 Cron: Invalidation failed: ' . $result->get_error_message() );
123121
} else {
124122
error_log( 'C3 Cron: Invalidation completed successfully' );
125123
}
126124
}
127125
$this->transient_service->delete_invalidation_query();
128-
if ( $this->log_cron_register_task ) {
129-
error_log( '===== C3 Invalidation cron has been COMPLETED ===' );
130-
}
126+
$this->debug_logger->log_cron_complete();
131127
return true;
132128
}
133129

134-
/**
135-
* Get debug setting value
136-
*
137-
* @param string $setting_key Debug setting key.
138-
* @return boolean Debug setting value.
139-
*/
140-
private function get_debug_setting( $setting_key ) {
141-
$debug_options = get_option( Constants::DEBUG_OPTION_NAME, array() );
142-
$value = isset( $debug_options[ $setting_key ] ) ? $debug_options[ $setting_key ] : false;
143-
return $value;
144-
}
145130
}

classes/Invalidation_Service.php

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ class Invalidation_Service {
6464
private $invalidation_batch;
6565

6666
/**
67-
* Log invalidation parameters flag
67+
* Debug logger service.
6868
*
69-
* @var boolean
69+
* @var WP\Debug_Logger
7070
*/
71-
private $log_invalidation_params;
71+
private $debug_logger;
7272

7373
/**
7474
* Initialize the service, register WordPress hooks, and optionally inject dependencies.
@@ -88,8 +88,9 @@ function __construct( ...$args ) {
8888
$this->option_service = new WP\Options_Service();
8989
$this->invalidation_batch = new AWS\Invalidation_Batch_Service();
9090
$this->transient_service = new WP\Transient_Service();
91-
$this->cf_service = new AWS\CloudFront_Service();
9291
$this->notice = new WP\Admin_Notice();
92+
$this->debug_logger = null;
93+
$this->cf_service = null;
9394

9495
if ( $args && ! empty( $args ) ) {
9596
foreach ( $args as $key => $value ) {
@@ -105,9 +106,17 @@ function __construct( ...$args ) {
105106
$this->cf_service = $value;
106107
} elseif ( $value instanceof WP\Admin_Notice ) {
107108
$this->notice = $value;
109+
} elseif ( $value instanceof WP\Debug_Logger ) {
110+
$this->debug_logger = $value;
108111
}
109112
}
110113
}
114+
if ( ! $this->debug_logger ) {
115+
$this->debug_logger = new WP\Debug_Logger();
116+
}
117+
if ( ! $this->cf_service ) {
118+
$this->cf_service = new AWS\CloudFront_Service( $this->debug_logger );
119+
}
111120
$this->hook_service->add_action(
112121
'transition_post_status',
113122
array(
@@ -140,7 +149,6 @@ function __construct( ...$args ) {
140149
'handle_invalidation_details_ajax',
141150
)
142151
);
143-
$this->log_invalidation_params = $this->hook_service->apply_filters( 'c3_log_invalidation_params', $this->get_debug_setting( Constants::DEBUG_LOG_INVALIDATION_PARAMS ) );
144152
}
145153

146154
/**
@@ -196,34 +204,24 @@ public function invalidate_manually() {
196204
* @return boolean If true, cron has been scheduled.
197205
*/
198206
public function register_cron_event( $query ) {
199-
if ( $this->log_invalidation_params ) {
200-
error_log( '===== C3 CRON Job registration [START] ===' );
201-
}
207+
$this->debug_logger->log_cron_registration_start();
202208
if ( ! isset( $query['Paths'] ) || ! isset( $query['Paths']['Items'] ) || $query['Paths']['Items'][0] === '/*' ) {
203-
if ( $this->log_invalidation_params ) {
204-
error_log( '===== C3 CRON Job registration [SKIP | NO ITEM] ===' );
205-
}
209+
$this->debug_logger->log_cron_registration_skip( '===== C3 CRON Job registration [SKIP | NO ITEM] ===' );
206210
return false;
207211
}
208212
if ( $this->hook_service->apply_filters( 'c3_disabled_cron_retry', false ) ) {
209-
if ( $this->log_invalidation_params ) {
210-
error_log( '===== C3 CRON Job registration [SKIP | DISABLED] ===' );
211-
}
213+
$this->debug_logger->log_cron_registration_skip( '===== C3 CRON Job registration [SKIP | DISABLED] ===' );
212214
return false;
213215
}
214216
$query = $this->transient_service->save_invalidation_query( $query );
215217

216218
$interval_minutes = $this->hook_service->apply_filters( 'c3_invalidation_cron_interval', 1 );
217219
$time = time() + MINUTE_IN_SECONDS * $interval_minutes;
218-
if ( $this->log_invalidation_params ) {
219-
error_log( print_r( $query, true ) );
220-
}
220+
$this->debug_logger->log_invalidation_params( '', array( $query ) );
221221

222222
$result = wp_schedule_single_event( $time, 'c3_cron_invalidation' );
223223

224-
if ( $this->log_invalidation_params ) {
225-
error_log( '===== C3 CRON Job registration [COMPLETE] ===' );
226-
}
224+
$this->debug_logger->log_cron_registration_complete();
227225
return $result;
228226
}
229227

@@ -272,10 +270,10 @@ public function invalidate_by_query( $query, $force = false ) {
272270
return $query;
273271
}
274272

275-
if ( $this->hook_service->apply_filters( 'c3_log_invalidation_params', $this->get_debug_setting( Constants::DEBUG_LOG_INVALIDATION_PARAMS ) ) ) {
276-
error_log( 'C3 Invalidation Started - Query: ' . print_r( $query, true ) );
277-
error_log( 'C3 Invalidation Started - Force: ' . ( $force ? 'true' : 'false' ) );
278-
}
273+
$this->debug_logger->log_invalidation_request( array(
274+
'query' => $query,
275+
'force' => $force,
276+
) );
279277

280278
if ( $this->transient_service->should_regist_cron_job() && false === $force ) {
281279
/**
@@ -494,15 +492,4 @@ public function invalidate_attachment_cache( $attachment_id ) {
494492
return $this->invalidate_by_query( $query );
495493
}
496494

497-
/**
498-
* Get debug setting value
499-
*
500-
* @param string $setting_key Debug setting key.
501-
* @return boolean Debug setting value.
502-
*/
503-
private function get_debug_setting( $setting_key ) {
504-
$debug_options = get_option( Constants::DEBUG_OPTION_NAME, array() );
505-
$value = isset( $debug_options[ $setting_key ] ) ? $debug_options[ $setting_key ] : false;
506-
return $value;
507-
}
508495
}

0 commit comments

Comments
 (0)