Skip to content

Commit 66d3974

Browse files
committed
#1507: Refactoring, naming update and removed module sequence
1 parent 7923212 commit 66d3974

File tree

15 files changed

+185
-300
lines changed

15 files changed

+185
-300
lines changed

MediaGalleryRenditions/Model/Config.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
namespace Magento\MediaGalleryRenditions\Model;
1010

1111
use Magento\Framework\App\Config\ScopeConfigInterface;
12-
use Magento\MediaGalleryRenditionsApi\Model\ConfigInterface;
1312

1413
/**
1514
* Class responsible for providing access to Media Gallery Renditions system configuration.
1615
*/
17-
class Config implements ConfigInterface
16+
class Config
1817
{
1918
/**
2019
* Config path for Media Gallery Renditions Width
@@ -32,7 +31,6 @@ class Config implements ConfigInterface
3231
private $scopeConfig;
3332

3433
/**
35-
* Config constructor.
3634
* @param ScopeConfigInterface $scopeConfig
3735
*/
3836
public function __construct(
@@ -48,7 +46,7 @@ public function __construct(
4846
*/
4947
public function getWidth(): int
5048
{
51-
return (int)$this->scopeConfig->getValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_WIDTH_PATH);
49+
return (int) $this->scopeConfig->getValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_WIDTH_PATH);
5250
}
5351

5452
/**
@@ -58,6 +56,6 @@ public function getWidth(): int
5856
*/
5957
public function getHeight(): int
6058
{
61-
return (int)$this->scopeConfig->getValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_HEIGHT_PATH);
59+
return (int) $this->scopeConfig->getValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_HEIGHT_PATH);
6260
}
6361
}

MediaGalleryRenditions/Model/GenerateRenditionImages.php

Lines changed: 0 additions & 57 deletions
This file was deleted.

MediaGalleryRenditions/Model/GenerateRenditions.php

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
namespace Magento\MediaGalleryRenditions\Model;
99

1010
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\Exception\LocalizedException;
1112
use Magento\Framework\Filesystem;
13+
use Magento\Framework\Filesystem\Driver\File;
1214
use Magento\Framework\Image\AdapterFactory;
13-
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
1415
use Magento\MediaGalleryRenditionsApi\Api\GenerateRenditionsInterface;
1516
use Magento\MediaGalleryRenditionsApi\Api\GetRenditionPathInterface;
16-
use Magento\MediaGallerySynchronization\Model\CreateAssetFromFile;
17-
use Magento\Framework\Filesystem\Driver\File;
1817

