Skip to content

Commit

Permalink
feat: Migrated to AsDoctrineListener and removed useless TablePrefi…
Browse files Browse the repository at this point in the history
…xSubscriber.php
  • Loading branch information
ambroisemaupate committed Jun 1, 2024
1 parent e01e2a6 commit ab57bbe
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 256 deletions.
102 changes: 43 additions & 59 deletions lib/Documents/src/Events/DocumentLifeCycleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace RZ\Roadiz\Documents\Events;

use Doctrine\Common\EventSubscriber;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\ORM\Event\PostRemoveEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use League\Flysystem\FilesystemException;
use League\Flysystem\FilesystemOperator;
use League\Flysystem\UnableToMoveFile;
Expand All @@ -18,36 +18,27 @@
/**
* Handle file management on document's lifecycle events.
*/
class DocumentLifeCycleSubscriber implements EventSubscriber
#[AsDoctrineListener(event: Events::postRemove)]
#[AsDoctrineListener(event: Events::preUpdate)]
final class DocumentLifeCycleSubscriber
{
private FilesystemOperator $documentsStorage;

public function __construct(FilesystemOperator $documentsStorage)
{
$this->documentsStorage = $documentsStorage;
}

/**
* {@inheritdoc}
*/
public function getSubscribedEvents(): array
public function __construct(private readonly FilesystemOperator $documentsStorage)
{
return array(
Events::postRemove,
Events::preUpdate,
);
}

/**
* @param PreUpdateEventArgs $args
* @throws FilesystemException
*/
public function preUpdate(PreUpdateEventArgs $args): void
{
$document = $args->getObject();

if (!$document instanceof DocumentInterface) {
return;
}

if (
$document instanceof DocumentInterface
&& $args->hasChangedField('filename')
$args->hasChangedField('filename')
&& is_string($args->getOldValue('filename'))
&& is_string($args->getNewValue('filename'))
&& $args->getOldValue('filename') !== ''
Expand All @@ -66,21 +57,16 @@ public function preUpdate(PreUpdateEventArgs $args): void
}
}
}
if ($document instanceof DocumentInterface && $args->hasChangedField('private')) {
if ($args->hasChangedField('private')) {
if ($document->isPrivate() === true) {
$this->makePrivate($document, $args);
$this->makePrivate($document);
} else {
$this->makePublic($document, $args);
$this->makePublic($document);
}
}
}

/**
* @param DocumentInterface $document
* @param PreUpdateEventArgs $args
* @throws FilesystemException
*/
protected function makePublic(DocumentInterface $document, PreUpdateEventArgs $args): void
private function makePublic(DocumentInterface $document): void
{
$this->validateDocument($document);
$documentPublicPath = $this->getDocumentPublicPath($document);
Expand All @@ -96,12 +82,7 @@ protected function makePublic(DocumentInterface $document, PreUpdateEventArgs $a
}
}

