Skip to content

Commit

Permalink
Consolidate class naming scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-meyer committed Jan 8, 2024
1 parent c0321ab commit 647e2ac
Show file tree
Hide file tree
Showing 19 changed files with 244 additions and 220 deletions.
43 changes: 42 additions & 1 deletion src/ConsoleCommand.php → src/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Base class for all OAI-PMH console commands.
*
* @author Sebastian Meyer <[email protected]>
* @package opencultureconsulting/oai-pmh2
*/
abstract class ConsoleCommand extends Command
abstract class Console extends Command
{
/**
* Clears the result cache.
Expand Down Expand Up @@ -76,4 +78,43 @@ protected function getMemoryLimit(): int
}
return $limit;
}

/**
* Validate input arguments.
*
* @param InputInterface $input The inputs
* @param OutputInterface $output The output interface
*
* @return bool Whether the inputs validate
*/
protected function validateInput(InputInterface $input, OutputInterface $output): bool
{
/** @var array<string, string> */
$arguments = $input->getArguments();

$formats = Database::getInstance()->getMetadataFormats()->getQueryResult();
if (!in_array($arguments['format'], array_keys($formats), true)) {
$output->writeln([
'',
sprintf(
' [ERROR] Metadata format "%s" is not supported. ',
$arguments['format']
),
''
]);
return false;
}
if (!is_readable($arguments['file'])) {
$output->writeln([
'',
sprintf(
' [ERROR] File "%s" not found or not readable. ',
$arguments['file']
),
''
]);
return false;
}
return true;
}
}
47 changes: 24 additions & 23 deletions src/Console/AddRecordCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@

namespace OCC\OaiPmh2\Console;

use OCC\OaiPmh2\ConsoleCommand;
use OCC\OaiPmh2\Console;
use OCC\OaiPmh2\Database;
use OCC\OaiPmh2\Database\Format;
use OCC\OaiPmh2\Database\Record;
use OCC\OaiPmh2\Entity\Format;
use OCC\OaiPmh2\Entity\Record;
use OCC\OaiPmh2\Entity\Set;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -42,7 +43,7 @@
name: 'oai:records:add',
description: 'Add or update a record in the database'
)]
class AddRecordCommand extends ConsoleCommand
class AddRecordCommand extends Console
{
/**
* Configures the current command.
Expand Down Expand Up @@ -84,29 +85,29 @@ protected function configure(): void
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @var array<string, string> */
$arguments = $input->getArguments();
/** @var Format */
$format = Database::getInstance()->getEntityManager()->getReference(Format::class, $arguments['format']);

$content = file_get_contents($arguments['file']);
if ($content === false) {
$output->writeln([
'',
sprintf(
' [ERROR] File "%s" not found or not readable. ',
$arguments['file']
),
''
]);
if (!$this->validateInput($input, $output)) {
return Command::INVALID;
}
/** @var string */
$identifier = $input->getArgument('identifier');
/** @var Format */
$format = Database::getInstance()->getEntityManager()->getReference(Format::class, $input->getArgument('format'));
/** @var string */
$file = $input->getArgument('file');
/** @var string[] */
$sets = $input->getArgument('sets');
/** @var string */
$content = file_get_contents($file);

$record = new Record($arguments['identifier'], $format);
$record = new Record($identifier, $format);
if (trim($content) !== '') {
$record->setContent($content);
}
// TODO: Add full set support.
foreach ($sets as $set) {
/** @var Set */
$setSpec = Database::getInstance()->getEntityManager()->getReference(Set::class, $set);
$record->addSet($setSpec);
}

Database::getInstance()->addOrUpdateRecord($record);
Database::getInstance()->pruneOrphanSets();
Expand All @@ -117,8 +118,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'',
sprintf(
' [OK] Record "%s" with metadata prefix "%s" added or updated successfully! ',
$arguments['identifier'],
$arguments['format']
$identifier,
$format->getPrefix()
),
''
]);
Expand Down
57 changes: 12 additions & 45 deletions src/Console/CsvImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
namespace OCC\OaiPmh2\Console;

use DateTime;
use OCC\OaiPmh2\ConsoleCommand;
use OCC\OaiPmh2\Console;
use OCC\OaiPmh2\Database;
use OCC\OaiPmh2\Database\Format;
use OCC\OaiPmh2\Database\Record;
use OCC\OaiPmh2\Entity\Format;
use OCC\OaiPmh2\Entity\Record;
use OCC\OaiPmh2\Entity\Set;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressIndicator;
Expand All @@ -45,7 +46,7 @@
name: 'oai:records:import:csv',
description: 'Import records from a CSV file'
)]
class CsvImportCommand extends ConsoleCommand
class CsvImportCommand extends Console
{
/**
* Configures the current command.
Expand Down Expand Up @@ -93,7 +94,7 @@ function (): array {
'setColumn',
's',
InputOption::VALUE_OPTIONAL,
'Name of the CSV column which holds the records\' sets list.',
'Name of the CSV column which holds the comma-separated list of the records\' sets.',
'sets'
);
$this->addOption(
Expand Down Expand Up @@ -148,7 +149,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (strlen(trim($row[$columns['contentColumn']])) > 0) {
$record->setContent($row[$columns['contentColumn']], !$noValidation);
}
// TODO: Complete support for sets.
$sets = $row[$columns['setColumn']] ?? '';
foreach (explode(',', trim($sets)) as $set) {
/** @var Set */
$setSpec = Database::getInstance()->getEntityManager()->getReference(Set::class, $set);
$record->addSet($setSpec);
}
Database::getInstance()->addOrUpdateRecord($record, true);

