diff --git a/architecture-baseline.json b/architecture-baseline.json index 589f339..fe51488 100644 --- a/architecture-baseline.json +++ b/architecture-baseline.json @@ -1,128 +1 @@ -[ - { - "fileName": "src/Spryker/Service/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php", - "description": "Bridges: The bridge interface has incorrect method signature for `read()`. Missed return type. That violates the rule \"All bridge interface methods must have exactly the same or more strict signature as their parent\"", - "rule": "BridgeMethodsInterfaceRule", - "ruleset": "Spryker", - "priority": "2" - }, - { - "fileName": "src/Spryker/Service/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php", - "description": "Bridges: The bridge interface has incorrect method signature for `getMimeType()`. Missed return type. That violates the rule \"All bridge interface methods must have exactly the same or more strict signature as their parent\"", - "rule": "BridgeMethodsInterfaceRule", - "ruleset": "Spryker", - "priority": "2" - }, - { - "fileName": "src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php", - "description": "Bridges: The bridge interface has incorrect method signature for `getMimeType()`. Missed return type. That violates the rule \"All bridge interface methods must have exactly the same or more strict signature as their parent\"", - "rule": "BridgeMethodsInterfaceRule", - "ruleset": "Spryker", - "priority": "2" - }, - { - "fileName": "src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php", - "description": "Bridges: The bridge interface has incorrect method signature for `getTimestamp()`. Missed return type. That violates the rule \"All bridge interface methods must have exactly the same or more strict signature as their parent\"", - "rule": "BridgeMethodsInterfaceRule", - "ruleset": "Spryker", - "priority": "2" - }, - { - "fileName": "src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php", - "description": "Bridges: The bridge interface has incorrect method signature for `getSize()`. Missed return type. That violates the rule \"All bridge interface methods must have exactly the same or more strict signature as their parent\"", - "rule": "BridgeMethodsInterfaceRule", - "ruleset": "Spryker", - "priority": "2" - }, - { - "fileName": "src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php", - "description": "Bridges: The bridge interface has incorrect method signature for `isPrivate()`. Missed return type. That violates the rule \"All bridge interface methods must have exactly the same or more strict signature as their parent\"", - "rule": "BridgeMethodsInterfaceRule", - "ruleset": "Spryker", - "priority": "2" - }, - { - "fileName": "src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php", - "description": "Bridges: The bridge interface has incorrect method signature for `read()`. Missed return type. That violates the rule \"All bridge interface methods must have exactly the same or more strict signature as their parent\"", - "rule": "BridgeMethodsInterfaceRule", - "ruleset": "Spryker", - "priority": "2" - }, - { - "fileName": "src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php", - "description": "Bridges: The bridge interface has incorrect method signature for `listContents()`. Missed return type. That violates the rule \"All bridge interface methods must have exactly the same or more strict signature as their parent\"", - "rule": "BridgeMethodsInterfaceRule", - "ruleset": "Spryker", - "priority": "2" - }, - { - "fileName": "src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php", - "description": "Bridges: The bridge interface has incorrect method signature for `has()`. Missed return type. That violates the rule \"All bridge interface methods must have exactly the same or more strict signature as their parent\"", - "rule": "BridgeMethodsInterfaceRule", - "ruleset": "Spryker", - "priority": "2" - }, - { - "fileName": "src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php", - "description": "Bridges: The bridge interface has incorrect method signature for `markAsPrivate()`. Missed return type. That violates the rule \"All bridge interface methods must have exactly the same or more strict signature as their parent\"", - "rule": "BridgeMethodsInterfaceRule", - "ruleset": "Spryker", - "priority": "2" - }, - { - "fileName": "src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php", - "description": "Bridges: The bridge interface has incorrect method signature for `markAsPublic()`. Missed return type. That violates the rule \"All bridge interface methods must have exactly the same or more strict signature as their parent\"", - "rule": "BridgeMethodsInterfaceRule", - "ruleset": "Spryker", - "priority": "2" - }, - { - "fileName": "src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php", - "description": "Bridges: The bridge interface has incorrect method signature for `createDirectory()`. Missed return type. That violates the rule \"All bridge interface methods must have exactly the same or more strict signature as their parent\"", - "rule": "BridgeMethodsInterfaceRule", - "ruleset": "Spryker", - "priority": "2" - }, - { - "fileName": "src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php", - "description": "Bridges: The bridge interface has incorrect method signature for `deleteDirectory()`. Missed return type. That violates the rule \"All bridge interface methods must have exactly the same or more strict signature as their parent\"", - "rule": "BridgeMethodsInterfaceRule", - "ruleset": "Spryker", - "priority": "2" - }, - { - "fileName": "src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php", - "description": "Bridges: The bridge interface has incorrect method signature for `copy()`. Missed return type. That violates the rule \"All bridge interface methods must have exactly the same or more strict signature as their parent\"", - "rule": "BridgeMethodsInterfaceRule", - "ruleset": "Spryker", - "priority": "2" - }, - { - "fileName": "src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php", - "description": "Bridges: The bridge interface has incorrect method signature for `delete()`. Missed return type. That violates the rule \"All bridge interface methods must have exactly the same or more strict signature as their parent\"", - "rule": "BridgeMethodsInterfaceRule", - "ruleset": "Spryker", - "priority": "2" - }, - { - "fileName": "src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php", - "description": "Bridges: The bridge interface has incorrect method signature for `rename()`. Missed return type. That violates the rule \"All bridge interface methods must have exactly the same or more strict signature as their parent\"", - "rule": "BridgeMethodsInterfaceRule", - "ruleset": "Spryker", - "priority": "2" - }, - { - "fileName": "src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php", - "description": "Bridges: The bridge interface has incorrect method signature for `write()`. Missed return type. That violates the rule \"All bridge interface methods must have exactly the same or more strict signature as their parent\"", - "rule": "BridgeMethodsInterfaceRule", - "ruleset": "Spryker", - "priority": "2" - }, - { - "fileName": "src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php", - "description": "Bridges: The bridge interface has incorrect method signature for `writeStream()`. Missed return type. That violates the rule \"All bridge interface methods must have exactly the same or more strict signature as their parent\"", - "rule": "BridgeMethodsInterfaceRule", - "ruleset": "Spryker", - "priority": "2" - } -] +[] diff --git a/composer.json b/composer.json index 4596ad6..4b4ae40 100644 --- a/composer.json +++ b/composer.json @@ -5,6 +5,7 @@ "license": "proprietary", "require": { "php": ">=8.2", + "spryker/file-manager-extension": "^1.0.0", "spryker/file-system": "^2.0.0", "spryker/kernel": "^3.30.0", "spryker/locale": "^3.0.0 || ^4.0.0", diff --git a/psalm-report.json b/psalm-report.json index 4e291b2..f06491e 100644 --- a/psalm-report.json +++ b/psalm-report.json @@ -1,5 +1,90 @@ { - "error": [], + "error": [ + { + "severity": "error", + "line_from": 103, + "line_to": 103, + "type": "InvalidReturnType", + "message": "The declared return type 'array' for Spryker\\Zed\\FileManager\\Business\\File\\FileReader::getFilesByIds is incorrect, got 'array'", + "file_name": "vendor/spryker/spryker/Bundles/FileManager/src/Spryker/Zed/FileManager/Business/File/FileReader.php", + "snippet": " * @return array<\\Generated\\Shared\\Transfer\\FileManagerDataTransfer>", + "selected_text": "array<\\Generated\\Shared\\Transfer\\FileManagerDataTransfer>", + "error_level": 6, + "shortcode": 11 + }, + { + "severity": "error", + "line_from": 120, + "line_to": 120, + "type": "InvalidArgument", + "message": "Argument 1 of Generated\\Shared\\Transfer\\FileManagerDataCollectionTransfer::setFileManagerDataItems expects ArrayObject, but ArrayObject provided", + "file_name": "vendor/spryker/spryker/Bundles/FileManager/src/Spryker/Zed/FileManager/Business/File/FileReader.php", + "snippet": " $fileManagerDataCollectionTransfer->setFileManagerDataItems(new ArrayObject($fileManagerDataTransfers));", + "selected_text": "new ArrayObject($fileManagerDataTransfers)", + "error_level": 6, + "shortcode": 4 + }, + { + "severity": "error", + "line_from": 124, + "line_to": 124, + "type": "InvalidReturnStatement", + "message": "The inferred type 'array' does not match the declared return type 'array' for Spryker\\Zed\\FileManager\\Business\\File\\FileReader::getFilesByIds", + "file_name": "vendor/spryker/spryker/Bundles/FileManager/src/Spryker/Zed/FileManager/Business/File/FileReader.php", + "snippet": " return $fileManagerDataCollectionTransfer->getFileManagerDataItems()->getArrayCopy();", + "selected_text": "$fileManagerDataCollectionTransfer->getFileManagerDataItems()->getArrayCopy()", + "error_level": 6, + "shortcode": 128 + }, + { + "severity": "error", + "line_from": 142, + "line_to": 142, + "type": "InvalidArgument", + "message": "Argument 1 of ArrayObject::offsetGet expects Generated\\Shared\\Transfer\\FileManagerDataTransfer, but 0 provided", + "file_name": "vendor/spryker/spryker/Bundles/FileManager/src/Spryker/Zed/FileManager/Business/File/FileReader.php", + "snippet": " return $fileManagerDataCollectionTransfer->getFileManagerDataItems()->offsetGet(0);", + "selected_text": "0", + "error_level": 6, + "shortcode": 4 + }, + { + "severity": "error", + "line_from": 154, + "line_to": 154, + "type": "InvalidArgument", + "message": "Argument 1 of ArrayObject::offsetGet expects Generated\\Shared\\Transfer\\FileInfoTransfer, but 0 provided", + "file_name": "vendor/spryker/spryker/Bundles/FileManager/src/Spryker/Zed/FileManager/Business/File/FileReader.php", + "snippet": " return $fileTransfer->getFileInfo()[0] ?? null;", + "selected_text": "0", + "error_level": 6, + "shortcode": 4 + }, + { + "severity": "error", + "line_from": 258, + "line_to": 258, + "type": "InvalidArgument", + "message": "Argument 1 of ArrayObject::offsetGet expects Generated\\Shared\\Transfer\\FileManagerDataTransfer, but 0 provided", + "file_name": "vendor/spryker/spryker/Bundles/FileManager/src/Spryker/Zed/FileManager/Business/File/FileSaver.php", + "snippet": " return $fileManagerDataCollectionTransfer->getFileManagerDataItems()->offsetGet(0);", + "selected_text": "0", + "error_level": 6, + "shortcode": 4 + }, + { + "severity": "error", + "line_from": 45, + "line_to": 45, + "type": "NullArgument", + "message": "Argument 1 of ArrayObject::offsetSet cannot be null, null value provided to parameter with type int", + "file_name": "vendor/spryker/spryker/Bundles/FileManager/src/Spryker/Zed/FileManager/Business/FileDirectoryTree/FileDirectoryTreeReader.php", + "snippet": " $nodesWithoutPosition[] = $fileDirectoryTreeNodeTransfer;", + "selected_text": "$nodesWithoutPosition", + "error_level": 6, + "shortcode": 57 + } + ], "warning": [ { "severity": "info", @@ -13,18 +98,6 @@ "error_level": 1, "shortcode": 80 }, - { - "severity": "info", - "line_from": 53, - "line_to": 53, - "type": "PossiblyInvalidMethodCall", - "message": "Cannot call method on possible array variable ", - "file_name": "vendor/spryker/spryker/Bundles/FileManager/src/Spryker/Zed/FileManager/Business/FileDirectoryTree/FileDirectoryTreeReader.php", - "snippet": " $fileDirectoryTreeTransfer->getNodes()->append($item);", - "selected_text": "append", - "error_level": 3, - "shortcode": 113 - }, { "severity": "info", "line_from": 29, @@ -75,16 +148,42 @@ }, { "severity": "info", - "line_from": 56, - "line_to": 56, - "type": "PossiblyNullArrayOffset", - "message": "Cannot access value on variable $fileInfoCollection using possibly null offset int|mixed|null", - "file_name": "vendor/spryker/spryker/Bundles/FileManager/src/Spryker/Zed/FileManager/Persistence/Mapper/FileManagerMapper.php", - "snippet": " $fileInfo = $fileInfoCollection[$fileInfoTransfer->getIdFileInfo()] ?? new SpyFileInfo();", - "selected_text": "$fileInfoCollection", + "line_from": 345, + "line_to": 345, + "type": "PossiblyNullArgument", + "message": "Argument 1 of Orm\\Zed\\FileManager\\Persistence\\SpyFileQuery::limit cannot be null, possibly null value provided", + "file_name": "vendor/spryker/spryker/Bundles/FileManager/src/Spryker/Zed/FileManager/Persistence/FileManagerRepository.php", + "snippet": " ->limit($paginationTransfer->getLimit())", + "selected_text": "$paginationTransfer->getLimit()", "error_level": 3, - "shortcode": 125 + "shortcode": 78 + }, + { + "severity": "info", + "line_from": 346, + "line_to": 346, + "type": "PossiblyNullArgument", + "message": "Argument 1 of Orm\\Zed\\FileManager\\Persistence\\SpyFileQuery::offset cannot be null, possibly null value provided", + "file_name": "vendor/spryker/spryker/Bundles/FileManager/src/Spryker/Zed/FileManager/Persistence/FileManagerRepository.php", + "snippet": " ->offset($paginationTransfer->getOffset());", + "selected_text": "$paginationTransfer->getOffset()", + "error_level": 3, + "shortcode": 78 + } + ], + "deprecation": [ + { + "severity": "info", + "line_from": 218, + "line_to": 218, + "type": "DeprecatedMethod", + "message": "The method Spryker\\Zed\\FileManager\\Persistence\\Mapper\\FileManagerMapper::hasExtensionsField has been marked as deprecated", + "file_name": "vendor/spryker/spryker/Bundles/FileManager/src/Spryker/Zed/FileManager/Persistence/Mapper/FileManagerMapper.php", + "snippet": " if ($this->hasExtensionsField() && $mimeType->getExtensions()) {", + "selected_text": "hasExtensionsField", + "error_level": 2, + "shortcode": 1 } ], - "deprecation": [] + "count": 15 } diff --git a/src/Spryker/Service/FileManager/Dependency/Service/FileManagerToFileSystemServiceBridge.php b/src/Spryker/Service/FileManager/Dependency/Service/FileManagerToFileSystemServiceBridge.php index a0135b7..2678b3e 100644 --- a/src/Spryker/Service/FileManager/Dependency/Service/FileManagerToFileSystemServiceBridge.php +++ b/src/Spryker/Service/FileManager/Dependency/Service/FileManagerToFileSystemServiceBridge.php @@ -30,7 +30,7 @@ public function __construct($fileSystemService) * * @return string */ - public function read(FileSystemQueryTransfer $fileSystemQueryTransfer) + public function read(FileSystemQueryTransfer $fileSystemQueryTransfer): string { return $this->fileSystemService->read($fileSystemQueryTransfer); } @@ -40,7 +40,7 @@ public function read(FileSystemQueryTransfer $fileSystemQueryTransfer) * * @return mixed */ - public function readStream(FileSystemStreamTransfer $fileSystemStreamTransfer) + public function readStream(FileSystemStreamTransfer $fileSystemStreamTransfer): mixed { return $this->fileSystemService->readStream($fileSystemStreamTransfer); } @@ -50,7 +50,7 @@ public function readStream(FileSystemStreamTransfer $fileSystemStreamTransfer) * * @return string */ - public function getMimeType(FileSystemQueryTransfer $fileSystemQueryTransfer) + public function getMimeType(FileSystemQueryTransfer $fileSystemQueryTransfer): string { return $this->fileSystemService->getMimeType($fileSystemQueryTransfer); } diff --git a/src/Spryker/Service/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php b/src/Spryker/Service/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php index 90ea8b6..e7e7fe6 100644 --- a/src/Spryker/Service/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php +++ b/src/Spryker/Service/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php @@ -19,7 +19,7 @@ interface FileManagerToFileSystemServiceInterface * * @return string */ - public function read(FileSystemQueryTransfer $fileSystemQueryTransfer); + public function read(FileSystemQueryTransfer $fileSystemQueryTransfer): string; /** * @param \Generated\Shared\Transfer\FileSystemStreamTransfer $fileSystemStreamTransfer @@ -28,7 +28,7 @@ public function read(FileSystemQueryTransfer $fileSystemQueryTransfer); * * @return mixed */ - public function readStream(FileSystemStreamTransfer $fileSystemStreamTransfer); + public function readStream(FileSystemStreamTransfer $fileSystemStreamTransfer): mixed; /** * @param \Generated\Shared\Transfer\FileSystemQueryTransfer $fileSystemQueryTransfer @@ -37,5 +37,5 @@ public function readStream(FileSystemStreamTransfer $fileSystemStreamTransfer); * * @return string */ - public function getMimeType(FileSystemQueryTransfer $fileSystemQueryTransfer); + public function getMimeType(FileSystemQueryTransfer $fileSystemQueryTransfer): string; } diff --git a/src/Spryker/Shared/FileManager/Transfer/file_manager.transfer.xml b/src/Spryker/Shared/FileManager/Transfer/file_manager.transfer.xml index 6a77afd..f2e0dd7 100644 --- a/src/Spryker/Shared/FileManager/Transfer/file_manager.transfer.xml +++ b/src/Spryker/Shared/FileManager/Transfer/file_manager.transfer.xml @@ -157,4 +157,8 @@ + + + + diff --git a/src/Spryker/Zed/FileManager/Business/File/FileReader.php b/src/Spryker/Zed/FileManager/Business/File/FileReader.php index 6f865b5..9d6cb0a 100644 --- a/src/Spryker/Zed/FileManager/Business/File/FileReader.php +++ b/src/Spryker/Zed/FileManager/Business/File/FileReader.php @@ -7,6 +7,8 @@ namespace Spryker\Zed\FileManager\Business\File; +use ArrayObject; +use Generated\Shared\Transfer\FileManagerDataCollectionTransfer; use Generated\Shared\Transfer\FileManagerDataTransfer; use Generated\Shared\Transfer\FileTransfer; use Spryker\Zed\FileManager\Business\FileContent\FileContentInterface; @@ -24,14 +26,24 @@ class FileReader implements FileReaderInterface */ protected $fileContent; + /** + * @var list<\Spryker\Zed\FileManagerExtension\Dependency\Plugin\FileManagerDataCollectionExpanderPluginInterface> + */ + protected array $fileManagerDataCollectionExpanderPlugins; + /** * @param \Spryker\Zed\FileManager\Persistence\FileManagerRepositoryInterface $repository * @param \Spryker\Zed\FileManager\Business\FileContent\FileContentInterface $fileContent + * @param list<\Spryker\Zed\FileManagerExtension\Dependency\Plugin\FileManagerDataCollectionExpanderPluginInterface> $fileManagerDataCollectionExpanderPlugins */ - public function __construct(FileManagerRepositoryInterface $repository, FileContentInterface $fileContent) - { + public function __construct( + FileManagerRepositoryInterface $repository, + FileContentInterface $fileContent, + array $fileManagerDataCollectionExpanderPlugins + ) { $this->repository = $repository; $this->fileContent = $fileContent; + $this->fileManagerDataCollectionExpanderPlugins = $fileManagerDataCollectionExpanderPlugins; } /** @@ -101,10 +113,15 @@ public function getFilesByIds(array $idFiles): array $fileManagerDataTransfers = []; foreach ($fileTransfers as $fileTransfer) { - $fileManagerDataTransfers[] = $this->createResponseTransfer($fileTransfer); + $fileManagerDataTransfers[] = $this->createFileManagerDataTransfer($fileTransfer); } - return $fileManagerDataTransfers; + $fileManagerDataCollectionTransfer = new FileManagerDataCollectionTransfer(); + $fileManagerDataCollectionTransfer->setFileManagerDataItems(new ArrayObject($fileManagerDataTransfers)); + + $fileManagerDataCollectionTransfer = $this->executeFileManagerDataCollectionExpanderPlugins($fileManagerDataCollectionTransfer); + + return $fileManagerDataCollectionTransfer->getFileManagerDataItems()->getArrayCopy(); } /** @@ -115,18 +132,14 @@ public function getFilesByIds(array $idFiles): array */ protected function createResponseTransfer(FileTransfer $fileTransfer, ?int $idFileInfo = null) { - $fileManagerDataTransfer = new FileManagerDataTransfer(); - $fileManagerDataTransfer->setFile($fileTransfer); - $fileInfoTransfer = $this->getRequestedFileInfo($fileTransfer, $idFileInfo); - $fileManagerDataTransfer->setFileInfo($fileInfoTransfer); + $fileManagerDataTransfer = $this->createFileManagerDataTransfer($fileTransfer, $idFileInfo); - if ($fileInfoTransfer !== null) { - $fileManagerDataTransfer->setContent( - $this->fileContent->read($fileInfoTransfer->getStorageFileNameOrFail()), - ); - } + $fileManagerDataCollectionTransfer = new FileManagerDataCollectionTransfer(); + $fileManagerDataCollectionTransfer->addFileManagerDataItem($fileManagerDataTransfer); - return $fileManagerDataTransfer; + $fileManagerDataCollectionTransfer = $this->executeFileManagerDataCollectionExpanderPlugins($fileManagerDataCollectionTransfer); + + return $fileManagerDataCollectionTransfer->getFileManagerDataItems()->offsetGet(0); } /** @@ -149,4 +162,41 @@ protected function getRequestedFileInfo(FileTransfer $fileTransfer, ?int $idFile return null; } + + /** + * @param \Generated\Shared\Transfer\FileManagerDataCollectionTransfer $fileManagerDataCollectionTransfer + * + * @return \Generated\Shared\Transfer\FileManagerDataCollectionTransfer + */ + protected function executeFileManagerDataCollectionExpanderPlugins( + FileManagerDataCollectionTransfer $fileManagerDataCollectionTransfer + ): FileManagerDataCollectionTransfer { + foreach ($this->fileManagerDataCollectionExpanderPlugins as $fileManagerDataCollectionExpanderPlugin) { + $fileManagerDataCollectionTransfer = $fileManagerDataCollectionExpanderPlugin->expand($fileManagerDataCollectionTransfer); + } + + return $fileManagerDataCollectionTransfer; + } + + /** + * @param \Generated\Shared\Transfer\FileTransfer $fileTransfer + * @param int|null $idFileInfo + * + * @return \Generated\Shared\Transfer\FileManagerDataTransfer + */ + protected function createFileManagerDataTransfer(FileTransfer $fileTransfer, ?int $idFileInfo = null): FileManagerDataTransfer + { + $fileManagerDataTransfer = new FileManagerDataTransfer(); + $fileManagerDataTransfer->setFile($fileTransfer); + $fileInfoTransfer = $this->getRequestedFileInfo($fileTransfer, $idFileInfo); + $fileManagerDataTransfer->setFileInfo($fileInfoTransfer); + + if ($fileInfoTransfer !== null) { + $fileManagerDataTransfer->setContent( + $this->fileContent->read($fileInfoTransfer->getStorageFileNameOrFail()), + ); + } + + return $fileManagerDataTransfer; + } } diff --git a/src/Spryker/Zed/FileManager/Business/File/FileSaver.php b/src/Spryker/Zed/FileManager/Business/File/FileSaver.php index af20e2a..5ec936a 100644 --- a/src/Spryker/Zed/FileManager/Business/File/FileSaver.php +++ b/src/Spryker/Zed/FileManager/Business/File/FileSaver.php @@ -7,6 +7,7 @@ namespace Spryker\Zed\FileManager\Business\File; +use Generated\Shared\Transfer\FileManagerDataCollectionTransfer; use Generated\Shared\Transfer\FileManagerDataTransfer; use Generated\Shared\Transfer\FileTransfer; use Spryker\Zed\FileManager\Business\FileContent\FileContentInterface; @@ -61,25 +62,33 @@ class FileSaver implements FileSaverInterface */ protected $attributesSaver; + /** + * @var list<\Spryker\Zed\FileManagerExtension\Dependency\Plugin\FileManagerDataCollectionExpanderPreSavePluginInterface> + */ + protected array $fileManagerDataCollectionExpanderPreSavePlugins; + /** * @param \Spryker\Zed\FileManager\Persistence\FileManagerEntityManagerInterface $entityManager * @param \Spryker\Zed\FileManager\Business\File\FileVersionInterface $fileVersion * @param \Spryker\Zed\FileManager\Business\FileContent\FileContentInterface $fileContent * @param \Spryker\Zed\FileManager\Business\FileLocalizedAttributes\FileLocalizedAttributesSaverInterface $attributesSaver * @param \Spryker\Zed\FileManager\FileManagerConfig $config + * @param list<\Spryker\Zed\FileManagerExtension\Dependency\Plugin\FileManagerDataCollectionExpanderPreSavePluginInterface> $fileManagerDataCollectionExpanderPreSavePlugins */ public function __construct( FileManagerEntityManagerInterface $entityManager, FileVersionInterface $fileVersion, FileContentInterface $fileContent, FileLocalizedAttributesSaverInterface $attributesSaver, - FileManagerConfig $config + FileManagerConfig $config, + array $fileManagerDataCollectionExpanderPreSavePlugins ) { $this->entityManager = $entityManager; $this->fileVersion = $fileVersion; $this->fileContent = $fileContent; $this->config = $config; $this->attributesSaver = $attributesSaver; + $this->fileManagerDataCollectionExpanderPreSavePlugins = $fileManagerDataCollectionExpanderPreSavePlugins; } /** @@ -101,6 +110,8 @@ public function save(FileManagerDataTransfer $fileManagerDataTransfer) */ protected function executeSaveTransaction(FileManagerDataTransfer $fileManagerDataTransfer) { + $fileManagerDataTransfer = $this->expandFileManagerDataTransfer($fileManagerDataTransfer); + $this->saveFile($fileManagerDataTransfer); $this->saveFileInfo($fileManagerDataTransfer); @@ -215,4 +226,35 @@ protected function getFileNameVersionDelimiter() { return $this->config->getFileNameVersionDelimiter(); } + + /** + * @param \Generated\Shared\Transfer\FileManagerDataCollectionTransfer $fileManagerDataCollectionTransfer + * + * @return \Generated\Shared\Transfer\FileManagerDataCollectionTransfer + */ + protected function executeFileManagerDataCollectionExpanderPreSavePlugins( + FileManagerDataCollectionTransfer $fileManagerDataCollectionTransfer + ): FileManagerDataCollectionTransfer { + foreach ($this->fileManagerDataCollectionExpanderPreSavePlugins as $fileManagerDataCollectionExpanderPreSavePlugin) { + $fileManagerDataCollectionTransfer = $fileManagerDataCollectionExpanderPreSavePlugin->expand($fileManagerDataCollectionTransfer); + } + + return $fileManagerDataCollectionTransfer; + } + + /** + * @param \Generated\Shared\Transfer\FileManagerDataTransfer $fileManagerDataTransfer + * + * @return \Generated\Shared\Transfer\FileManagerDataTransfer + */ + protected function expandFileManagerDataTransfer( + FileManagerDataTransfer $fileManagerDataTransfer + ): FileManagerDataTransfer { + $fileManagerDataCollectionTransfer = new FileManagerDataCollectionTransfer(); + $fileManagerDataCollectionTransfer->addFileManagerDataItem($fileManagerDataTransfer); + + $fileManagerDataCollectionTransfer = $this->executeFileManagerDataCollectionExpanderPreSavePlugins($fileManagerDataCollectionTransfer); + + return $fileManagerDataCollectionTransfer->getFileManagerDataItems()->offsetGet(0); + } } diff --git a/src/Spryker/Zed/FileManager/Business/FileManagerBusinessFactory.php b/src/Spryker/Zed/FileManager/Business/FileManagerBusinessFactory.php index c03b234..6bd8329 100644 --- a/src/Spryker/Zed/FileManager/Business/FileManagerBusinessFactory.php +++ b/src/Spryker/Zed/FileManager/Business/FileManagerBusinessFactory.php @@ -44,6 +44,7 @@ public function createFileSaver() $this->createFileContent(), $this->createLocalizedAttributesSaver(), $this->getConfig(), + $this->getFileManagerDataCollectionExpanderPreSavePlugins(), ); } @@ -55,6 +56,7 @@ public function createFileReader() return new FileReader( $this->getRepository(), $this->createFileContent(), + $this->getFileManagerDataCollectionExpanderPlugins(), ); } @@ -203,4 +205,20 @@ public function createMimeTypeReader() { return new MimeTypeReader($this->getRepository()); } + + /** + * @return list<\Spryker\Zed\FileManagerExtension\Dependency\Plugin\FileManagerDataCollectionExpanderPluginInterface> + */ + public function getFileManagerDataCollectionExpanderPlugins(): array + { + return $this->getProvidedDependency(FileManagerDependencyProvider::PLUGINS_FILE_MANAGER_DATA_COLLECTION_EXPANDER); + } + + /** + * @return list<\Spryker\Zed\FileManagerExtension\Dependency\Plugin\FileManagerDataCollectionExpanderPreSavePluginInterface> + */ + public function getFileManagerDataCollectionExpanderPreSavePlugins(): array + { + return $this->getProvidedDependency(FileManagerDependencyProvider::PLUGINS_FILE_MANAGER_DATA_COLLECTION_EXPANDER_PRE_SAVE); + } } diff --git a/src/Spryker/Zed/FileManager/Business/FileManagerFacadeInterface.php b/src/Spryker/Zed/FileManager/Business/FileManagerFacadeInterface.php index aee2ed5..4b851ab 100644 --- a/src/Spryker/Zed/FileManager/Business/FileManagerFacadeInterface.php +++ b/src/Spryker/Zed/FileManager/Business/FileManagerFacadeInterface.php @@ -25,6 +25,7 @@ interface FileManagerFacadeInterface * - Saves file info * - Uploads file content * - Creates a new file version + * - Executes the stack of {@link \Spryker\Zed\FileManagerExtension\Dependency\Plugin\FileManagerDataCollectionExpanderPreSavePluginInterface} plugins. * * @api * @@ -50,6 +51,7 @@ public function saveDirectory(FileDirectoryTransfer $fileDirectoryTransfer); * Specification: * - Finds latest file version * - Returns a file meta info and a file content + * - Executes the stack of {@link \Spryker\Zed\FileManagerExtension\Dependency\Plugin\FileManagerDataCollectionExpanderPluginInterface} plugins. * * @api * @@ -113,6 +115,7 @@ public function rollbackFile($idFileInfo); * - Finds a file by idFile. * - Returns file with localized attributes. * - Reads latest file. + * - Executes the stack of {@link \Spryker\Zed\FileManagerExtension\Dependency\Plugin\FileManagerDataCollectionExpanderPluginInterface} plugins. * * @api * @@ -126,6 +129,7 @@ public function findFileByIdFile($idFile); * Specification: * - Finds a file info by idFileInfo * - Returns a file meta info and a file content with a specified version + * - Executes the stack of {@link \Spryker\Zed\FileManagerExtension\Dependency\Plugin\FileManagerDataCollectionExpanderPluginInterface} plugins. * * @api * @@ -233,6 +237,7 @@ public function findAllowedMimeTypes(); * Specification: * - Finds files by their ids. * - Returns files with localized attributes. + * - Executes the stack of {@link \Spryker\Zed\FileManagerExtension\Dependency\Plugin\FileManagerDataCollectionExpanderPluginInterface} plugins. * * @api * diff --git a/src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceBridge.php b/src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceBridge.php index ca0e302..46add6c 100644 --- a/src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceBridge.php +++ b/src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceBridge.php @@ -38,7 +38,7 @@ public function __construct($fileSystemService) * * @return string */ - public function getMimeType(FileSystemQueryTransfer $fileSystemQueryTransfer) + public function getMimeType(FileSystemQueryTransfer $fileSystemQueryTransfer): string { return $this->fileSystemService->getMimeType($fileSystemQueryTransfer); } @@ -48,7 +48,7 @@ public function getMimeType(FileSystemQueryTransfer $fileSystemQueryTransfer) * * @return int|null */ - public function getTimestamp(FileSystemQueryTransfer $fileSystemQueryTransfer) + public function getTimestamp(FileSystemQueryTransfer $fileSystemQueryTransfer): ?int { return $this->fileSystemService->getTimestamp($fileSystemQueryTransfer); } @@ -58,7 +58,7 @@ public function getTimestamp(FileSystemQueryTransfer $fileSystemQueryTransfer) * * @return int */ - public function getSize(FileSystemQueryTransfer $fileSystemQueryTransfer) + public function getSize(FileSystemQueryTransfer $fileSystemQueryTransfer): int { return $this->fileSystemService->getSize($fileSystemQueryTransfer); } @@ -68,7 +68,7 @@ public function getSize(FileSystemQueryTransfer $fileSystemQueryTransfer) * * @return bool */ - public function isPrivate(FileSystemQueryTransfer $fileSystemQueryTransfer) + public function isPrivate(FileSystemQueryTransfer $fileSystemQueryTransfer): bool { return $this->fileSystemService->isPrivate($fileSystemQueryTransfer); } @@ -78,7 +78,7 @@ public function isPrivate(FileSystemQueryTransfer $fileSystemQueryTransfer) * * @return string */ - public function read(FileSystemQueryTransfer $fileSystemQueryTransfer) + public function read(FileSystemQueryTransfer $fileSystemQueryTransfer): string { return $this->fileSystemService->read($fileSystemQueryTransfer); } @@ -88,7 +88,7 @@ public function read(FileSystemQueryTransfer $fileSystemQueryTransfer) * * @return array<\Generated\Shared\Transfer\FileSystemResourceTransfer> */ - public function listContents(FileSystemListTransfer $fileSystemListTransfer) + public function listContents(FileSystemListTransfer $fileSystemListTransfer): array { return $this->fileSystemService->listContents($fileSystemListTransfer); } @@ -98,7 +98,7 @@ public function listContents(FileSystemListTransfer $fileSystemListTransfer) * * @return bool */ - public function has(FileSystemQueryTransfer $fileSystemQueryTransfer) + public function has(FileSystemQueryTransfer $fileSystemQueryTransfer): bool { return $this->fileSystemService->has($fileSystemQueryTransfer); } @@ -108,7 +108,7 @@ public function has(FileSystemQueryTransfer $fileSystemQueryTransfer) * * @return void */ - public function markAsPrivate(FileSystemVisibilityTransfer $fileSystemVisibilityTransfer) + public function markAsPrivate(FileSystemVisibilityTransfer $fileSystemVisibilityTransfer): void { $this->fileSystemService->markAsPrivate($fileSystemVisibilityTransfer); } @@ -118,7 +118,7 @@ public function markAsPrivate(FileSystemVisibilityTransfer $fileSystemVisibility * * @return void */ - public function markAsPublic(FileSystemVisibilityTransfer $fileSystemVisibilityTransfer) + public function markAsPublic(FileSystemVisibilityTransfer $fileSystemVisibilityTransfer): void { $this->fileSystemService->markAsPublic($fileSystemVisibilityTransfer); } @@ -128,7 +128,7 @@ public function markAsPublic(FileSystemVisibilityTransfer $fileSystemVisibilityT * * @return void */ - public function createDirectory(FileSystemCreateDirectoryTransfer $fileSystemCreateDirectoryTransfer) + public function createDirectory(FileSystemCreateDirectoryTransfer $fileSystemCreateDirectoryTransfer): void { $this->fileSystemService->createDirectory($fileSystemCreateDirectoryTransfer); } @@ -138,7 +138,7 @@ public function createDirectory(FileSystemCreateDirectoryTransfer $fileSystemCre * * @return void */ - public function deleteDirectory(FileSystemDeleteDirectoryTransfer $fileSystemDeleteDirectoryTransfer) + public function deleteDirectory(FileSystemDeleteDirectoryTransfer $fileSystemDeleteDirectoryTransfer): void { $this->fileSystemService->deleteDirectory($fileSystemDeleteDirectoryTransfer); } @@ -148,7 +148,7 @@ public function deleteDirectory(FileSystemDeleteDirectoryTransfer $fileSystemDel * * @return void */ - public function copy(FileSystemCopyTransfer $fileSystemCopyTransfer) + public function copy(FileSystemCopyTransfer $fileSystemCopyTransfer): void { $this->fileSystemService->copy($fileSystemCopyTransfer); } @@ -158,7 +158,7 @@ public function copy(FileSystemCopyTransfer $fileSystemCopyTransfer) * * @return void */ - public function delete(FileSystemDeleteTransfer $fileSystemDeleteTransfer) + public function delete(FileSystemDeleteTransfer $fileSystemDeleteTransfer): void { $this->fileSystemService->delete($fileSystemDeleteTransfer); } @@ -168,7 +168,7 @@ public function delete(FileSystemDeleteTransfer $fileSystemDeleteTransfer) * * @return void */ - public function rename(FileSystemRenameTransfer $fileSystemRenameTransfer) + public function rename(FileSystemRenameTransfer $fileSystemRenameTransfer): void { $this->fileSystemService->rename($fileSystemRenameTransfer); } @@ -178,7 +178,7 @@ public function rename(FileSystemRenameTransfer $fileSystemRenameTransfer) * * @return void */ - public function write(FileSystemContentTransfer $fileSystemContentTransfer) + public function write(FileSystemContentTransfer $fileSystemContentTransfer): void { $this->fileSystemService->write($fileSystemContentTransfer); } @@ -188,7 +188,7 @@ public function write(FileSystemContentTransfer $fileSystemContentTransfer) * * @return mixed */ - public function readStream(FileSystemStreamTransfer $fileSystemStreamTransfer) + public function readStream(FileSystemStreamTransfer $fileSystemStreamTransfer): mixed { return $this->fileSystemService->readStream($fileSystemStreamTransfer); } @@ -199,7 +199,7 @@ public function readStream(FileSystemStreamTransfer $fileSystemStreamTransfer) * * @return void */ - public function writeStream(FileSystemStreamTransfer $fileSystemStreamTransfer, $stream) + public function writeStream(FileSystemStreamTransfer $fileSystemStreamTransfer, mixed $stream): void { $this->fileSystemService->writeStream($fileSystemStreamTransfer, $stream); } diff --git a/src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php b/src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php index acd33f3..1b04ad7 100644 --- a/src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php +++ b/src/Spryker/Zed/FileManager/Dependency/Service/FileManagerToFileSystemServiceInterface.php @@ -27,7 +27,7 @@ interface FileManagerToFileSystemServiceInterface * * @return string */ - public function getMimeType(FileSystemQueryTransfer $fileSystemQueryTransfer); + public function getMimeType(FileSystemQueryTransfer $fileSystemQueryTransfer): string; /** * @param \Generated\Shared\Transfer\FileSystemQueryTransfer $fileSystemQueryTransfer @@ -36,7 +36,7 @@ public function getMimeType(FileSystemQueryTransfer $fileSystemQueryTransfer); * * @return int|null */ - public function getTimestamp(FileSystemQueryTransfer $fileSystemQueryTransfer); + public function getTimestamp(FileSystemQueryTransfer $fileSystemQueryTransfer): ?int; /** * @param \Generated\Shared\Transfer\FileSystemQueryTransfer $fileSystemQueryTransfer @@ -45,7 +45,7 @@ public function getTimestamp(FileSystemQueryTransfer $fileSystemQueryTransfer); * * @return int */ - public function getSize(FileSystemQueryTransfer $fileSystemQueryTransfer); + public function getSize(FileSystemQueryTransfer $fileSystemQueryTransfer): int; /** * @param \Generated\Shared\Transfer\FileSystemQueryTransfer $fileSystemQueryTransfer @@ -54,7 +54,7 @@ public function getSize(FileSystemQueryTransfer $fileSystemQueryTransfer); * * @return bool */ - public function isPrivate(FileSystemQueryTransfer $fileSystemQueryTransfer); + public function isPrivate(FileSystemQueryTransfer $fileSystemQueryTransfer): bool; /** * @param \Generated\Shared\Transfer\FileSystemQueryTransfer $fileSystemQueryTransfer @@ -63,7 +63,7 @@ public function isPrivate(FileSystemQueryTransfer $fileSystemQueryTransfer); * * @return string */ - public function read(FileSystemQueryTransfer $fileSystemQueryTransfer); + public function read(FileSystemQueryTransfer $fileSystemQueryTransfer): string; /** * @param \Generated\Shared\Transfer\FileSystemListTransfer $fileSystemListTransfer @@ -72,7 +72,7 @@ public function read(FileSystemQueryTransfer $fileSystemQueryTransfer); * * @return array<\Generated\Shared\Transfer\FileSystemResourceTransfer> */ - public function listContents(FileSystemListTransfer $fileSystemListTransfer); + public function listContents(FileSystemListTransfer $fileSystemListTransfer): array; /** * @param \Generated\Shared\Transfer\FileSystemQueryTransfer $fileSystemQueryTransfer @@ -81,7 +81,7 @@ public function listContents(FileSystemListTransfer $fileSystemListTransfer); * * @return bool */ - public function has(FileSystemQueryTransfer $fileSystemQueryTransfer); + public function has(FileSystemQueryTransfer $fileSystemQueryTransfer): bool; /** * @param \Generated\Shared\Transfer\FileSystemVisibilityTransfer $fileSystemVisibilityTransfer @@ -90,7 +90,7 @@ public function has(FileSystemQueryTransfer $fileSystemQueryTransfer); * * @return void */ - public function markAsPrivate(FileSystemVisibilityTransfer $fileSystemVisibilityTransfer); + public function markAsPrivate(FileSystemVisibilityTransfer $fileSystemVisibilityTransfer): void; /** * @param \Generated\Shared\Transfer\FileSystemVisibilityTransfer $fileSystemVisibilityTransfer @@ -99,7 +99,7 @@ public function markAsPrivate(FileSystemVisibilityTransfer $fileSystemVisibility * * @return void */ - public function markAsPublic(FileSystemVisibilityTransfer $fileSystemVisibilityTransfer); + public function markAsPublic(FileSystemVisibilityTransfer $fileSystemVisibilityTransfer): void; /** * @param \Generated\Shared\Transfer\FileSystemCreateDirectoryTransfer $fileSystemCreateDirectoryTransfer @@ -108,7 +108,7 @@ public function markAsPublic(FileSystemVisibilityTransfer $fileSystemVisibilityT * * @return void */ - public function createDirectory(FileSystemCreateDirectoryTransfer $fileSystemCreateDirectoryTransfer); + public function createDirectory(FileSystemCreateDirectoryTransfer $fileSystemCreateDirectoryTransfer): void; /** * @param \Generated\Shared\Transfer\FileSystemDeleteDirectoryTransfer $fileSystemDeleteDirectoryTransfer @@ -117,7 +117,7 @@ public function createDirectory(FileSystemCreateDirectoryTransfer $fileSystemCre * * @return void */ - public function deleteDirectory(FileSystemDeleteDirectoryTransfer $fileSystemDeleteDirectoryTransfer); + public function deleteDirectory(FileSystemDeleteDirectoryTransfer $fileSystemDeleteDirectoryTransfer): void; /** * @param \Generated\Shared\Transfer\FileSystemCopyTransfer $fileSystemCopyTransfer @@ -126,7 +126,7 @@ public function deleteDirectory(FileSystemDeleteDirectoryTransfer $fileSystemDel * * @return void */ - public function copy(FileSystemCopyTransfer $fileSystemCopyTransfer); + public function copy(FileSystemCopyTransfer $fileSystemCopyTransfer): void; /** * @param \Generated\Shared\Transfer\FileSystemDeleteTransfer $fileSystemDeleteTransfer @@ -135,7 +135,7 @@ public function copy(FileSystemCopyTransfer $fileSystemCopyTransfer); * * @return void */ - public function delete(FileSystemDeleteTransfer $fileSystemDeleteTransfer); + public function delete(FileSystemDeleteTransfer $fileSystemDeleteTransfer): void; /** * @param \Generated\Shared\Transfer\FileSystemRenameTransfer $fileSystemRenameTransfer @@ -144,7 +144,7 @@ public function delete(FileSystemDeleteTransfer $fileSystemDeleteTransfer); * * @return void */ - public function rename(FileSystemRenameTransfer $fileSystemRenameTransfer); + public function rename(FileSystemRenameTransfer $fileSystemRenameTransfer): void; /** * @param \Generated\Shared\Transfer\FileSystemContentTransfer $fileSystemContentTransfer @@ -153,7 +153,7 @@ public function rename(FileSystemRenameTransfer $fileSystemRenameTransfer); * * @return void */ - public function write(FileSystemContentTransfer $fileSystemContentTransfer); + public function write(FileSystemContentTransfer $fileSystemContentTransfer): void; /** * @param \Generated\Shared\Transfer\FileSystemStreamTransfer $fileSystemStreamTransfer @@ -162,7 +162,7 @@ public function write(FileSystemContentTransfer $fileSystemContentTransfer); * * @return mixed */ - public function readStream(FileSystemStreamTransfer $fileSystemStreamTransfer); + public function readStream(FileSystemStreamTransfer $fileSystemStreamTransfer): mixed; /** * @param \Generated\Shared\Transfer\FileSystemStreamTransfer $fileSystemStreamTransfer @@ -172,5 +172,5 @@ public function readStream(FileSystemStreamTransfer $fileSystemStreamTransfer); * * @return void */ - public function writeStream(FileSystemStreamTransfer $fileSystemStreamTransfer, $stream); + public function writeStream(FileSystemStreamTransfer $fileSystemStreamTransfer, mixed $stream): void; } diff --git a/src/Spryker/Zed/FileManager/FileManagerDependencyProvider.php b/src/Spryker/Zed/FileManager/FileManagerDependencyProvider.php index a904d24..e981eca 100644 --- a/src/Spryker/Zed/FileManager/FileManagerDependencyProvider.php +++ b/src/Spryker/Zed/FileManager/FileManagerDependencyProvider.php @@ -27,6 +27,16 @@ class FileManagerDependencyProvider extends AbstractBundleDependencyProvider */ public const SERVICE_UTIL_ENCODING = 'SERVICE_UTIL_ENCODING'; + /** + * @var string + */ + public const PLUGINS_FILE_MANAGER_DATA_COLLECTION_EXPANDER = 'PLUGINS_FILE_MANAGER_DATA_COLLECTION_EXPANDER'; + + /** + * @var string + */ + public const PLUGINS_FILE_MANAGER_DATA_COLLECTION_EXPANDER_PRE_SAVE = 'PLUGINS_FILE_MANAGER_DATA_COLLECTION_EXPANDER_PRE_SAVE'; + /** * @param \Spryker\Zed\Kernel\Container $container * @@ -35,6 +45,8 @@ class FileManagerDependencyProvider extends AbstractBundleDependencyProvider public function provideBusinessLayerDependencies(Container $container) { $container = $this->addFileSystemService($container); + $container = $this->addFileManagerDataCollectionExpanderPlugins($container); + $container = $this->addFileManagerDataCollectionExpanderPreSavePlugins($container); return $container; } @@ -84,4 +96,48 @@ protected function addFileSystemService(Container $container) return $container; } + + /** + * @param \Spryker\Zed\Kernel\Container $container + * + * @return \Spryker\Zed\Kernel\Container + */ + protected function addFileManagerDataCollectionExpanderPlugins(Container $container): Container + { + $container->set(static::PLUGINS_FILE_MANAGER_DATA_COLLECTION_EXPANDER, function () { + return $this->getFileManagerDataCollectionExpanderPlugins(); + }); + + return $container; + } + + /** + * @param \Spryker\Zed\Kernel\Container $container + * + * @return \Spryker\Zed\Kernel\Container + */ + protected function addFileManagerDataCollectionExpanderPreSavePlugins(Container $container): Container + { + $container->set(static::PLUGINS_FILE_MANAGER_DATA_COLLECTION_EXPANDER_PRE_SAVE, function () { + return $this->getFileManagerDataCollectionExpanderPreSavePlugins(); + }); + + return $container; + } + + /** + * @return list<\Spryker\Zed\FileManagerExtension\Dependency\Plugin\FileManagerDataCollectionExpanderPluginInterface> + */ + protected function getFileManagerDataCollectionExpanderPlugins(): array + { + return []; + } + + /** + * @return list<\Spryker\Zed\FileManagerExtension\Dependency\Plugin\FileManagerDataCollectionExpanderPreSavePluginInterface> + */ + protected function getFileManagerDataCollectionExpanderPreSavePlugins(): array + { + return []; + } } diff --git a/tests/SprykerTest/Zed/FileManager/Business/File/FileReaderTest.php b/tests/SprykerTest/Zed/FileManager/Business/File/FileReaderTest.php index 4948c04..2983585 100644 --- a/tests/SprykerTest/Zed/FileManager/Business/File/FileReaderTest.php +++ b/tests/SprykerTest/Zed/FileManager/Business/File/FileReaderTest.php @@ -94,9 +94,11 @@ protected function assertFileInfo(FileManagerDataTransfer $fileManagerDataTransf */ public function testRead(): void { + //Arrange $fileContentMock = $this->createFileContentMock(); $fileManagerRepositoryMock = $this->createFileManagerRepositoryMock(); + // Expect $fileManagerRepositoryMock->expects($this->once()) ->method('getFileByIdFileInfo') ->willReturn($this->getMockedFile()); @@ -105,7 +107,13 @@ public function testRead(): void ->method('read') ->willReturn('the content of the file'); - $fileReader = new FileReader($fileManagerRepositoryMock, $fileContentMock); + $fileReader = new FileReader( + $fileManagerRepositoryMock, + $fileContentMock, + [], + ); + + //Act $this->assertFileInfo($fileReader->readFileByIdFileInfo(1)); } @@ -114,9 +122,11 @@ public function testRead(): void */ public function testReadLatestByFileId(): void { + //Arrange $fileContentMock = $this->createFileContentMock(); $fileManagerRepositoryMock = $this->createFileManagerRepositoryMock(); + // Expect $fileManagerRepositoryMock->expects($this->once()) ->method('getLatestFileInfoByIdFile') ->willReturn($this->getMockedFileInfo()); @@ -125,7 +135,13 @@ public function testReadLatestByFileId(): void ->method('read') ->willReturn('the content of the file'); - $fileReader = new FileReader($fileManagerRepositoryMock, $fileContentMock); + $fileReader = new FileReader( + $fileManagerRepositoryMock, + $fileContentMock, + [], + ); + + //Act $this->assertFileInfo($fileReader->readLatestByFileId(1)); } } diff --git a/tests/SprykerTest/Zed/FileManager/Business/FileContent/FileContentTest.php b/tests/SprykerTest/Zed/FileManager/Business/FileContent/FileContentTest.php index ed71e4b..8ce7f20 100644 --- a/tests/SprykerTest/Zed/FileManager/Business/FileContent/FileContentTest.php +++ b/tests/SprykerTest/Zed/FileManager/Business/FileContent/FileContentTest.php @@ -49,8 +49,7 @@ public function testSave(): void { $fileSystemServiceMock = $this->getFileSystemServiceMock(); $fileSystemServiceMock->expects($this->once()) - ->method('write') - ->willReturn(null); + ->method('write'); $fileContent = new FileContent( $fileSystemServiceMock, @@ -71,8 +70,7 @@ public function testDelete(): void { $fileSystemServiceMock = $this->getFileSystemServiceMock(); $fileSystemServiceMock->expects($this->any()) - ->method('delete') - ->willReturn(null); + ->method('delete'); $fileContent = new FileContent( $fileSystemServiceMock, diff --git a/tests/SprykerTest/Zed/FileManager/Business/FileManagerFacadeTest.php b/tests/SprykerTest/Zed/FileManager/Business/FileManagerFacadeTest.php index d66f0a2..327058d 100644 --- a/tests/SprykerTest/Zed/FileManager/Business/FileManagerFacadeTest.php +++ b/tests/SprykerTest/Zed/FileManager/Business/FileManagerFacadeTest.php @@ -12,6 +12,7 @@ use Generated\Shared\Transfer\FileDirectoryTreeNodeTransfer; use Generated\Shared\Transfer\FileDirectoryTreeTransfer; use Generated\Shared\Transfer\FileInfoTransfer; +use Generated\Shared\Transfer\FileManagerDataCollectionTransfer; use Generated\Shared\Transfer\FileManagerDataTransfer; use Generated\Shared\Transfer\FileTransfer; use Generated\Shared\Transfer\MimeTypeCollectionTransfer; @@ -21,6 +22,7 @@ use Orm\Zed\FileManager\Persistence\SpyFileQuery; use Orm\Zed\FileManager\Persistence\SpyMimeType; use Orm\Zed\FileManager\Persistence\SpyMimeTypeQuery; +use PHPUnit\Framework\MockObject\MockObject; use Spryker\Service\FileSystem\FileSystemDependencyProvider; use Spryker\Service\FileSystem\FileSystemService; use Spryker\Service\FileSystem\FileSystemServiceFactory; @@ -36,6 +38,8 @@ use Spryker\Zed\FileManager\Business\FileManagerFacade; use Spryker\Zed\FileManager\Dependency\Service\FileManagerToFileSystemServiceBridge; use Spryker\Zed\FileManager\FileManagerDependencyProvider; +use Spryker\Zed\FileManagerExtension\Dependency\Plugin\FileManagerDataCollectionExpanderPluginInterface; +use Spryker\Zed\FileManagerExtension\Dependency\Plugin\FileManagerDataCollectionExpanderPreSavePluginInterface; use Spryker\Zed\Kernel\Container; use SprykerTest\Zed\FileManager\Stub\FileManagerConfigStub; use SprykerTest\Zed\FileManager\Stub\FileSystemConfigStub; @@ -95,6 +99,14 @@ protected function setUp(): void return new FileManagerToFileSystemServiceBridge($fileSystemService); }; + $container[FileManagerDependencyProvider::PLUGINS_FILE_MANAGER_DATA_COLLECTION_EXPANDER_PRE_SAVE] = function (Container $container) { + return [$this->getFileManagerDataCollectionExpanderPreSavePluginMock()]; + }; + + $container[FileManagerDependencyProvider::PLUGINS_FILE_MANAGER_DATA_COLLECTION_EXPANDER] = function (Container $container) { + return [$this->getFileManagerDataCollectionExpanderPluginMock()]; + }; + $config = new FileManagerConfigStub(); $factory = new FileManagerBusinessFactory(); $factory->setContainer($container); @@ -203,24 +215,13 @@ public function testDeleteFileInfo(): void */ public function testSave(): void { - $fileInfo = new FileInfoTransfer(); - $fileInfo->setVersionName('v10'); - $fileInfo->setVersion(10); - $fileInfo->setSize(17); - $fileInfo->setStorageFileName('new_customer.txt'); - $fileInfo->setType('text'); - $fileInfo->setExtension('txt'); - - $file = new FileTransfer(); - $file->setFileContent('new customer file'); - $file->setFileName('new%customer.txt'); - - $fileManagerDataTransfer = new FileManagerDataTransfer(); - $fileManagerDataTransfer->setContent('new version of the file'); - $fileManagerDataTransfer->setFile($file); - $fileManagerDataTransfer->setFileInfo($fileInfo); + // Arrange + $fileManagerDataTransfer = $this->createFileManagerDataTransfer(); + // Act $fileManagerDataTransfer = $this->facade->saveFile($fileManagerDataTransfer); + + // Assert $file = SpyFileQuery::create()->findOneByFileName('newcustomer.txt'); $this->assertEquals($this->tester->getIdFile() + 1, $fileManagerDataTransfer->getFile()->getIdFile()); @@ -472,4 +473,67 @@ protected function hasExtensionsField(): bool { return property_exists(SpyMimeType::class, 'extensions'); } + + /** + * @return \Spryker\Zed\FileManagerExtension\Dependency\Plugin\FileManagerDataCollectionExpanderPreSavePluginInterface|\PHPUnit\Framework\MockObject\MockObject + */ + protected function getFileManagerDataCollectionExpanderPreSavePluginMock(): FileManagerDataCollectionExpanderPreSavePluginInterface|MockObject + { + $fileManagerDataCollectionExpanderPreSavePluginMock = $this + ->getMockBuilder(FileManagerDataCollectionExpanderPreSavePluginInterface::class) + ->getMock(); + + $fileManagerDataCollectionExpanderPreSavePluginMock + ->expects($this->once()) + ->method('expand') + ->willReturnCallback(function (FileManagerDataCollectionTransfer $fileManagerDataCollectionTransfer) { + return $fileManagerDataCollectionTransfer; + }); + + return $fileManagerDataCollectionExpanderPreSavePluginMock; + } + + /** + * @return \Spryker\Zed\FileManagerExtension\Dependency\Plugin\FileManagerDataCollectionExpanderPluginInterface|\PHPUnit\Framework\MockObject\MockObject + */ + protected function getFileManagerDataCollectionExpanderPluginMock(): FileManagerDataCollectionExpanderPluginInterface|MockObject + { + $fileManagerDataCollectionExpanderPluginMock = $this + ->getMockBuilder(FileManagerDataCollectionExpanderPluginInterface::class) + ->getMock(); + + $fileManagerDataCollectionExpanderPluginMock + ->expects($this->once()) + ->method('expand') + ->willReturnCallback(function (FileManagerDataCollectionTransfer $fileManagerDataCollectionTransfer) { + return $fileManagerDataCollectionTransfer; + }); + + return $fileManagerDataCollectionExpanderPluginMock; + } + + /** + * @return \Generated\Shared\Transfer\FileManagerDataTransfer + */ + public function createFileManagerDataTransfer(): FileManagerDataTransfer + { + $fileInfo = new FileInfoTransfer(); + $fileInfo->setVersionName('v10'); + $fileInfo->setVersion(10); + $fileInfo->setSize(17); + $fileInfo->setStorageFileName('new_customer.txt'); + $fileInfo->setType('text'); + $fileInfo->setExtension('txt'); + + $file = new FileTransfer(); + $file->setFileContent('new customer file'); + $file->setFileName('new%customer.txt'); + + $fileManagerDataTransfer = new FileManagerDataTransfer(); + $fileManagerDataTransfer->setContent('new version of the file'); + $fileManagerDataTransfer->setFile($file); + $fileManagerDataTransfer->setFileInfo($fileInfo); + + return $fileManagerDataTransfer; + } }