Skip to content

Commit

Permalink
ENGCOM-8011: #1727: Introduce internal class wrapping SplFileInfo #29461
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldagama authored Aug 19, 2020
2 parents 77084b0 + c49766f commit 54633c4
Show file tree
Hide file tree
Showing 6 changed files with 305 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -60,9 +60,9 @@ class CreateAssetFromFile
private $extractMetadata;

/**
* @var SplFileInfoFactory
* @var GetFileInfo
*/
private $splFileInfoFactory;
private $getFileInfo;

/**
* @param Filesystem $filesystem
Expand All @@ -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,
Expand All @@ -80,15 +80,15 @@ public function __construct(
AssetInterfaceFactory $assetFactory,
GetContentHash $getContentHash,
ExtractMetadataInterface $extractMetadata,
SplFileInfoFactory $splFileInfoFactory
GetFileInfo $getFileInfo
) {
$this->filesystem = $filesystem;
$this->driver = $driver;
$this->date = $date;
$this->assetFactory = $assetFactory;
$this->getContentHash = $getContentHash;
$this->extractMetadata = $extractMetadata;
$this->splFileInfoFactory = $splFileInfoFactory;
$this->getFileInfo = $getFileInfo;
}

/**
Expand All @@ -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);
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaGallerySynchronization\Model\Filesystem;

/**
* Class for getting image file information.
*/
class FileInfo
{
/**
* @var string
*/
private $path;

/**
* @var string
*/
private $filename;

/**
* @var string
*/
private $extension;

/**
* @var $basename
*/
private $basename;

/**
* @var int
*/
private $size;

/**
* @var int
*/
private $mTime;

/**
* @var int
*/
private $cTime;

/**
* FileInfo constructor.
*
* @param string $path
* @param string $filename
* @param string $extension
* @param string $basename
* @param int $size
* @param int $mTime
* @param int $cTime
*/
public function __construct(
string $path,
string $filename,
string $extension,
string $basename,
int $size,
int $mTime,
int $cTime
) {
$this->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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaGallerySynchronization\Model\Filesystem;

use Magento\MediaGallerySynchronization\Model\Filesystem\FileInfoFactory;

/**
* Get file information
*/
class GetFileInfo
{
/**
* @var FileInfoFactory
*/
private $fileInfoFactory;

/**
* GetFileInfo constructor.
* @param FileInfoFactory $fileInfoFactory
*/
public function __construct(
FileInfoFactory $fileInfoFactory
) {
$this->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()
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -50,9 +50,9 @@ class SynchronizeFiles implements SynchronizeFilesInterface
private $driver;

/**
* @var SplFileInfoFactory
* @var GetFileInfo
*/
private $splFileInfoFactory;
private $getFileInfo;

/**
* @var ImportFilesInterface
Expand All @@ -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
*/
Expand All @@ -78,15 +78,15 @@ public function __construct(
Filesystem $filesystem,
DateTime $date,
LoggerInterface $log,
SplFileInfoFactory $splFileInfoFactory,
GetFileInfo $getFileInfo,
GetAssetsByPathsInterface $getAssetsByPaths,
ImportFilesInterface $importFiles
) {
$this->driver = $driver;
$this->filesystem = $filesystem;
$this->date = $date;
$this->log = $log;
$this->splFileInfoFactory = $splFileInfoFactory;
$this->getFileInfo = $getFileInfo;
$this->getAssetsByPaths = $getAssetsByPaths;
$this->importFiles = $importFiles;
}
Expand Down Expand Up @@ -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()
);
}

Expand Down
Loading

0 comments on commit 54633c4

Please sign in to comment.