Skip to content

Commit c49766f

Browse files
ENGCOM-8011: #1727: Introduce internal class wrapping SplFileInfo #29461
- Merge Pull Request #29461 from joweecaquicla/magento2:1727-introduce-internal-class-wrapping-splfileinfo - Merged commits: 1. 5a500f3 2. bc24c44 3. 9f02257 4. 0b6ba91 5. 89e7f22 6. beae26b 7. 3752c1e
2 parents 4930963 + 3752c1e commit c49766f

File tree

6 files changed

+305
-25
lines changed

6 files changed

+305
-25
lines changed

app/code/Magento/MediaGallerySynchronization/Model/CreateAssetFromFile.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
1717
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory;
1818
use Magento\MediaGalleryMetadataApi\Api\ExtractMetadataInterface;
19-
use Magento\MediaGallerySynchronization\Model\Filesystem\SplFileInfoFactory;
19+
use Magento\MediaGallerySynchronization\Model\Filesystem\GetFileInfo;
2020
use Magento\MediaGallerySynchronization\Model\GetContentHash;
2121

2222
/**
@@ -60,9 +60,9 @@ class CreateAssetFromFile
6060
private $extractMetadata;
6161

6262
/**
63-
* @var SplFileInfoFactory
63+
* @var GetFileInfo
6464
*/
65-
private $splFileInfoFactory;
65+
private $getFileInfo;
6666

