Skip to content

Commit 4a71d53

Browse files
Refactoring GetRenditionPath class
1 parent e7daa20 commit 4a71d53

File tree

3 files changed

+101
-109
lines changed

3 files changed

+101
-109
lines changed

MediaGalleryRenditions/Model/GenerateRenditions.php

Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
1414
use Magento\MediaGalleryRenditionsApi\Api\GenerateRenditionsInterface;
1515
use Magento\MediaGalleryRenditionsApi\Api\GetRenditionPathInterface;
16-
use Magento\MediaGalleryRenditionsApi\Model\ConfigInterface;
1716
use Magento\MediaGallerySynchronization\Model\CreateAssetFromFile;
17+
use Magento\Framework\Filesystem\Driver\File;
1818

1919
class GenerateRenditions implements GenerateRenditionsInterface
2020
{
@@ -23,11 +23,6 @@ class GenerateRenditions implements GenerateRenditionsInterface
2323
*/
2424
private $imageFactory;
2525

26-
/**
27-
* @var ConfigInterface
28-
*/
29-
private $config;
30-
3126
/**
3227
* @var GetRenditionPathInterface
3328
*/
@@ -43,26 +38,39 @@ class GenerateRenditions implements GenerateRenditionsInterface
4338
*/
4439
private $filesystem;
4540

41+
/**
42+
* @var IsRenditionImageResizeable
43+
*/
44+
private $isRenditionImageResizeable;
45+
46+
/**
47+
* @var File
48+
*/
49+
private $driver;
50+
4651
/**
4752
* GenerateRenditions constructor.
4853
* @param AdapterFactory $imageFactory
4954
* @param GetRenditionPathInterface $getRenditionPath
5055
* @param CreateAssetFromFile $createAssetFromFile
5156
* @param Filesystem $filesystem
52-
* @param ConfigInterface $config
57+
* @param File $driver
58+
* @param IsRenditionImageResizeable $isRenditionImageResizeable
5359
*/
5460
public function __construct(
5561
AdapterFactory $imageFactory,
5662
GetRenditionPathInterface $getRenditionPath,
5763
CreateAssetFromFile $createAssetFromFile,
5864
Filesystem $filesystem,
59-
ConfigInterface $config
65+
File $driver,
66+
IsRenditionImageResizeable $isRenditionImageResizeable
6067
) {
6168
$this->imageFactory = $imageFactory;
62-
$this->config = $config;
6369
$this->getRenditionPath = $getRenditionPath;
6470
$this->createAssetFromFile = $createAssetFromFile;
6571
$this->filesystem = $filesystem;
72+
$this->isRenditionImageResizeable = $isRenditionImageResizeable;
73+
$this->driver = $driver;
6674
}
6775

6876
/**
@@ -74,50 +82,21 @@ public function execute(array $assets): void
7482
foreach ($assets as $asset) {
7583
$mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
7684
$path = $mediaDirectory->getAbsolutePath($asset->getPath());
77-
if (!$this->isResizeable($asset->getHeight(), $asset->getWidth())) {
85+
if (!$this->isRenditionImageResizeable->execute($asset)) {
7886
continue;
7987
}
80-
$renditionDirectoryPath = $this->getRenditionPath->execute($asset);
88+
$renditionImagePath = $this->getRenditionPath->execute($asset);
89+
$renditionDirectoryPath = $this->driver->getParentDirectory($renditionImagePath);
8190
$this->filesystem->getDirectoryWrite(DirectoryList::MEDIA)
8291
->create($renditionDirectoryPath);
83-
$renditionImagePath = $renditionDirectoryPath . '/' . $asset->getPath();
8492
$image = $this->imageFactory->create();
8593
$image->open($path);
8694
$image->keepAspectRatio(true);
87-
$image->resize($this->getResizedWidth(), $this->getResizedHeight());
88-
$image->save($renditionImagePath);
95+
$image->resize(
96+
$this->isRenditionImageResizeable->getResizedWidth(),
97+
$this->isRenditionImageResizeable->getResizedHeight()
98+
);
99+
$image->save($mediaDirectory->getAbsolutePath($renditionImagePath));
89100
}
90101
}
91-
92-
/**
93-
* Check if image needs to resize or not
94-
*
95-
* @param int $height
96-
* @param int $width
97-
* @return bool
98-
*/
99-
private function isResizeable(int $height, int $width): bool
100-
{
101-
return $width > $this->getResizedWidth() || $height > $this->getResizedHeight();
102-
}
103-
104-
/**
105-
* Get resized image width
106-
*
107-
* @return int
108-
*/
109-
private function getResizedWidth(): int
110-
{
111-
return $this->config->getWidth();
112-
}
113-
114-
/**
115-
* Get resized image height
116-
*
117-
* @return int
118-
*/
119-
private function getResizedHeight() :int
120-
{
121-
return $this->config->getHeight();
122-
}
123102
}

MediaGalleryRenditions/Model/GetRenditionPath.php

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

1010
use Magento\Framework\App\Filesystem\DirectoryList;
11-
use Magento\Framework\App\ObjectManager;
12-
use Magento\Framework\Exception\FileSystemException;
13-
use Magento\Framework\Exception\LocalizedException;
1411
use Magento\Framework\Filesystem;
15-
use Magento\Framework\Filesystem\DriverInterface;
1612
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
1713
use Magento\MediaGalleryRenditionsApi\Api\GetRenditionPathInterface;
1814

