Use repository instead of query builder

This commit is contained in:
Sebastian Meyer 2024-01-04 05:21:33 +01:00
parent a1e905ba93
commit 751a0b7b05
2 changed files with 33 additions and 45 deletions

View File

@ -31,6 +31,7 @@ use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Validator\Exception\ValidationFailedException;
/** /**
* Synchronize metadata formats in database with configuration. * Synchronize metadata formats in database with configuration.
@ -59,7 +60,8 @@ class UpdateFormatsCommand extends Command
continue; continue;
} }
} }
if (Database::getInstance()->addOrUpdateMetadataFormat($prefix, $format['namespace'], $format['schema'])) { try {
Database::getInstance()->addOrUpdateMetadataFormat($prefix, $format['namespace'], $format['schema']);
++$added; ++$added;
$output->writeln([ $output->writeln([
sprintf( sprintf(
@ -67,12 +69,13 @@ class UpdateFormatsCommand extends Command
$prefix $prefix
) )
]); ]);
} else { } catch (ValidationFailedException $exception) {
$output->writeln([ $output->writeln([
sprintf( sprintf(
' [ERROR] Could not add or update metadata format "%s". ', ' [ERROR] Could not add or update metadata format "%s". ',
$prefix $prefix
) ),
$exception->getMessage()
]); ]);
} }
} }

View File

@ -32,7 +32,6 @@ use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AttributeDriver; use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Proxy\ProxyFactory; use Doctrine\ORM\Proxy\ProxyFactory;
use Doctrine\ORM\Tools\Pagination\Paginator; use Doctrine\ORM\Tools\Pagination\Paginator;
use Exception;
use OCC\Basics\Traits\Singleton; use OCC\Basics\Traits\Singleton;
use OCC\OaiPmh2\Database\Format; use OCC\OaiPmh2\Database\Format;
use OCC\OaiPmh2\Database\Record; use OCC\OaiPmh2\Database\Record;
@ -41,6 +40,7 @@ use OCC\OaiPmh2\Database\Set;
use OCC\OaiPmh2\Database\Token; use OCC\OaiPmh2\Database\Token;
use Symfony\Component\Cache\Adapter\PhpFilesAdapter; use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
use Symfony\Component\Filesystem\Path; use Symfony\Component\Filesystem\Path;
use Symfony\Component\Validator\Exception\ValidationFailedException;
/** /**
* Handles all database shenanigans. * Handles all database shenanigans.
@ -76,37 +76,29 @@ class Database
* @param string $namespace The namespace URI * @param string $namespace The namespace URI
* @param string $schema The schema URL * @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(); $format = $this->entityManager->find(Format::class, $prefix);
if (in_array($prefix, array_keys($inDatabase), true)) { if (isset($format)) {
try { try {
$dql = $this->entityManager->createQueryBuilder(); $format->setNamespace($namespace);
$dql->update(Format::class, 'format') $format->setSchema($schema);
->set('format.namespace', ':namepsace') } catch (ValidationFailedException $exception) {
->set('format.xmlSchema', ':schema') throw $exception;
->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;
} }
} else { } else {
try { try {
$format = new Format($prefix, $namespace, $schema); $format = new Format($prefix, $namespace, $schema);
} catch (ValidationFailedException $exception) {
throw $exception;
}
}
$this->entityManager->persist($format); $this->entityManager->persist($format);
$this->entityManager->flush(); $this->entityManager->flush();
return true;
} catch (Exception) {
return false;
}
}
} }
/** /**
@ -183,17 +175,13 @@ class Database
*/ */
public function getRecord(string $identifier, string $metadataPrefix): ?Record public function getRecord(string $identifier, string $metadataPrefix): ?Record
{ {
$dql = $this->entityManager->createQueryBuilder(); return $this->entityManager->find(
$dql->select('record') Record::class,
->from(Record::class, 'record') [
->where($dql->expr()->eq('record.identifier', ':identifier')) 'identifier' => $identifier,
->andWhere($dql->expr()->eq('record.format', ':format')) 'format' => $metadataPrefix
->setParameter('identifier', $identifier) ]
->setParameter('format', $metadataPrefix) );
->setMaxResults(1);
$query = $dql->getQuery();
/** @var ?Record */
return $query->getOneOrNullResult();
} }
/** /**
@ -365,15 +353,12 @@ class Database
*/ */
public function removeMetadataFormat(string $prefix): bool public function removeMetadataFormat(string $prefix): bool
{ {
$dql = $this->entityManager->createQueryBuilder(); $format = $this->entityManager->find(Format::class, $prefix);
$dql->delete(Format::class, 'format') if (isset($format)) {
->where($dql->expr()->eq('format.prefix', ':prefix')) $this->entityManager->remove($format);
->setParameter('prefix', $prefix); $this->entityManager->flush();
$query = $dql->getQuery();
try {
$query->execute();
return true; return true;
} catch (Exception) { } else {
return false; return false;
} }
} }