1918
class GenerateRenditions implements GenerateRenditionsInterface
2019
{
@@ -24,79 +23,108 @@ class GenerateRenditions implements GenerateRenditionsInterface
2423
private $imageFactory;
2524

2625
/**
27-
* @var GetRenditionPathInterface
26+
* @var Config
2827
*/
29-
private $getRenditionPath;
28+
private $config;
3029

3130
/**
32-
* @var CreateAssetFromFile
31+
* @var GetRenditionPathInterface
3332
*/
34-
private $createAssetFromFile;
33+
private $getRenditionPath;
3534

3635
/**
3736
* @var Filesystem
3837
*/
3938
private $filesystem;
4039

4140
/**
42-
* @var IsRenditionImageResizeable
41+
* @var IsRenditionRequired
4342
*/
44-
private $isRenditionImageResizeable;
43+
private $isRenditionRequired;
4544

4645
/**
4746
* @var File
4847
*/
4948
private $driver;
5049

5150
/**
52-
* GenerateRenditions constructor.
5351
* @param AdapterFactory $imageFactory
52+
* @param Config $config
5453
* @param GetRenditionPathInterface $getRenditionPath
55-
* @param CreateAssetFromFile $createAssetFromFile
5654
* @param Filesystem $filesystem
5755
* @param File $driver
58-
* @param IsRenditionImageResizeable $isRenditionImageResizeable
56+
* @param IsRenditionRequired $isRenditionRequired
5957
*/
6058
public function __construct(
6159
AdapterFactory $imageFactory,
60+
Config $config,
6261
GetRenditionPathInterface $getRenditionPath,
63-
CreateAssetFromFile $createAssetFromFile,
6462
Filesystem $filesystem,
6563
File $driver,
66-
IsRenditionImageResizeable $isRenditionImageResizeable
64+
IsRenditionRequired $isRenditionRequired
6765
) {
6866
$this->imageFactory = $imageFactory;
67+
$this->config = $config;
6968
$this->getRenditionPath = $getRenditionPath;
70-
$this->createAssetFromFile = $createAssetFromFile;
7169
$this->filesystem = $filesystem;
72-
$this->isRenditionImageResizeable = $isRenditionImageResizeable;
70+
$this->isRenditionRequired = $isRenditionRequired;
7371
$this->driver = $driver;
7472
}
7573

7674
/**
77-
* @inheritDoc
75+
* @inheritdoc
7876
*/
79-
public function execute(array $assets): void
77+
public function execute(array $paths): void
8078
{
81-
/* @var $asset AssetInterface */
82-
foreach ($assets as $asset) {
79+
foreach ($paths as $path) {
8380
$mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
84-
$path = $mediaDirectory->getAbsolutePath($asset->getPath());
85-
if (!$this->isRenditionImageResizeable->execute($asset)) {
81+
$absolutePath = $mediaDirectory->getAbsolutePath($path);
82+
if (!$this->isRenditionRequired->execute($absolutePath)) {
8683
continue;
8784
}
88-
$renditionImagePath = $this->getRenditionPath->execute($asset);
89-
$renditionDirectoryPath = $this->driver->getParentDirectory($renditionImagePath);
85+
86+
$renditionPath = $this->getRenditionPath->execute($path);
87+
$this->createDirectory($renditionPath);
88+
89+
try {
90+
$this->createRendition($absolutePath, $mediaDirectory->getAbsolutePath($renditionPath));
91+
} catch (\Exception $exception) {
92+
throw new LocalizedException(
93+
__('Cannot create rendition for media asset %path', ['path' => $path])
94+
);
95+
}
96+
}
97+
}
98+
99+
/**
100+
* Create directory for rendition file
101+
*
102+
* @param string $path
103+
* @throws LocalizedException
104+
*/
105+
private function createDirectory(string $path): void
106+
{
107+
try {
90108
$this->filesystem->getDirectoryWrite(DirectoryList::MEDIA)
91-
->create($renditionDirectoryPath);
92-
$image = $this->imageFactory->create();
93-
$image->open($path);
94-
$image->keepAspectRatio(true);
95-
$image->resize(
96-
$this->isRenditionImageResizeable->getResizedWidth(),
97-
$this->isRenditionImageResizeable->getResizedHeight()
98-
);
99-
$image->save($mediaDirectory->getAbsolutePath($renditionImagePath));
109+
->create($this->driver->getParentDirectory($path));
110+
} catch (\Exception $exception) {
111+
throw new LocalizedException(__('Cannot create directory for rendition %path', ['path' => $path]));
100112
}
101113
}
114+
115+
/**
116+
* Create rendition file
117+
*
118+
* @param string $absolutePath
119+
* @param string $absoluteRenditionPath
120+
* @throws \Exception
121+
*/
122+
private function createRendition(string $absolutePath, string $absoluteRenditionPath): void
123+
{
124+
$image = $this->imageFactory->create();
125+
$image->open($absolutePath);
126+
$image->keepAspectRatio(true);
127+
$image->resize($this->config->getWidth(), $this->config->getHeight());
128+
$image->save($absoluteRenditionPath);
129+
}
102130
}

MediaGalleryRenditions/Model/GetRenditionPath.php

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,65 @@
88
namespace Magento\MediaGalleryRenditions\Model;
99

1010
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\Exception\LocalizedException;
1112
use Magento\Framework\Filesystem;
12-
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
13+
use Magento\Framework\Filesystem\Directory\ReadInterface;
1314
use Magento\MediaGalleryRenditionsApi\Api\GetRenditionPathInterface;
1415

1516
class GetRenditionPath implements GetRenditionPathInterface
1617
{
1718
private const RENDITIONS_DIRECTORY_NAME = '.renditions';
1819

1920
/**
20-
* @var Filesystem\Directory\WriteInterface
21+
* @var Filesystem
2122
*/
22-
private $directory;
23+
private $filesystem;
2324

2425
/**
25-
* @var IsRenditionImageResizeable
26+
* @var IsRenditionRequired
2627
*/
27-
private $isRenditionImageResizeable;
28+
private $isRenditionRequired;
2829

2930
/**
3031
* @param Filesystem $filesystem
31-
* @param IsRenditionImageResizeable $isRenditionImageResizeable
32+
* @param IsRenditionRequired $isRenditionRequired
3233
*/
3334
public function __construct(
3435
Filesystem $filesystem,
35-
IsRenditionImageResizeable $isRenditionImageResizeable
36+
IsRenditionRequired $isRenditionRequired
3637
) {
37-
$this->directory = $filesystem->getDirectoryRead(DirectoryList::MEDIA);
38-
$this->isRenditionImageResizeable = $isRenditionImageResizeable;
38+
$this->filesystem = $filesystem;
39+
$this->isRenditionRequired = $isRenditionRequired;
3940
}
4041

4142
/**
4243
* Returns Rendition image path
4344
*
44-
* @param AssetInterface $asset
45+
* @param string $path
4546
* @return string
4647
*/
47-
public function execute(AssetInterface $asset) :string
48+
public function execute(string $path) :string
4849
{
49-
if (!$this->directory->isFile($asset->getPath())) {
50-
throw new \InvalidArgumentException(__('Incorrect asset path!'));
50+
$mediaDirectory = $this->getMediaDirectory();
51+
52+
if (!$mediaDirectory->isFile($path)) {
53+
throw new LocalizedException(__('Media asset file %path does not exist!', ['path' => $path]));
5154
}
52-
if (!$this->isRenditionImageResizeable->execute($asset)) {
53-
return $asset->getPath();
55+
56+
if (!$this->isRenditionRequired->execute($mediaDirectory->getAbsolutePath($path))) {
57+
return $path;
5458
}
5559

56-
return $this->directory->getRelativePath(
57-
self::RENDITIONS_DIRECTORY_NAME . '/' .
58-
$this->directory->getRelativePath(ltrim($asset->getPath(), '/'))
59-
);
60+
return self::RENDITIONS_DIRECTORY_NAME . '/' . ltrim($path, '/');
61+
}
62+
63+
/**
64+
* Retrieve media directory instance with read access
65+
*
66+
* @return ReadInterface
67+
*/
68+
private function getMediaDirectory(): ReadInterface
69+
{
70+
return $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
6071
}
6172
}

MediaGalleryRenditions/Model/IsRenditionImageResizeable.php

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)