Skip to content

Commit 0305fca

Browse files
committed
Refactored DownloadLogService to inject service.
1 parent 47bb318 commit 0305fca

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

plugins/wpgraphql-logging/src/Admin/View/Download/DownloadLogService.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use League\Csv\Writer;
88
use WPGraphQL\Logging\Logger\Api\LogEntityInterface;
9-
use WPGraphQL\Logging\Logger\Store\LogStoreService;
9+
use WPGraphQL\Logging\Logger\Api\LogServiceInterface;
1010

1111
/**
1212
* Service for handling log downloads.
@@ -16,6 +16,14 @@
1616
* @since 0.0.1
1717
*/
1818
class DownloadLogService {
19+
/**
20+
* Constructor.
21+
*
22+
* @param \WPGraphQL\Logging\Logger\Api\LogServiceInterface $log_service The log service.
23+
*/
24+
public function __construct(protected readonly LogServiceInterface $log_service) {
25+
}
26+
1927
/**
2028
* Generates and serves a CSV file for a single log entry.
2129
*
@@ -30,9 +38,7 @@ public function generate_csv( int $log_id ): void {
3038
wp_die( esc_html__( 'Invalid log ID.', 'wpgraphql-logging' ) );
3139
}
3240

33-
34-
$log_service = LogStoreService::get_log_service();
35-
$log = $log_service->find_entity_by_id( $log_id );
41+
$log = $this->log_service->find_entity_by_id( $log_id );
3642

3743
if ( is_null( $log ) ) {
3844
wp_die( esc_html__( 'Log not found.', 'wpgraphql-logging' ) );

plugins/wpgraphql-logging/src/Admin/ViewLogsPage.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ protected function get_post_value(string $key): ?string {
245245
* Renders the list page for log entries.
246246
*/
247247
protected function render_list_page(): void {
248-
// Variable required for list template.
249248
$list_table = new ListTable( new LogsRepository() ); // @phpcs:ignore SlevomatCodingStandard.Variables.UnusedVariable.UnusedVariable
250249
$list_template = apply_filters(
251250
'wpgraphql_logging_list_template',
@@ -263,7 +262,7 @@ protected function process_log_download(): void {
263262
}
264263

265264
$log_id = isset( $_GET['log'] ) ? absint( $_GET['log'] ) : 0; // @phpcs:ignore WordPress.Security.NonceVerification.Recommended
266-
$downloader = new DownloadLogService();
265+
$downloader = new DownloadLogService( $this->get_log_service() );
267266
$downloader->generate_csv( $log_id );
268267
}
269268

plugins/wpgraphql-logging/tests/wpunit/Admin/View/Download/DownloadLogServiceTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
class DownloadLogServiceTest extends WPTestCase {
2222

2323
private DownloadLogService $service;
24-
private WordPressDatabaseLogService $repository;
24+
private WordPressDatabaseLogService $log_service;
2525

2626
protected array $fixture = [
2727
'channel' => 'wpgraphql_logging',
@@ -54,8 +54,8 @@ class DownloadLogServiceTest extends WPTestCase {
5454

5555
public function setUp(): void {
5656
parent::setUp();
57-
$this->service = new DownloadLogService();
58-
$this->repository = new WordPressDatabaseLogService();
57+
$this->log_service = new WordPressDatabaseLogService();
58+
$this->service = new DownloadLogService($this->log_service);
5959
}
6060

6161
public function set_as_admin(): void {
@@ -107,11 +107,11 @@ public function test_generate_csv_returns_valid_csv(): void {
107107
$entity->shouldReceive('get_extra')->andReturn($this->fixture['extra']);
108108

109109
// Mock the repository to return our mocked entity
110-
$this->repository = \Mockery::mock(WordPressDatabaseLogService::class);
111-
$this->repository->shouldReceive('find_entity_by_id')->with(123)->andReturn($entity);
110+
$this->log_service = \Mockery::mock(WordPressDatabaseLogService::class);
111+
$this->log_service->shouldReceive('find_entity_by_id')->with(123)->andReturn($entity);
112112

113113
// Inject the mocked repository into the service
114-
$this->service = new DownloadLogService($this->repository);
114+
$this->service = new DownloadLogService($this->log_service);
115115

116116
$log_id = 123;
117117

0 commit comments

Comments
 (0)