Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor out DatastoreResource class #4372

Draft
wants to merge 3 commits into
base: 2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions modules/common/src/DataResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,8 @@ public function __construct($file_path, $mimeType, $perspective = self::DEFAULT_
* @return \Drupal\common\DataResource
* DataResource object.
*
* @deprecated Use DataResource::createFromEntity() instead.
*
* @see self::createFromEntity()
* @deprecated in dkan:2.17.1 and is removed from dkan:2.20.0. Use DataResource::createFromEntity() instead.
* @see https://github.com/GetDKAN/dkan/pull/4027
*/
public static function createFromRecord(object $record): DataResource {
$resource = new static($record->filePath, $record->mimeType, $record->perspective);
Expand Down Expand Up @@ -236,6 +235,8 @@ public function changeMimeType($newMimeType) {
*
* @return \Drupal\datastore\DatastoreResource
* Datastore Resource.
*
* @deprecated
*/
public function getDatastoreResource(): DatastoreResource {
return new DatastoreResource(
Expand All @@ -253,10 +254,16 @@ public function getIdentifier() {
}

/**
* Getter.
* Get the resource's filepath.
*
* @param bool|null $resolve
* Whether to resolve the URL host tokens in the file path.
*
* @return string
* The file path.
*/
public function getFilePath() {
return $this->filePath;
public function getFilePath(?bool $resolve = FALSE):string {
return $resolve ? UrlHostTokenResolver::resolve($this->getFilePath()) : $this->filePath;
}

/**
Expand Down Expand Up @@ -324,6 +331,8 @@ public function getUniqueIdentifierNoPerspective(): string {

/**
* Retrieve datastore table name for resource.
*
* @deprecated
*/
public function getTableName() {
return 'datastore_' . md5($this->getUniqueIdentifier());
Expand All @@ -333,7 +342,7 @@ public function getTableName() {
* {@inheritDoc}
*/
#[\ReturnTypeWillChange]
public function jsonSerialize() {
public function jsonSerialize(): mixed {
return $this->serialize();
}

Expand Down
15 changes: 0 additions & 15 deletions modules/common/tests/src/Unit/DataResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,6 @@
*/
class DataResourceTest extends TestCase {

/**
* Test getTableName().
*/
public function testGetTableName() {

$resource = new DataResource(
'/foo/bar',
'txt',
DataResource::DEFAULT_SOURCE_PERSPECTIVE
);
$tableName = $resource->getTableName();

$this->assertStringStartsWith('datastore_', $tableName);
}

/**
* Test getFolder().
*/
Expand Down
1 change: 1 addition & 0 deletions modules/datastore/datastore.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ services:
- '@dkan.datastore.data_dictionary.alter_table_query_builder.mysql'
- '@dkan.metastore.service'
- '@dkan.metastore.data_dictionary_discovery'
- '@dkan.datastore.database_table_factory'
tags:
- { name: resource_processor, priority: 25 }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ protected function runIt() {
}

// Attempt to resolve resource file name from file path.
if (($file_path = \Drupal::service('file_system')->realpath($this->resource->getFilePath())) === FALSE) {
return $this->setResultError(sprintf('Unable to resolve file name "%s" for resource with identifier "%s".', $this->resource->getFilePath(), $this->resource->getId()));
if (($file_path = \Drupal::service('file_system')->realpath($this->resource->getFilePath(TRUE))) === FALSE) {
return $this->setResultError(sprintf(
'Unable to resolve file name "%s" for resource with identifier "%s".',
$this->resource->getFilePath(TRUE),
$this->resource->getUniqueIdentifier())
);
}

$size = @filesize($file_path);
Expand Down
9 changes: 7 additions & 2 deletions modules/datastore/src/DatastoreResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,26 @@ public function __construct($id, $file_path, $mime_type) {

/**
* Get the resource ID.
*
* Note: duplicates Drupal\common\DataResource::getUniqueIdentifier().
*/
public function getId(): string {
return $this->id;
}

/**
* Get the file path.
*
* Note: duplicates Drupal\common\DataResource::getFilePath(TRUE).
*/
public function getFilePath(): string {
return $this->filePath;
}

/**
* Get the mimeType.
*
* Note: duplicates Drupal\common\DataResource::getMimeType().
*/
public function getMimeType(): string {
return $this->mimeType;
Expand All @@ -65,8 +71,7 @@ public function getMimeType(): string {
/**
* {@inheritdoc}
*/
#[\ReturnTypeWillChange]
public function jsonSerialize() {
public function jsonSerialize(): mixed {
return (object) [
'filePath' => $this->getFilePath(),
'id' => $this->getId(),
Expand Down
6 changes: 3 additions & 3 deletions modules/datastore/src/Plugin/QueueWorker/ImportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class ImportJob extends AbstractPersistentJob {
/**
* Datastore resource.
*
* @var \Drupal\datastore\DatastoreResource
* @var \Drupal\common\DataResource
*/
protected $resource;

Expand All @@ -105,7 +105,7 @@ class ImportJob extends AbstractPersistentJob {
* @param array|null $config
* Configuration options.
*/
protected function __construct(string $identifier, $storage, array $config = NULL) {
protected function __construct(string $identifier, $storage, ?array $config = NULL) {
parent::__construct($identifier, $storage, $config);

$this->dataStorage = $config['storage'];
Expand Down Expand Up @@ -195,7 +195,7 @@ public function getStorage() {
* {@inheritdoc}
*/
protected function runIt() {
$filename = $this->resource->getFilePath();
$filename = $this->resource->getFilePath(TRUE);
$size = @filesize($filename);
if (!$size) {
return $this->setResultError("Can't get size from file {$filename}");
Expand Down
17 changes: 8 additions & 9 deletions modules/datastore/src/Service/ImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,9 @@ public function import() {
$data_resource = $this->getResource();

if ($result->getStatus() === Result::ERROR) {
$datastore_resource = $data_resource->getDatastoreResource();
$this->logger->error('Error importing resource id:%id path:%path message:%message', [
'%id' => $datastore_resource->getId(),
'%path' => $datastore_resource->getFilePath(),
'%id' => $data_resource->getUniqueIdentifier(),
'%path' => $data_resource->getFilePath(TRUE),
'%message' => $result->getError(),
]);
}
Expand Down Expand Up @@ -188,20 +187,20 @@ public function getImporter(): ImportJob {
if ($this->importJob ?? FALSE) {
return $this->importJob;
}
$datastore_resource = $this->getResource()->getDatastoreResource();
$data_resource = $this->getResource();

$delimiter = ",";
if ($datastore_resource->getMimeType() == 'text/tab-separated-values') {
if ($data_resource->getMimeType() == 'text/tab-separated-values') {
$delimiter = "\t";
}

$this->importJob = call_user_func([$this->importerClass, 'get'],
$datastore_resource->getId(),
$data_resource->getUniqueIdentifier(),
$this->importJobStoreFactory->getInstance(),
[
"storage" => $this->getStorage(),
"parser" => $this->getNonRecordingParser($delimiter),
"resource" => $datastore_resource,
"resource" => $data_resource,
]
);

Expand Down Expand Up @@ -245,8 +244,8 @@ private function getNonRecordingParser(string $delimiter) : Csv {
* DatabaseTable storage object.
*/
public function getStorage(): DatabaseTable {
$datastore_resource = $this->getResource()->getDatastoreResource();
return $this->databaseTableFactory->getInstance($datastore_resource->getId(), ['resource' => $datastore_resource]);
$data_resource = $this->getResource();
return $this->databaseTableFactory->getInstance($data_resource->getUniqueIdentifier(), ['resource' => $data_resource]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\common\DataResource;
use Drupal\datastore\DataDictionary\AlterTableQueryBuilderInterface;
use Drupal\datastore\Service\ResourceProcessorInterface;
use Drupal\datastore\Storage\DatabaseTableFactory;
use Drupal\metastore\MetastoreService;
use Drupal\metastore\DataDictionary\DataDictionaryDiscoveryInterface;

Expand Down Expand Up @@ -43,6 +44,13 @@ class DictionaryEnforcer implements ResourceProcessorInterface {
*/
protected $resourceMapper;

/**
* Database table factory service.
*
* @var \Drupal\datastore\Storage\DatabaseTableFactory
*/
protected $databaseTableFactory;

/**
* Constructs a \Drupal\Component\Plugin\PluginBase object.
*
Expand All @@ -56,11 +64,13 @@ class DictionaryEnforcer implements ResourceProcessorInterface {
public function __construct(
AlterTableQueryBuilderInterface $alter_table_query_builder,
MetastoreService $metastore,
DataDictionaryDiscoveryInterface $data_dictionary_discovery
DataDictionaryDiscoveryInterface $data_dictionary_discovery,
DatabaseTableFactory $table_factory,
) {
$this->metastore = $metastore;
$this->dataDictionaryDiscovery = $data_dictionary_discovery;
$this->alterTableQueryBuilder = $alter_table_query_builder;
$this->databaseTableFactory = $table_factory;
}

/**
Expand All @@ -78,7 +88,8 @@ public function process(DataResource $resource): void {
// Get data-dictionary for the given resource.
$dictionary = $this->getDataDictionaryForResource($resource);
// Retrieve name of datastore table for resource.
$datastore_table = $resource->getTableName();
$table = $this->databaseTableFactory->getInstance('', ['resource' => $resource]);
$datastore_table = $table->getTableName();

$this->applyDictionary($dictionary, $datastore_table);
}
Expand Down
14 changes: 7 additions & 7 deletions modules/datastore/src/Storage/DatabaseTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Drupal\datastore\Storage;

use Drupal\Core\Database\Connection;
use Drupal\common\DataResource;
use Drupal\common\Storage\AbstractDatabaseTable;
use Drupal\datastore\DatastoreResource;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -17,7 +17,7 @@ class DatabaseTable extends AbstractDatabaseTable implements \JsonSerializable {
/**
* Datastore resource object.
*
* @var \Drupal\datastore\DatastoreResource
* @var \Drupal\common\DataResource
*/
private $resource;

Expand All @@ -31,15 +31,15 @@ class DatabaseTable extends AbstractDatabaseTable implements \JsonSerializable {
*
* @param \Drupal\Core\Database\Connection $connection
* Drupal database connection object.
* @param \Drupal\datastore\DatastoreResource $resource
* @param \Drupal\common\DataResource $resource
* A resource.
* @param \Psr\Log\LoggerInterface $loggerChannel
* DKAN logger channel service.
*/
public function __construct(
Connection $connection,
DatastoreResource $resource,
LoggerInterface $loggerChannel
DataResource $resource,
LoggerInterface $loggerChannel,
) {
// Set resource before calling the parent constructor. The parent calls
// getTableName which we implement and needs the resource to operate.
Expand Down Expand Up @@ -76,7 +76,7 @@ public function getSummary() {
* {@inheritdoc}
*/
#[\ReturnTypeWillChange]
public function jsonSerialize() {
public function jsonSerialize(): mixed {
return (object) ['resource' => $this->resource];
}

Expand All @@ -88,7 +88,7 @@ public function jsonSerialize() {
*/
public function getTableName() {
if ($this->resource) {
return 'datastore_' . $this->resource->getId();
return 'datastore_' . md5($this->resource->getUniqueIdentifier());
}
return 'datastore_does_not_exist';
}
Expand Down
2 changes: 1 addition & 1 deletion modules/datastore/src/Storage/QueryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(DatastoreQuery $datastoreQuery, array $storageMap) {
/**
* Static factory create method.
*
* @param Drupal\datastore\Service\DatastoreQuery $datastoreQuery
* @param \Drupal\datastore\Service\DatastoreQuery $datastoreQuery
* Datastore query request object.
* @param array $storageMap
* Storage map array.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\Tests\datastore\Unit\Controller;

use Drupal\common\DataResource;
use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\Config\ConfigFactoryInterface;
Expand Down Expand Up @@ -470,7 +471,7 @@ public function mockDatastoreTable($connection, $id, $csvFile, $fields) {

$storage = new SqliteDatabaseTable(
$connection,
new DatastoreResource($id, "data-$id.csv", "text/csv"),
new DataResource("data-$id.csv", "text/csv"),
$this->createStub(LoggerInterface::class)
);
$storage->setSchema([
Expand Down