Skip to content

Commit

Permalink
Use repository instead of query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-meyer committed Jan 4, 2024
1 parent a1e905b commit 751a0b7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 45 deletions.
9 changes: 6 additions & 3 deletions src/Console/UpdateFormatsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Validator\Exception\ValidationFailedException;

/**
* Synchronize metadata formats in database with configuration.
Expand Down Expand Up @@ -59,20 +60,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
continue;
}
}
if (Database::getInstance()->addOrUpdateMetadataFormat($prefix, $format['namespace'], $format['schema'])) {
try {
Database::getInstance()->addOrUpdateMetadataFormat($prefix, $format['namespace'], $format['schema']);
++$added;
$output->writeln([
sprintf(
' [OK] Metadata format "%s" added or updated successfully. ',
$prefix
)
]);
} else {
} catch (ValidationFailedException $exception) {
$output->writeln([
sprintf(
' [ERROR] Could not add or update metadata format "%s". ',
$prefix
)
),
$exception->getMessage()
]);
}
}
Expand Down
69 changes: 27 additions & 42 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Proxy\ProxyFactory;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Exception;
use OCC\Basics\Traits\Singleton;
use OCC\OaiPmh2\Database\Format;
use OCC\OaiPmh2\Database\Record;
Expand All @@ -41,6 +40,7 @@
use OCC\OaiPmh2\Database\Token;
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Validator\Exception\ValidationFailedException;

/**
* Handles all database shenanigans.
Expand Down Expand Up @@ -76,37 +76,29 @@ class Database
* @param string $namespace The namespace URI
* @param string $schema The schema URL
*
* @return bool Whether the format was inserted/updated successfully
* @return void
*
* @throws ValidationFailedException
*/
public function addOrUpdateMetadataFormat(string $prefix, string $namespace, string $schema): bool
public function addOrUpdateMetadataFormat(string $prefix, string $namespace, string $schema): void
{
$inDatabase = $this->getMetadataFormats()->getQueryResult();
if (in_array($prefix, array_keys($inDatabase), true)) {
$format = $this->entityManager->find(Format::class, $prefix);
if (isset($format)) {
try {
$dql = $this->entityManager->createQueryBuilder();
$dql->update(Format::class, 'format')
->set('format.namespace', ':namepsace')
->set('format.xmlSchema', ':schema')
->where($dql->expr()->eq('format.prefix', ':prefix'))
->setParameter('prefix', $prefix)
->setParameter('namespace', $namespace)
->setParameter('schema', $schema);
$query = $dql->getQuery();
$query->execute();
return true;
} catch (Exception) {
return false;
$format->setNamespace($namespace);
$format->setSchema($schema);
} catch (ValidationFailedException $exception) {
throw $exception;
}
} else {
try {
$format = new Format($prefix, $namespace, $schema);
$this->entityManager->persist($format);
$this->entityManager->flush();
return true;
} catch (Exception) {
return false;
} catch (ValidationFailedException $exception) {
throw $exception;
}
}
$this->entityManager->persist($format);
$this->entityManager->flush();
}

/**
Expand Down Expand Up @@ -183,17 +175,13 @@ public function getMetadataFormats(?string $identifier = null): Result
*/
public function getRecord(string $identifier, string $metadataPrefix): ?Record
{
$dql = $this->entityManager->createQueryBuilder();
$dql->select('record')
->from(Record::class, 'record')
->where($dql->expr()->eq('record.identifier', ':identifier'))
->andWhere($dql->expr()->eq('record.format', ':format'))
->setParameter('identifier', $identifier)
->setParameter('format', $metadataPrefix)
->setMaxResults(1);
$query = $dql->getQuery();
/** @var ?Record */
return $query->getOneOrNullResult();
return $this->entityManager->find(
Record::class,
[
'identifier' => $identifier,
'format' => $metadataPrefix
]
);
}

/**
Expand Down Expand Up @@ -365,15 +353,12 @@ public function pruneResumptionTokens(): int
*/
public function removeMetadataFormat(string $prefix): bool
{
$dql = $this->entityManager->createQueryBuilder();
$dql->delete(Format::class, 'format')
->where($dql->expr()->eq('format.prefix', ':prefix'))
->setParameter('prefix', $prefix);
$query = $dql->getQuery();
try {
$query->execute();
$format = $this->entityManager->find(Format::class, $prefix);
if (isset($format)) {
$this->entityManager->remove($format);
$this->entityManager->flush();
return true;
} catch (Exception) {
} else {
return false;
}
}
Expand Down

0 comments on commit 751a0b7

Please sign in to comment.