++$count;
Expand Down Expand Up @@ -230,43 +236,4 @@ protected function getColumnNames(InputInterface $input, OutputInterface $output
}
return $columns;
}

/**
* Validate input arguments.
*
* @param InputInterface $input The inputs
* @param OutputInterface $output The output interface
*
* @return bool Whether the inputs validate
*/
protected function validateInput(InputInterface $input, OutputInterface $output): bool
{
/** @var array<string, string> */
$arguments = $input->getArguments();

$formats = Database::getInstance()->getMetadataFormats()->getQueryResult();
if (!in_array($arguments['format'], array_keys($formats), true)) {
$output->writeln([
'',
sprintf(
' [ERROR] Metadata format "%s" is not supported. ',
$arguments['format']
),
''
]);
return false;
}
if (!is_readable($arguments['file'])) {
$output->writeln([
'',
sprintf(
' [ERROR] File "%s" not found or not readable. ',
$arguments['file']
),
''
]);
return false;
}
return true;
}
}
8 changes: 4 additions & 4 deletions src/Console/DeleteRecordCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

namespace OCC\OaiPmh2\Console;

use OCC\OaiPmh2\ConsoleCommand;
use OCC\OaiPmh2\Console;
use OCC\OaiPmh2\Database;
use OCC\OaiPmh2\Database\Format;
use OCC\OaiPmh2\Database\Record;
use OCC\OaiPmh2\Entity\Format;
use OCC\OaiPmh2\Entity\Record;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -42,7 +42,7 @@
name: 'oai:records:delete',
description: 'Delete a record from database'
)]
class DeleteRecordCommand extends ConsoleCommand
class DeleteRecordCommand extends Console
{
/**
* Configures the current command.
Expand Down
4 changes: 2 additions & 2 deletions src/Console/PruneDeletedRecordsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
namespace OCC\OaiPmh2\Console;

use OCC\OaiPmh2\Configuration;
use OCC\OaiPmh2\ConsoleCommand;
use OCC\OaiPmh2\Console;
use OCC\OaiPmh2\Database;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand All @@ -41,7 +41,7 @@
name: 'oai:records:prune',
description: 'Prune deleted records from database'
)]
class PruneDeletedRecordsCommand extends ConsoleCommand
class PruneDeletedRecordsCommand extends Console
{
/**
* Configures the current command.
Expand Down
4 changes: 2 additions & 2 deletions src/Console/PruneResumptionTokensCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace OCC\OaiPmh2\Console;

use OCC\OaiPmh2\ConsoleCommand;
use OCC\OaiPmh2\Console;
use OCC\OaiPmh2\Database;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand All @@ -39,7 +39,7 @@
name: 'oai:tokens:prune',
description: 'Prune expired resumption tokens from database'
)]
class PruneResumptionTokensCommand extends ConsoleCommand
class PruneResumptionTokensCommand extends Console
{
/**
* Executes the current command.
Expand Down
6 changes: 3 additions & 3 deletions src/Console/UpdateFormatsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
namespace OCC\OaiPmh2\Console;

use OCC\OaiPmh2\Configuration;
use OCC\OaiPmh2\ConsoleCommand;
use OCC\OaiPmh2\Console;
use OCC\OaiPmh2\Database;
use OCC\OaiPmh2\Database\Format;
use OCC\OaiPmh2\Entity\Format;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -42,7 +42,7 @@
name: 'oai:formats:update',
description: 'Update metadata formats in database from configuration'
)]
class UpdateFormatsCommand extends ConsoleCommand
class UpdateFormatsCommand extends Console
{
/**
* Executes the current command.
Expand Down
14 changes: 7 additions & 7 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
use Doctrine\ORM\Proxy\ProxyFactory;
use Doctrine\ORM\Tools\Pagination\Paginator;
use OCC\Basics\Traits\Singleton;
use OCC\OaiPmh2\Database\Format;
use OCC\OaiPmh2\Database\Record;
use OCC\OaiPmh2\Database\Result;
use OCC\OaiPmh2\Database\Set;
use OCC\OaiPmh2\Database\Token;
use OCC\OaiPmh2\Entity\Format;
use OCC\OaiPmh2\Entity\Record;
use OCC\OaiPmh2\Entity\Set;
use OCC\OaiPmh2\Entity\Token;
use OCC\OaiPmh2\Result;
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
use Symfony\Component\Filesystem\Path;

Expand Down Expand Up @@ -485,10 +485,10 @@ private function __construct()
)
);
$configuration->setMetadataDriverImpl(
new AttributeDriver([__DIR__ . '/Database'])
new AttributeDriver([__DIR__ . '/Entity'])
);
$configuration->setProxyDir(__DIR__ . '/../var/generated');
$configuration->setProxyNamespace('OCC\OaiPmh2\Database\Proxy');
$configuration->setProxyNamespace('OCC\OaiPmh2\Entity\Proxy');
$configuration->setQueryCache(
new PhpFilesAdapter(
'Query',
Expand Down
1 change: 0 additions & 1 deletion src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use DOMNode;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;

/**
* An OAI-PMH XML response object.
Expand Down
Loading

0 comments on commit 647e2ac

Please sign in to comment.