Skip to content

Commit

Permalink
Add data view repository collection.
Browse files Browse the repository at this point in the history
  • Loading branch information
doekenorg committed Aug 13, 2024
1 parent b044fc7 commit 70583d8
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 9 deletions.
23 changes: 14 additions & 9 deletions datakit.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/

use DataKit\DataViews\DataView\ArrayDataViewRepository;
use DataKit\Plugin\DataView\WordPressDataViewRepository;
use DataKit\Plugin\DataViewPlugin;

/** If this file is called directly, abort. */
Expand All @@ -25,11 +25,16 @@
const DATAVIEW_VERSION = '0.1.0';

// Initialize the plugin.
try {
DataViewPlugin::get_instance(
new ArrayDataViewRepository(),
);
} catch ( Throwable $e ) {
// Todo: log errors somewhere.
return;
}
add_action(
'plugins_loaded',
function () {
try {
DataViewPlugin::get_instance(
new WordPressDataViewRepository(),
);
} catch ( Throwable $e ) {
// Todo: log errors somewhere.
return;
}
},
);
115 changes: 115 additions & 0 deletions src/DataView/WordPressDataViewRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace DataKit\Plugin\DataView;

use DataKit\DataViews\DataView\ArrayDataViewRepository;
use DataKit\DataViews\DataView\DataView;
use DataKit\DataViews\DataView\DataViewNotFoundException;
use DataKit\DataViews\DataView\DataViewRepository;

/**
* A DataView repository that manages multiple registered {@see DataViewRepository} instances.
*
* @since $ver$
*/
final class WordPressDataViewRepository implements DataViewRepository {
/**
* The collection of repositories.
*
* @since $ver$
* @var DataViewRepository[]
*/
private array $repositories;

/**
* Repository used to register extra DataViews.
*
* @since $ver$
* @var ArrayDataViewRepository
*/
private ArrayDataViewRepository $inner;

/**
* Creates the repository.
*
* @since $ver$
*
* @param DataViewRepository ...$repositories The collection of repositories.
*/
public function __construct( DataViewRepository ...$repositories ) {
$this->repositories = (array) apply_filters( 'datakit/repositories', $repositories );
$this->inner = new ArrayDataViewRepository();
}

/**
* Returns the registered repositories and the inner repository last.
*
* @since $ver$
*
* @return DataViewRepository[] The repositories.
*/
private function repositories(): array {
return [ ...$this->repositories, $this->inner ];
}

/**
* @inheritDoc
* @since $ver$
*/
public function all(): array {
$result = [];
foreach ( $this->repositories() as $repository ) {
$result[] = $repository->all();
}

return array_merge( [], ...$result );
}

/**
* @inheritDoc
* @since $ver$
*/
public function get( string $id ): DataView {
foreach ( $this->repositories() as $repository ) {
if ( $repository->has( $id ) ) {
return $repository->get( $id );
}
}

throw new DataViewNotFoundException();
}

/**
* @inheritDoc
* @since $ver$
*/
public function has( string $id ): bool {
foreach ( $this->repositories() as $repository ) {
if ( $repository->has( $id ) ) {
return true;
}
}

return false;
}

/**
* @inheritDoc
* @since $ver$
*/
public function save( DataView $data_view ): void {
$this->inner->save( $data_view );
}

/**
* @inheritDoc
* @since $ver$
*/
public function delete( DataView $data_view ): void {
foreach ( $this->repositories() as $repository ) {
if ( $this->has( $data_view->id() ) ) {
$repository->delete( $data_view );
}
}
}
}
46 changes: 46 additions & 0 deletions tests/DataView/WordPressDataViewRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace DataKit\Plugin\Tests\DataView;

use DataKit\DataViews\Data\ArrayDataSource;
use DataKit\DataViews\DataView\ArrayDataViewRepository;
use DataKit\DataViews\DataView\DataView;
use DataKit\DataViews\DataView\DataViewNotFoundException;
use DataKit\Plugin\DataView\WordPressDataViewRepository;
use PHPUnit\Framework\TestCase;

/**
* Unit tests for {@see WordPressDataViewRepository}
*
* @since $ver$
*/
final class WordPressDataViewRepositoryTest extends TestCase {
/**
* Test case for {@see WordPressDataViewRepository}.
*
* @since $ver$
*/
public function test_collection(): void {
$data = new ArrayDataSource( 'data', [] );
$repository_1 = new ArrayDataViewRepository( [ $table_1 = DataView::table( 'table_1', $data, [] ) ] );
$repository_2 = new ArrayDataViewRepository( [ $table_2 = DataView::table( 'table_2', $data, [] ) ] );

$wordpress_repository = new WordPressDataViewRepository( $repository_1, $repository_2 );
$wordpress_repository->save( $table_3 = DataView::table( 'table_3', $data, [] ) );

self::assertTrue( $wordpress_repository->has( 'table_1' ) );
self::assertTrue( $wordpress_repository->has( 'table_2' ) );
self::assertTrue( $wordpress_repository->has( 'table_3' ) );

self::assertFalse( $wordpress_repository->has( 'missing' ) );
self::assertEquals( $data, $wordpress_repository->get( 'table_1' )->data_source() );
self::assertSame( compact( 'table_1', 'table_2', 'table_3' ), $wordpress_repository->all() );

$wordpress_repository->delete( $table_1 );
self::assertFalse( $wordpress_repository->has( 'table_1' ) );
self::assertFalse( $repository_1->has( 'table_1' ) );

$this->expectException( DataViewNotFoundException::class );
$wordpress_repository->get( 'missing' );
}
}
7 changes: 7 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<?php

require dirname( __DIR__ ) . '/vendor/autoload.php';

// Polyfill for unit tests.
if ( ! function_exists( 'apply_filters' ) ) {
function apply_filters( ...$args ) {
return $args[1];
}
}

0 comments on commit 70583d8

Please sign in to comment.