1915
class GetRenditionPath implements GetRenditionPathInterface
2016
{
17+
private const RENDITIONS_DIRECTORY_NAME = '.renditions';
18+
2119
/**
2220
* @var Filesystem\Directory\WriteInterface
2321
*/
2422
private $directory;
2523

2624
/**
27-
* @var DriverInterface|mixed|null
25+
* @var IsRenditionImageResizeable
2826
*/
29-
private $file;
30-
31-
private const RENDITIONS_DIRECTORY_NAME = '.renditions';
27+
private $isRenditionImageResizeable;
3228

3329
/**
3430
* @param Filesystem $filesystem
35-
* @param DriverInterface|null $file
36-
* @throws FileSystemException
31+
* @param IsRenditionImageResizeable $isRenditionImageResizeable
3732
*/
3833
public function __construct(
3934
Filesystem $filesystem,
40-
DriverInterface $file = null
35+
IsRenditionImageResizeable $isRenditionImageResizeable
4136
) {
4237
$this->directory = $filesystem->getDirectoryRead(DirectoryList::MEDIA);
43-
$this->file = $file ?: ObjectManager::getInstance()->get(Filesystem\Driver\File::class);
38+
$this->isRenditionImageResizeable = $isRenditionImageResizeable;
4439
}
4540

4641
/**
4742
* Returns Rendition image path
4843
*
4944
* @param AssetInterface $asset
5045
* @return string
51-
* @throws LocalizedException
5246
*/
5347
public function execute(AssetInterface $asset) :string
5448
{
55-
return $this->getRenditionsImageDirectory($asset->getPath());
56-
}
57-
58-
/**
59-
* Return renditions directory path for file/current directory
60-
*
61-
* @param string $filePath Path to the file
62-
* @return string
63-
*/
64-
private function getRenditionsImageDirectory(string $filePath) :string
65-
{
66-
$renditionRootDir = $this->getRenditionsRoot();
67-
68-
if ($filePath) {
69-
$renditionImagePath = $this->getRenditionsPath($filePath);
70-
if ($renditionImagePath) {
71-
$renditionImageDir = $this->file->getParentDirectory($renditionImagePath);
72-
}
49+
if (!$this->directory->isFile($asset->getPath())) {
50+
throw new \InvalidArgumentException(__('Incorrect asset path!'));
7351
}
74-
return $renditionImageDir ?? $renditionRootDir;
75-
}
76-
77-
/**
78-
* Renditions root directory getter
79-
*
80-
* @return string
81-
*/
82-
private function getRenditionsRoot() :string
83-
{
84-
return $this->directory->getAbsolutePath() . self::RENDITIONS_DIRECTORY_NAME;
85-
}
86-
87-
/**
88-
* Renditions path getter
89-
*
90-
* @param string $filePath original file path
91-
* @return string|false
92-
*/
93-
private function getRenditionsPath($filePath) :?string
94-
{
95-
$mediaRootDir = $this->directory->getAbsolutePath('');
96-
if (strpos($filePath, (string) $mediaRootDir) === 0) {
97-
$relativeFilePath = substr($filePath, strlen($mediaRootDir));
98-
// phpcs:ignore Magento2.Functions.DiscouragedFunction
99-
$renditionPath = $this->getRenditionsRoot();
100-
if ($relativeFilePath === basename($filePath)) {
101-
$renditionPath .= DIRECTORY_SEPARATOR;
102-
}
103-
$renditionPath .= $relativeFilePath;
104-
return $renditionPath;
52+
if (!$this->isRenditionImageResizeable->execute($asset)) {
53+
return $asset->getPath();
10554
}
10655

107-
return null;
56+
return $this->directory->getRelativePath(
57+
self::RENDITIONS_DIRECTORY_NAME . '/' .
58+
$this->directory->getRelativePath(ltrim($asset->getPath(), '/'))
59+
);
10860
}
10961
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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\MediaGalleryRenditions\Model;
9+
10+
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
11+
use Magento\MediaGalleryRenditionsApi\Model\ConfigInterface;
12+
13+
class IsRenditionImageResizeable
14+
{
15+
/**
16+
* @var ConfigInterface
17+
*/
18+
private $config;
19+
20+
/**
21+
* IsRenditionImageResizeable constructor.
22+
* @param ConfigInterface $config
23+
*/
24+
public function __construct(
25+
ConfigInterface $config
26+
) {
27+
$this->config = $config;
28+
}
29+
30+
/**
31+
* Check if image needs to resize or not
32+
*
33+
* @param AssetInterface $asset
34+
* @return bool
35+
*/
36+
public function execute(AssetInterface $asset): bool
37+
{
38+
return $asset->getWidth() > $this->getResizedWidth()
39+
|| $asset->getHeight() > $this->getResizedHeight();
40+
}
41+
42+
/**
43+
* Get resized image width
44+
*
45+
* @return int
46+
*/
47+
public function getResizedWidth(): int
48+
{
49+
return $this->config->getWidth();
50+
}
51+
52+
/**
53+
* Get resized image height
54+
*
55+
* @return int
56+
*/
57+
public function getResizedHeight() :int
58+
{
59+
return $this->config->getHeight();
60+
}
61+
}

0 commit comments

Comments
 (0)