6767
/**
6868
* @param Filesystem $filesystem
@@ -71,7 +71,7 @@ class CreateAssetFromFile
7171
* @param AssetInterfaceFactory $assetFactory
7272
* @param GetContentHash $getContentHash
7373
* @param ExtractMetadataInterface $extractMetadata
74-
* @param SplFileInfoFactory $splFileInfoFactory
74+
* @param GetFileInfo $getFileInfo
7575
*/
7676
public function __construct(
7777
Filesystem $filesystem,
@@ -80,15 +80,15 @@ public function __construct(
8080
AssetInterfaceFactory $assetFactory,
8181
GetContentHash $getContentHash,
8282
ExtractMetadataInterface $extractMetadata,
83-
SplFileInfoFactory $splFileInfoFactory
83+
GetFileInfo $getFileInfo
8484
) {
8585
$this->filesystem = $filesystem;
8686
$this->driver = $driver;
8787
$this->date = $date;
8888
$this->assetFactory = $assetFactory;
8989
$this->getContentHash = $getContentHash;
9090
$this->extractMetadata = $extractMetadata;
91-
$this->splFileInfoFactory = $splFileInfoFactory;
91+
$this->getFileInfo = $getFileInfo;
9292
}
9393

9494
/**
@@ -101,7 +101,7 @@ public function __construct(
101101
public function execute(string $path): AssetInterface
102102
{
103103
$absolutePath = $this->getMediaDirectory()->getAbsolutePath($path);
104-
$file = $this->splFileInfoFactory->create($absolutePath);
104+
$file = $this->getFileInfo->execute($absolutePath);
105105
[$width, $height] = getimagesize($absolutePath);
106106

107107
$metadata = $this->extractMetadata->execute($absolutePath);
@@ -110,7 +110,7 @@ public function execute(string $path): AssetInterface
110110
[
111111
'id' => null,
112112
'path' => $path,
113-
'title' => $metadata->getTitle() ?: $file->getBasename('.' . $file->getExtension()),
113+
'title' => $metadata->getTitle() ?: $file->getBasename(),
114114
'description' => $metadata->getDescription(),
115115
'createdAt' => $this->date->date($file->getCTime())->format(self::DATE_FORMAT),
116116
'updatedAt' => $this->date->date($file->getMTime())->format(self::DATE_FORMAT),
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaGallerySynchronization\Model\Filesystem;
9+
10+
/**
11+
* Class for getting image file information.
12+
*/
13+
class FileInfo
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private $path;
19+
20+
/**
21+
* @var string
22+
*/
23+
private $filename;
24+
25+
/**
26+
* @var string
27+
*/
28+
private $extension;
29+
30+
/**
31+
* @var $basename
32+
*/
33+
private $basename;
34+
35+
/**
36+
* @var int
37+
*/
38+
private $size;
39+
40+
/**
41+
* @var int
42+
*/
43+
private $mTime;
44+
45+
/**
46+
* @var int
47+
*/
48+
private $cTime;
49+
50+
/**
51+
* FileInfo constructor.
52+
*
53+
* @param string $path
54+
* @param string $filename
55+
* @param string $extension
56+
* @param string $basename
57+
* @param int $size
58+
* @param int $mTime
59+
* @param int $cTime
60+
*/
61+
public function __construct(
62+
string $path,
63+
string $filename,
64+
string $extension,
65+
string $basename,
66+
int $size,
67+
int $mTime,
68+
int $cTime
69+
) {
70+
$this->path = $path;
71+
$this->filename = $filename;
72+
$this->extension = $extension;
73+
$this->basename = $basename;
74+
$this->size = $size;
75+
$this->mTime = $mTime;
76+
$this->cTime = $cTime;
77+
}
78+
79+
/**
80+
* Get path without filename.
81+
*
82+
* @return string
83+
*/
84+
public function getPath(): string
85+
{
86+
return $this->path;
87+
}
88+
89+
/**
90+
* Get filename.
91+
*
92+
* @return string
93+
*/
94+
public function getFilename(): string
95+
{
96+
return $this->filename;
97+
}
98+
99+
/**
100+
* Get file extension.
101+
*
102+
* @return string
103+
*/
104+
public function getExtension(): string
105+
{
106+
return $this->extension;
107+
}
108+
109+
/**
110+
* Get file basename.
111+
*
112+
* @return string
113+
*/
114+
public function getBasename(): string
115+
{
116+
return $this->basename;
117+
}
118+
119+
/**
120+
* Get file size.
121+
*
122+
* @return int
123+
*/
124+
public function getSize(): int
125+
{
126+
return $this->size;
127+
}
128+
129+
/**
130+
* Get last modified time.
131+
*
132+
* @return int
133+
*/
134+
public function getMTime(): int
135+
{
136+
return $this->mTime;
137+
}
138+
139+
/**
140+
* Get inode change time.
141+
*
142+
* @return int
143+
*/
144+
public function getCTime(): int
145+
{
146+
return $this->cTime;
147+
}
148+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaGallerySynchronization\Model\Filesystem;
9+
10+
use Magento\MediaGallerySynchronization\Model\Filesystem\FileInfoFactory;
11+
12+
/**
13+
* Get file information
14+
*/
15+
class GetFileInfo
16+
{
17+
/**
18+
* @var FileInfoFactory
19+
*/
20+
private $fileInfoFactory;
21+
22+
/**
23+
* GetFileInfo constructor.
24+
* @param FileInfoFactory $fileInfoFactory
25+
*/
26+
public function __construct(
27+
FileInfoFactory $fileInfoFactory
28+
) {
29+
$this->fileInfoFactory = $fileInfoFactory;
30+
}
31+
32+
/**
33+
* Get file information based on path provided.
34+
*
35+
* @param string $path
36+
* @return FileInfo
37+
*/
38+
public function execute(string $path): FileInfo
39+
{
40+
$splFileInfo = new \SplFileInfo($path);
41+
42+
return $this->fileInfoFactory->create([
43+
'path' => $splFileInfo->getPath(),
44+
'filename' => $splFileInfo->getFilename(),
45+
'extension' => $splFileInfo->getExtension(),
46+
'basename' => $splFileInfo->getBasename('.' . $splFileInfo->getExtension()),
47+
'size' => $splFileInfo->getSize(),
48+
'mTime' => $splFileInfo->getMTime(),
49+
'cTime' => $splFileInfo->getCTime()
50+
]);
51+
}
52+
}

app/code/Magento/MediaGallerySynchronization/Model/GetAssetFromPath.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
1313
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory;
1414
use Magento\MediaGalleryApi\Api\GetAssetsByPathsInterface;
15-
use Magento\MediaGallerySynchronization\Model\Filesystem\SplFileInfoFactory;
1615

1716
/**
1817
* Create media asset object based on the file information
@@ -34,27 +33,19 @@ class GetAssetFromPath
3433
*/
3534
private $createAssetFromFile;
3635

37-
/**
38-
* @var SplFileInfoFactory
39-
*/
40-
private $splFileInfoFactory;
41-
4236
/**
4337
* @param AssetInterfaceFactory $assetFactory
4438
* @param GetAssetsByPathsInterface $getMediaGalleryAssetByPath
4539
* @param CreateAssetFromFile $createAssetFromFile
46-
* @param SplFileInfoFactory $splFileInfoFactory
4740
*/
4841
public function __construct(
4942
AssetInterfaceFactory $assetFactory,
5043
GetAssetsByPathsInterface $getMediaGalleryAssetByPath,
51-
CreateAssetFromFile $createAssetFromFile,
52-
SplFileInfoFactory $splFileInfoFactory
44+
CreateAssetFromFile $createAssetFromFile
5345
) {
5446
$this->assetFactory = $assetFactory;
5547
$this->getAssetsByPaths = $getMediaGalleryAssetByPath;
5648
$this->createAssetFromFile = $createAssetFromFile;
57-
$this->splFileInfoFactory= $splFileInfoFactory;
5849
}
5950

6051
/**

app/code/Magento/MediaGallerySynchronization/Model/SynchronizeFiles.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Magento\MediaGalleryApi\Api\GetAssetsByPathsInterface;
1717
use Magento\MediaGallerySynchronizationApi\Model\ImportFilesInterface;
1818
use Magento\MediaGallerySynchronizationApi\Api\SynchronizeFilesInterface;
19-
use Magento\MediaGallerySynchronization\Model\Filesystem\SplFileInfoFactory;
19+
use Magento\MediaGallerySynchronization\Model\Filesystem\GetFileInfo;
2020
use Psr\Log\LoggerInterface;
2121

2222
/**
@@ -50,9 +50,9 @@ class SynchronizeFiles implements SynchronizeFilesInterface
5050
private $driver;
5151

5252
/**
53-
* @var SplFileInfoFactory
53+
* @var GetFileInfo
5454
*/
55-
private $splFileInfoFactory;
55+
private $getFileInfo;
5656

5757
/**
5858
* @var ImportFilesInterface
@@ -69,7 +69,7 @@ class SynchronizeFiles implements SynchronizeFilesInterface
6969
* @param Filesystem $filesystem
7070
* @param DateTime $date
7171
* @param LoggerInterface $log
72-
* @param SplFileInfoFactory $splFileInfoFactory
72+
* @param GetFileInfo $getFileInfo
7373
* @param GetAssetsByPathsInterface $getAssetsByPaths
7474
* @param ImportFilesInterface $importFiles
7575
*/
@@ -78,15 +78,15 @@ public function __construct(
7878
Filesystem $filesystem,
7979
DateTime $date,
8080
LoggerInterface $log,
81-
SplFileInfoFactory $splFileInfoFactory,
81+
GetFileInfo $getFileInfo,
8282
GetAssetsByPathsInterface $getAssetsByPaths,
8383
ImportFilesInterface $importFiles
8484
) {
8585
$this->driver = $driver;
8686
$this->filesystem = $filesystem;
8787
$this->date = $date;
8888
$this->log = $log;
89-
$this->splFileInfoFactory = $splFileInfoFactory;
89+
$this->getFileInfo = $getFileInfo;
9090
$this->getAssetsByPaths = $getAssetsByPaths;
9191
$this->importFiles = $importFiles;
9292
}
@@ -150,7 +150,7 @@ private function getFileModificationTime(string $path): string
150150
{
151151
return $this->date->gmtDate(
152152
self::DATE_FORMAT,
153-
$this->splFileInfoFactory->create($this->getMediaDirectory()->getAbsolutePath($path))->getMTime()
153+
$this->getFileInfo->execute($this->getMediaDirectory()->getAbsolutePath($path))->getMTime()
154154
);
155155
}
156156

0 commit comments

Comments
 (0)