diff --git a/app/code/Magento/MediaGallerySynchronization/Model/CreateAssetFromFile.php b/app/code/Magento/MediaGallerySynchronization/Model/CreateAssetFromFile.php index 87d477507b680..b257c3da55c84 100644 --- a/app/code/Magento/MediaGallerySynchronization/Model/CreateAssetFromFile.php +++ b/app/code/Magento/MediaGallerySynchronization/Model/CreateAssetFromFile.php @@ -16,7 +16,7 @@ use Magento\MediaGalleryApi\Api\Data\AssetInterface; use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory; use Magento\MediaGalleryMetadataApi\Api\ExtractMetadataInterface; -use Magento\MediaGallerySynchronization\Model\Filesystem\SplFileInfoFactory; +use Magento\MediaGallerySynchronization\Model\Filesystem\GetFileInfo; use Magento\MediaGallerySynchronization\Model\GetContentHash; /** @@ -60,9 +60,9 @@ class CreateAssetFromFile private $extractMetadata; /** - * @var SplFileInfoFactory + * @var GetFileInfo */ - private $splFileInfoFactory; + private $getFileInfo; /** * @param Filesystem $filesystem @@ -71,7 +71,7 @@ class CreateAssetFromFile * @param AssetInterfaceFactory $assetFactory * @param GetContentHash $getContentHash * @param ExtractMetadataInterface $extractMetadata - * @param SplFileInfoFactory $splFileInfoFactory + * @param GetFileInfo $getFileInfo */ public function __construct( Filesystem $filesystem, @@ -80,7 +80,7 @@ public function __construct( AssetInterfaceFactory $assetFactory, GetContentHash $getContentHash, ExtractMetadataInterface $extractMetadata, - SplFileInfoFactory $splFileInfoFactory + GetFileInfo $getFileInfo ) { $this->filesystem = $filesystem; $this->driver = $driver; @@ -88,7 +88,7 @@ public function __construct( $this->assetFactory = $assetFactory; $this->getContentHash = $getContentHash; $this->extractMetadata = $extractMetadata; - $this->splFileInfoFactory = $splFileInfoFactory; + $this->getFileInfo = $getFileInfo; } /** @@ -101,7 +101,7 @@ public function __construct( public function execute(string $path): AssetInterface { $absolutePath = $this->getMediaDirectory()->getAbsolutePath($path); - $file = $this->splFileInfoFactory->create($absolutePath); + $file = $this->getFileInfo->execute($absolutePath); [$width, $height] = getimagesize($absolutePath); $metadata = $this->extractMetadata->execute($absolutePath); @@ -110,7 +110,7 @@ public function execute(string $path): AssetInterface [ 'id' => null, 'path' => $path, - 'title' => $metadata->getTitle() ?: $file->getBasename('.' . $file->getExtension()), + 'title' => $metadata->getTitle() ?: $file->getBasename(), 'description' => $metadata->getDescription(), 'createdAt' => $this->date->date($file->getCTime())->format(self::DATE_FORMAT), 'updatedAt' => $this->date->date($file->getMTime())->format(self::DATE_FORMAT), diff --git a/app/code/Magento/MediaGallerySynchronization/Model/Filesystem/FileInfo.php b/app/code/Magento/MediaGallerySynchronization/Model/Filesystem/FileInfo.php new file mode 100644 index 0000000000000..5e523fd0e905a --- /dev/null +++ b/app/code/Magento/MediaGallerySynchronization/Model/Filesystem/FileInfo.php @@ -0,0 +1,148 @@ +path = $path; + $this->filename = $filename; + $this->extension = $extension; + $this->basename = $basename; + $this->size = $size; + $this->mTime = $mTime; + $this->cTime = $cTime; + } + + /** + * Get path without filename. + * + * @return string + */ + public function getPath(): string + { + return $this->path; + } + + /** + * Get filename. + * + * @return string + */ + public function getFilename(): string + { + return $this->filename; + } + + /** + * Get file extension. + * + * @return string + */ + public function getExtension(): string + { + return $this->extension; + } + + /** + * Get file basename. + * + * @return string + */ + public function getBasename(): string + { + return $this->basename; + } + + /** + * Get file size. + * + * @return int + */ + public function getSize(): int + { + return $this->size; + } + + /** + * Get last modified time. + * + * @return int + */ + public function getMTime(): int + { + return $this->mTime; + } + + /** + * Get inode change time. + * + * @return int + */ + public function getCTime(): int + { + return $this->cTime; + } +} diff --git a/app/code/Magento/MediaGallerySynchronization/Model/Filesystem/GetFileInfo.php b/app/code/Magento/MediaGallerySynchronization/Model/Filesystem/GetFileInfo.php new file mode 100644 index 0000000000000..8f9080767d6e3 --- /dev/null +++ b/app/code/Magento/MediaGallerySynchronization/Model/Filesystem/GetFileInfo.php @@ -0,0 +1,52 @@ +fileInfoFactory = $fileInfoFactory; + } + + /** + * Get file information based on path provided. + * + * @param string $path + * @return FileInfo + */ + public function execute(string $path): FileInfo + { + $splFileInfo = new \SplFileInfo($path); + + return $this->fileInfoFactory->create([ + 'path' => $splFileInfo->getPath(), + 'filename' => $splFileInfo->getFilename(), + 'extension' => $splFileInfo->getExtension(), + 'basename' => $splFileInfo->getBasename('.' . $splFileInfo->getExtension()), + 'size' => $splFileInfo->getSize(), + 'mTime' => $splFileInfo->getMTime(), + 'cTime' => $splFileInfo->getCTime() + ]); + } +} diff --git a/app/code/Magento/MediaGallerySynchronization/Model/GetAssetFromPath.php b/app/code/Magento/MediaGallerySynchronization/Model/GetAssetFromPath.php index 5e825d57c5ce7..533d814c9f1d0 100644 --- a/app/code/Magento/MediaGallerySynchronization/Model/GetAssetFromPath.php +++ b/app/code/Magento/MediaGallerySynchronization/Model/GetAssetFromPath.php @@ -12,7 +12,6 @@ use Magento\MediaGalleryApi\Api\Data\AssetInterface; use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory; use Magento\MediaGalleryApi\Api\GetAssetsByPathsInterface; -use Magento\MediaGallerySynchronization\Model\Filesystem\SplFileInfoFactory; /** * Create media asset object based on the file information @@ -34,27 +33,19 @@ class GetAssetFromPath */ private $createAssetFromFile; - /** - * @var SplFileInfoFactory - */ - private $splFileInfoFactory; - /** * @param AssetInterfaceFactory $assetFactory * @param GetAssetsByPathsInterface $getMediaGalleryAssetByPath * @param CreateAssetFromFile $createAssetFromFile - * @param SplFileInfoFactory $splFileInfoFactory */ public function __construct( AssetInterfaceFactory $assetFactory, GetAssetsByPathsInterface $getMediaGalleryAssetByPath, - CreateAssetFromFile $createAssetFromFile, - SplFileInfoFactory $splFileInfoFactory + CreateAssetFromFile $createAssetFromFile ) { $this->assetFactory = $assetFactory; $this->getAssetsByPaths = $getMediaGalleryAssetByPath; $this->createAssetFromFile = $createAssetFromFile; - $this->splFileInfoFactory= $splFileInfoFactory; } /** diff --git a/app/code/Magento/MediaGallerySynchronization/Model/SynchronizeFiles.php b/app/code/Magento/MediaGallerySynchronization/Model/SynchronizeFiles.php index 81e9629f703f3..eebb172e48202 100644 --- a/app/code/Magento/MediaGallerySynchronization/Model/SynchronizeFiles.php +++ b/app/code/Magento/MediaGallerySynchronization/Model/SynchronizeFiles.php @@ -16,7 +16,7 @@ use Magento\MediaGalleryApi\Api\GetAssetsByPathsInterface; use Magento\MediaGallerySynchronizationApi\Model\ImportFilesInterface; use Magento\MediaGallerySynchronizationApi\Api\SynchronizeFilesInterface; -use Magento\MediaGallerySynchronization\Model\Filesystem\SplFileInfoFactory; +use Magento\MediaGallerySynchronization\Model\Filesystem\GetFileInfo; use Psr\Log\LoggerInterface; /** @@ -50,9 +50,9 @@ class SynchronizeFiles implements SynchronizeFilesInterface private $driver; /** - * @var SplFileInfoFactory + * @var GetFileInfo */ - private $splFileInfoFactory; + private $getFileInfo; /** * @var ImportFilesInterface @@ -69,7 +69,7 @@ class SynchronizeFiles implements SynchronizeFilesInterface * @param Filesystem $filesystem * @param DateTime $date * @param LoggerInterface $log - * @param SplFileInfoFactory $splFileInfoFactory + * @param GetFileInfo $getFileInfo * @param GetAssetsByPathsInterface $getAssetsByPaths * @param ImportFilesInterface $importFiles */ @@ -78,7 +78,7 @@ public function __construct( Filesystem $filesystem, DateTime $date, LoggerInterface $log, - SplFileInfoFactory $splFileInfoFactory, + GetFileInfo $getFileInfo, GetAssetsByPathsInterface $getAssetsByPaths, ImportFilesInterface $importFiles ) { @@ -86,7 +86,7 @@ public function __construct( $this->filesystem = $filesystem; $this->date = $date; $this->log = $log; - $this->splFileInfoFactory = $splFileInfoFactory; + $this->getFileInfo = $getFileInfo; $this->getAssetsByPaths = $getAssetsByPaths; $this->importFiles = $importFiles; } @@ -150,7 +150,7 @@ private function getFileModificationTime(string $path): string { return $this->date->gmtDate( self::DATE_FORMAT, - $this->splFileInfoFactory->create($this->getMediaDirectory()->getAbsolutePath($path))->getMTime() + $this->getFileInfo->execute($this->getMediaDirectory()->getAbsolutePath($path))->getMTime() ); } diff --git a/app/code/Magento/MediaGallerySynchronization/Test/Integration/Model/Filesystem/GetFileInfoTest.php b/app/code/Magento/MediaGallerySynchronization/Test/Integration/Model/Filesystem/GetFileInfoTest.php new file mode 100644 index 0000000000000..6b1e8a676d02b --- /dev/null +++ b/app/code/Magento/MediaGallerySynchronization/Test/Integration/Model/Filesystem/GetFileInfoTest.php @@ -0,0 +1,89 @@ +getFileInfo = Bootstrap::getObjectManager()->get(GetFileInfo::class); + } + + /** + * @dataProvider filesProvider + * @param string $file + */ + public function testExecute( + string $file + ): void { + + $path = $this->getImageFilePath($file); + + $fileInfo = $this->getFileInfo->execute($path); + $expectedResult = new \SplFileInfo($path); + $this->assertEquals($expectedResult->getPath(), $fileInfo->getPath()); + $this->assertEquals($expectedResult->getFilename(), $fileInfo->getFilename()); + $this->assertEquals($expectedResult->getExtension(), $fileInfo->getExtension()); + $this->assertEquals( + $expectedResult->getBasename('.' . $expectedResult->getExtension()), + $fileInfo->getBasename() + ); + $this->assertEquals($expectedResult->getSize(), $fileInfo->getSize()); + $this->assertEquals($expectedResult->getMTime(), $fileInfo->getMTime()); + $this->assertEquals($expectedResult->getCTime(), $fileInfo->getCTime()); + } + + /** + * Data provider for testExecute + * + * @return array[] + */ + public function filesProvider(): array + { + return [ + [ + 'magento.jpg', + 'magento_2.jpg' + ] + ]; + } + + /** + * Return image file path + * + * @param string $filename + * @return string + */ + private function getImageFilePath(string $filename): string + { + return dirname(__DIR__, 2) + . DIRECTORY_SEPARATOR + . implode( + DIRECTORY_SEPARATOR, + [ + '_files', + $filename + ] + ); + } +}