Skip to content

Commit

Permalink
Merge branch feature/doctrinelisteners into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed Jun 1, 2024
1 parent 381451b commit 55879e1
Showing 1 changed file with 43 additions and 59 deletions.
102 changes: 43 additions & 59 deletions 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)]

Check failure on line 21 in src/Events/DocumentLifeCycleSubscriber.php

View workflow job for this annotation

GitHub Actions / run-tests (8.1)

Attribute class Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener does not exist.

Check failure on line 21 in src/Events/DocumentLifeCycleSubscriber.php

View workflow job for this annotation

GitHub Actions / run-tests (8.2)

Attribute class Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener does not exist.

Check failure on line 21 in src/Events/DocumentLifeCycleSubscriber.php

View workflow job for this annotation

GitHub Actions / run-tests (8.3)

Attribute class Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener does not exist.
#[AsDoctrineListener(event: Events::preUpdate)]

Check failure on line 22 in src/Events/DocumentLifeCycleSubscriber.php

View workflow job for this annotation

GitHub Actions / run-tests (8.1)

Attribute class Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener does not exist.

Check failure on line 22 in src/Events/DocumentLifeCycleSubscriber.php

View workflow job for this annotation

GitHub Actions / run-tests (8.2)

Attribute class Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener does not exist.

Check failure on line 22 in src/Events/DocumentLifeCycleSubscriber.php

View workflow job for this annotation

GitHub Actions / run-tests (8.3)

Attribute class Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener does not exist.
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

0 comments on commit 55879e1

Please sign in to comment.