Skip to content
Merged
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: 23 additions & 0 deletions src/Handler/Document/ConvertDocument/ConvertDocumentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@

namespace OpenDxp\Bundle\AdminBundle\Handler\Document\ConvertDocument;

use Doctrine\DBAL\Connection;
use OpenDxp\Cache\RuntimeCache;
use OpenDxp\Config;
use OpenDxp\Model\Document;
use OpenDxp\Tool;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

final class ConvertDocumentHandler
{
public function __construct(private readonly Connection $db)
{
}

public function __invoke(ConvertDocumentPayload $payload): void
{
$document = Document::getById($payload->id);
Expand All @@ -48,6 +54,7 @@ public function __invoke(ConvertDocumentPayload $payload): void
return;
}

$previousType = $document->getType();
$new = new $class;

// overwrite internal store to avoid "duplicate full path" error
Expand All @@ -69,5 +76,21 @@ public function __invoke(ConvertDocumentPayload $payload): void

$new->setType($payload->type);
$new->save();

$this->removeRowOfPreviousType($document->getId(), $previousType, $payload->type);
}

private function removeRowOfPreviousType(int $id, string $previousType, string $newType): void
{
if ($previousType === $newType) {
return;
}

$config = Config::getSystemConfiguration('documents') ?? [];
$validTable = $config['type_definitions']['map'][$previousType]['valid_table'] ?? null;

if ($validTable) {
$this->db->delete('documents_' . $validTable, ['id' => $id]);
}
}
}
Loading