/**
* @param DocumentInterface $document
* @param PreUpdateEventArgs $args
* @throws FilesystemException
*/
protected function makePrivate(DocumentInterface $document, PreUpdateEventArgs $args): void
private function makePrivate(DocumentInterface $document): void
{
$this->validateDocument($document);
$documentPublicPath = $this->getDocumentPublicPath($document);
Expand All @@ -120,25 +101,28 @@ protected function makePrivate(DocumentInterface $document, PreUpdateEventArgs $
/**
* Unlink file after document has been deleted.
*
* @param LifecycleEventArgs $args
* @param PostRemoveEventArgs $args
* @throws FilesystemException
*/
public function postRemove(LifecycleEventArgs $args): void
public function postRemove(PostRemoveEventArgs $args): void
{
$document = $args->getObject();
if ($document instanceof DocumentInterface) {
try {
$this->validateDocument($document);
$document->setRawDocument(null);
$documentPath = $this->getDocumentPath($document);

if ($this->documentsStorage->fileExists($documentPath)) {
$this->documentsStorage->delete($documentPath);
}
$this->cleanFileDirectory($this->getDocumentFolderPath($document));
} catch (DocumentWithoutFileException $e) {
// Do nothing when document does not have any file on system.
if (!$document instanceof DocumentInterface) {
return;
}

try {
$this->validateDocument($document);
$document->setRawDocument(null);
$documentPath = $this->getDocumentPath($document);

if ($this->documentsStorage->fileExists($documentPath)) {
$this->documentsStorage->delete($documentPath);
}
$this->cleanFileDirectory($this->getDocumentFolderPath($document));
} catch (DocumentWithoutFileException $e) {
// Do nothing when document does not have any file on system.
}
}

Expand All @@ -149,7 +133,7 @@ public function postRemove(LifecycleEventArgs $args): void
* @return void
* @throws FilesystemException
*/
protected function cleanFileDirectory(string $documentFolderPath): void
private function cleanFileDirectory(string $documentFolderPath): void
{
if ($this->documentsStorage->directoryExists($documentFolderPath)) {
$isDirEmpty = \count($this->documentsStorage->listContents($documentFolderPath)->toArray()) <= 0;
Expand All @@ -165,7 +149,7 @@ protected function cleanFileDirectory(string $documentFolderPath): void
*
* @return string
*/
protected function getDocumentRelativePathForFilename(DocumentInterface $document, string $filename): string
private function getDocumentRelativePathForFilename(DocumentInterface $document, string $filename): string
{
$this->validateDocument($document);

Expand All @@ -178,7 +162,7 @@ protected function getDocumentRelativePathForFilename(DocumentInterface $documen
*
* @return string
*/
protected function getDocumentMountPathForFilename(DocumentInterface $document, string $filename): string
private function getDocumentMountPathForFilename(DocumentInterface $document, string $filename): string
{
if ($document->isPrivate()) {
return 'private://' . $this->getDocumentRelativePathForFilename($document, $filename);
Expand All @@ -190,7 +174,7 @@ protected function getDocumentMountPathForFilename(DocumentInterface $document,
* @param DocumentInterface $document
* @return string
*/
protected function getDocumentPath(DocumentInterface $document): string
private function getDocumentPath(DocumentInterface $document): string
{
$this->validateDocument($document);

Expand All @@ -204,7 +188,7 @@ protected function getDocumentPath(DocumentInterface $document): string
* @param DocumentInterface $document
* @return string
*/
protected function getDocumentPublicPath(DocumentInterface $document): string
private function getDocumentPublicPath(DocumentInterface $document): string
{
return 'public://' . $document->getRelativePath();
}
Expand All @@ -213,7 +197,7 @@ protected function getDocumentPublicPath(DocumentInterface $document): string
* @param DocumentInterface $document
* @return string
*/
protected function getDocumentPrivatePath(DocumentInterface $document): string
private function getDocumentPrivatePath(DocumentInterface $document): string
{
return 'private://' . $document->getRelativePath();
}
Expand All @@ -222,7 +206,7 @@ protected function getDocumentPrivatePath(DocumentInterface $document): string
* @param DocumentInterface $document
* @return string
*/
protected function getDocumentFolderPath(DocumentInterface $document): string
private function getDocumentFolderPath(DocumentInterface $document): string
{
if ($document->isPrivate()) {
return $this->getDocumentPrivateFolderPath($document);
Expand All @@ -234,7 +218,7 @@ protected function getDocumentFolderPath(DocumentInterface $document): string
* @param DocumentInterface $document
* @return string
*/
protected function getDocumentPublicFolderPath(DocumentInterface $document): string
private function getDocumentPublicFolderPath(DocumentInterface $document): string
{
return 'public://' . $document->getFolder();
}
Expand All @@ -243,7 +227,7 @@ protected function getDocumentPublicFolderPath(DocumentInterface $document): str
* @param DocumentInterface $document
* @return string
*/
protected function getDocumentPrivateFolderPath(DocumentInterface $document): string
private function getDocumentPrivateFolderPath(DocumentInterface $document): string
{
return 'private://' . $document->getFolder();
}
Expand All @@ -252,7 +236,7 @@ protected function getDocumentPrivateFolderPath(DocumentInterface $document): st
* @param DocumentInterface $document
* @throws DocumentWithoutFileException
*/
protected function validateDocument(DocumentInterface $document): void
private function validateDocument(DocumentInterface $document): void
{
if (!$document->isLocal()) {
throw new DocumentWithoutFileException($document);
Expand Down
84 changes: 34 additions & 50 deletions lib/Models/src/Core/Events/LeafEntityLifeCycleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,56 @@

namespace RZ\Roadiz\Core\Events;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\ORM\Event\PrePersistEventArgs;
use Doctrine\ORM\Events;
use RZ\Roadiz\Core\AbstractEntities\AbstractEntity;
use RZ\Roadiz\Core\AbstractEntities\LeafInterface;
use RZ\Roadiz\Core\Handlers\HandlerFactoryInterface;

/**
* @package RZ\Roadiz\Core\Events
*/
class LeafEntityLifeCycleSubscriber implements EventSubscriber
#[AsDoctrineListener(event: Events::prePersist)]
final class LeafEntityLifeCycleSubscriber
{
private HandlerFactoryInterface $handlerFactory;

public function __construct(HandlerFactoryInterface $handlerFactory)
public function __construct(private readonly HandlerFactoryInterface $handlerFactory)
{
$this->handlerFactory = $handlerFactory;
}

/**
* {@inheritdoc}
*/
public function getSubscribedEvents(): array
public function prePersist(PrePersistEventArgs $event): void
{
return [
Events::prePersist,
];
}
$entity = $event->getObject();

/**
* @param LifecycleEventArgs $event
* @return void
*/
public function prePersist(LifecycleEventArgs $event)
{
$entity = $event->getEntity();
if ($entity instanceof AbstractEntity && $entity instanceof LeafInterface) {
/*
* Automatically set position only if not manually set before.
*/
try {
$handler = $this->handlerFactory->getHandler($entity);
if (!($entity instanceof AbstractEntity) || !($entity instanceof LeafInterface)) {
return;
}

if ($entity->getPosition() === 0.0) {
/*
* Get the last index after last tag in parent
*/
$lastPosition = $handler->cleanPositions(false);
if ($lastPosition > 1 && null !== $entity->getParent()) {
/*
* Need to decrement position because current tag is already
* in parent's children collection count.
*/
$entity->setPosition($lastPosition - 1);
} else {
$entity->setPosition($lastPosition);
}
} elseif ($entity->getPosition() === 0.5) {
/*
* Automatically set position only if not manually set before.
*/
try {
$handler = $this->handlerFactory->getHandler($entity);

if ($entity->getPosition() === 0.0) {
/*
* Get the last index after last tag in parent
*/
$lastPosition = $handler->cleanPositions(false);
if ($lastPosition > 1 && null !== $entity->getParent()) {
/*
* Position is set to 0.5, so we need to
* shift all tags to the bottom.
* Need to decrement position because current tag is already
* in parent's children collection count.
*/
$handler->cleanPositions(true);
$entity->setPosition($lastPosition - 1);
} else {
$entity->setPosition($lastPosition);
}
} catch (\InvalidArgumentException $e) {
} elseif ($entity->getPosition() === 0.5) {
/*
* Position is set to 0.5, so we need to
* shift all tags to the bottom.
*/
$handler->cleanPositions(true);
}
} catch (\InvalidArgumentException $e) {
}
}
}
Loading

0 comments on commit ab57bbe

Please sign in to comment.