Skip to content

Commit

Permalink
Refactor files for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-meyer committed Jan 7, 2024
1 parent 08a5738 commit c50f296
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 159 deletions.
2 changes: 1 addition & 1 deletion src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected function loadConfigFile(): array
if ($violations->count() > 0) {
throw new ValidationFailedException(null, $violations);
}
/** @var array<TKey, TValue> $config */
/** @var array<TKey, TValue> */
return $config;
}

Expand Down
5 changes: 4 additions & 1 deletion src/Console/AddRecordCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace OCC\OaiPmh2\Console;

use OCC\OaiPmh2\ConsoleCommand;
use OCC\OaiPmh2\Database;
use OCC\OaiPmh2\Database\Format;
use OCC\OaiPmh2\Database\Record;
Expand All @@ -41,7 +42,7 @@
name: 'oai:records:add',
description: 'Add or update a record in the database'
)]
class AddRecordCommand extends Command
class AddRecordCommand extends ConsoleCommand
{
/**
* Configures the current command.
Expand Down Expand Up @@ -110,6 +111,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
Database::getInstance()->addOrUpdateRecord($record);
Database::getInstance()->pruneOrphanSets();

$this->clearResultCache();

$output->writeln([
'',
sprintf(
Expand Down
40 changes: 9 additions & 31 deletions src/Console/CsvImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace OCC\OaiPmh2\Console;

use DateTime;
use OCC\OaiPmh2\ConsoleCommand;
use OCC\OaiPmh2\Database;
use OCC\OaiPmh2\Database\Format;
use OCC\OaiPmh2\Database\Record;
Expand All @@ -44,7 +45,7 @@
name: 'oai:records:import:csv',
description: 'Import records from a CSV file'
)]
class CsvImportCommand extends Command
class CsvImportCommand extends ConsoleCommand
{
/**
* Configures the current command.
Expand Down Expand Up @@ -99,8 +100,7 @@ function (): array {
'noValidation',
null,
InputOption::VALUE_NONE,
'Omit content validation (improves performance for large record sets).',
false
'Omit content validation (improves performance for large record sets).'
);
parent::configure();
}
Expand Down Expand Up @@ -135,7 +135,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$count = 0;
$progressIndicator = new ProgressIndicator($output, 'verbose', 200, ['', '', '', '', '', '', '', '']);
$progressIndicator = new ProgressIndicator($output, null, 100, ['', '', '', '', '', '', '', '']);
$progressIndicator->start('Importing...');

while ($row = fgetcsv($file)) {
Expand All @@ -153,10 +153,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int

++$count;
$progressIndicator->advance();
$progressIndicator->setMessage((string) $count . ' done.');
$progressIndicator->setMessage('Importing... ' . (string) $count . ' records done.');

// Flush to database if memory usage reaches 90% of available limit.
if ((memory_get_usage() / $memoryLimit) > 0.9) {
// Flush to database if memory usage reaches 30% of available limit.
if ((memory_get_usage() / $memoryLimit) > 0.3) {
Database::getInstance()->flush([Record::class]);
}
}
Expand All @@ -167,6 +167,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int

fclose($file);

$this->clearResultCache();

$output->writeln([
'',
sprintf(
Expand Down Expand Up @@ -229,30 +231,6 @@ protected function getColumnNames(InputInterface $input, OutputInterface $output
return $columns;
}

/**
* Get the PHP memory limit in bytes.
*
* @return int The memory limit in bytes or -1 if unlimited
*/
protected function getMemoryLimit(): int
{
$ini = trim(ini_get('memory_limit'));
$limit = (int) $ini;
$unit = strtolower($ini[strlen($ini)-1]);
switch($unit) {
case 'g':
$limit *= 1024;
case 'm':
$limit *= 1024;
case 'k':
$limit *= 1024;
}
if ($limit < 0) {
return -1;
}
return $limit;
}

/**
* Validate input arguments.
*
Expand Down
5 changes: 3 additions & 2 deletions src/Console/DeleteRecordCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace OCC\OaiPmh2\Console;

use OCC\OaiPmh2\Configuration;
use OCC\OaiPmh2\ConsoleCommand;
use OCC\OaiPmh2\Database;
use OCC\OaiPmh2\Database\Format;
use OCC\OaiPmh2\Database\Record;
Expand All @@ -42,7 +42,7 @@
name: 'oai:records:delete',
description: 'Delete a record from database'
)]
class DeleteRecordCommand extends Command
class DeleteRecordCommand extends ConsoleCommand
{
/**
* Configures the current command.
Expand Down Expand Up @@ -89,6 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

if (isset($record)) {
Database::getInstance()->deleteRecord($record);
$this->clearResultCache();
$output->writeln([
'',
sprintf(
Expand Down
4 changes: 3 additions & 1 deletion src/Console/PruneDeletedRecordsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace OCC\OaiPmh2\Console;

use OCC\OaiPmh2\Configuration;
use OCC\OaiPmh2\ConsoleCommand;
use OCC\OaiPmh2\Database;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand All @@ -40,7 +41,7 @@
name: 'oai:records:prune',
description: 'Prune deleted records from database'
)]
class PruneDeletedRecordsCommand extends Command
class PruneDeletedRecordsCommand extends ConsoleCommand
{
/**
* Configures the current command.
Expand Down Expand Up @@ -75,6 +76,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
or ($policy === 'transient' && $forced)
) {
$deleted = Database::getInstance()->pruneDeletedRecords();
$this->clearResultCache();
$output->writeln([
'',
sprintf(
Expand Down
3 changes: 2 additions & 1 deletion src/Console/PruneResumptionTokensCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace OCC\OaiPmh2\Console;

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

use OCC\OaiPmh2\Configuration;
use OCC\OaiPmh2\ConsoleCommand;
use OCC\OaiPmh2\Database;
use OCC\OaiPmh2\Database\Format;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Attribute\AsCommand;
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;
use Symfony\Component\Validator\Exception\ValidationFailedException;

Expand All @@ -44,7 +42,7 @@
name: 'oai:formats:update',
description: 'Update metadata formats in database from configuration'
)]
class UpdateFormatsCommand extends Command
class UpdateFormatsCommand extends ConsoleCommand
{
/**
* Executes the current command.
Expand Down Expand Up @@ -103,15 +101,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
]);
}
}
/** @var Application */
$app = $this->getApplication();
$app->doRun(
new ArrayInput([
'command' => 'orm:clear-cache:result',
'--flush' => true
]),
new NullOutput()
);
$this->clearResultCache();
$currentFormats = array_keys(Database::getInstance()->getMetadataFormats()->getQueryResult());
if (count($currentFormats) > 0) {
$output->writeln(
Expand Down
79 changes: 79 additions & 0 deletions src/ConsoleCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/**
* OAI-PMH 2.0 Data Provider
* Copyright (C) 2023 Sebastian Meyer <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace OCC\OaiPmh2;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;

/**
* Base class for all OAI-PMH console commands.
*
* @author Sebastian Meyer <[email protected]>
* @package opencultureconsulting/oai-pmh2
*/
abstract class ConsoleCommand extends Command
{
/**
* Clears the result cache.
*
* @return void
*/
protected function clearResultCache(): void
{
/** @var Application */
$app = $this->getApplication();
$app->doRun(
new ArrayInput([
'command' => 'orm:clear-cache:result',
'--flush' => true
]),
new NullOutput()
);
}

/**
* Gets the PHP memory limit in bytes.
*
* @return int The memory limit in bytes or -1 if unlimited
*/
protected function getMemoryLimit(): int
{
$ini = trim(ini_get('memory_limit'));
$limit = (int) $ini;
$unit = strtolower($ini[strlen($ini)-1]);
switch($unit) {
case 'g':
$limit *= 1024;
case 'm':
$limit *= 1024;
case 'k':
$limit *= 1024;
}
if ($limit < 0) {
return -1;
}
return $limit;
}
}
Loading

0 comments on commit c50f296

Please sign in to comment.