Skip to content

Commit 1db2872

Browse files
author
Viktor Kopin
committed
1 parent e832db8 commit 1db2872

File tree

6 files changed

+210
-88
lines changed

6 files changed

+210
-88
lines changed

app/code/Magento/MediaGalleryUi/Controller/Adminhtml/Image/OnInsert.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
declare(strict_types=1);
8+
79
namespace Magento\MediaGalleryUi\Controller\Adminhtml\Image;
810

911
use Magento\Backend\App\Action;
@@ -56,15 +58,17 @@ public function __construct(
5658
public function execute()
5759
{
5860
$data = $this->getRequest()->getParams();
59-
$content = $this->getInsertImageData->getImageContent(
61+
$insertImageData = $this->getInsertImageData->execute(
6062
$data['filename'],
61-
$data['force_static_path'],
62-
$data['as_is'],
63+
(bool)$data['force_static_path'],
64+
(bool)$data['as_is'],
6365
isset($data['store']) ? (int)$data['store'] : null
6466
);
6567

66-
$size = $data['force_static_path'] ? $this->getInsertImageData->getFileSize($content) : 0;
67-
$type = $data['force_static_path'] ? $this->getInsertImageData->getMimeType($content) : '';
68-
return $this->resultJsonFactory->create()->setData(['content' => $content, 'size' => $size, 'type' => $type]);
68+
return $this->resultJsonFactory->create()->setData([
69+
'content' => $insertImageData->getContent(),
70+
'size' => $insertImageData->getSize(),
71+
'type' => $insertImageData->getType(),
72+
]);
6973
}
7074
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\MediaGalleryUi\Model;
10+
11+
use Magento\MediaGalleryUiApi\Api\Data\InsertImageDataInterface;
12+
use Magento\MediaGalleryUiApi\Api\InsertImageDataExtensionInterface;
13+
14+
/**
15+
* Class responsible to provide insert image details
16+
*/
17+
class InsertImageData implements InsertImageDataInterface
18+
{
19+
/**
20+
* @var InsertImageDataExtensionInterface
21+
*/
22+
private $extensionAttributes;
23+
24+
/**
25+
* @var string
26+
*/
27+
private $content;
28+
29+
/**
30+
* @var int
31+
*/
32+
private $size;
33+
34+
/**
35+
* @var string
36+
*/
37+
private $type;
38+
39+
/**
40+
* InsertImageData constructor.
41+
*
42+
* @param string $content
43+
* @param int $size
44+
* @param string $type
45+
*/
46+
public function __construct(string $content, int $size, string $type)
47+
{
48+
$this->content = $content;
49+
$this->size = $size;
50+
$this->type = $type;
51+
}
52+
53+
/**
54+
* Returns a content (just a link or an html block) for inserting image to the content
55+
*
56+
* @return string
57+
*/
58+
public function getContent(): string
59+
{
60+
return $this->content;
61+
}
62+
63+
/**
64+
* Returns size of requested file
65+
*
66+
* @return int
67+
*/
68+
public function getSize(): int
69+
{
70+
return $this->size;
71+
}
72+
73+
/**
74+
* Returns MIME type of requested file
75+
*
76+
* @return string
77+
*/
78+
public function getType(): string
79+
{
80+
return $this->type;
81+
}
82+
83+
/**
84+
* Get extension attributes
85+
*
86+
* @return ?\Magento\MediaGalleryUiApi\Api\InsertImageDataExtensionInterface;|null
87+
*/
88+
public function getExtensionAttributes(): ?InsertImageDataExtensionInterface
89+
{
90+
return $this->extensionAttributes;
91+
}
92+
93+
/**
94+
* Set extension attributes
95+
*
96+
* @param ?\Magento\MediaGalleryUiApi\Api\InsertImageDataExtensionInterface;|null $extensionAttributes
97+
* @return void
98+
*/
99+
public function setExtensionAttributes(?InsertImageDataExtensionInterface $extensionAttributes): void
100+
{
101+
$this->extensionAttributes = $extensionAttributes;
102+
}
103+
}

app/code/Magento/MediaGalleryUi/Model/GetInsertImageData.php renamed to app/code/Magento/MediaGalleryUi/Model/InsertImageData/GetInsertImageData.php

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,79 +6,92 @@
66

77
declare(strict_types=1);
88

9-
namespace Magento\MediaGalleryUi\Model;
9+
namespace Magento\MediaGalleryUi\Model\InsertImageData;
1010

1111
use Magento\Cms\Model\Wysiwyg\Images\GetInsertImageContent;
1212
use Magento\Framework\App\Filesystem\DirectoryList;
1313
use Magento\Framework\File\Mime;
1414
use Magento\Framework\Filesystem;
1515
use Magento\Framework\Filesystem\Directory\ReadInterface;
16-
use Magento\MediaGalleryUiApi\Api\GetInsertImageDataExtensionInterface;
16+
use Magento\MediaGalleryUi\Model\InsertImageDataFactory;
17+
use Magento\MediaGalleryUiApi\Api\Data\InsertImageDataInterface;
1718
use Magento\MediaGalleryUiApi\Api\GetInsertImageDataInterface;
1819

19-
/**
20-
* Class responsible to provide insert image details
21-
*/
2220
class GetInsertImageData implements GetInsertImageDataInterface
2321
{
2422
/**
25-
* @var GetInsertImageDataExtensionInterface
23+
* @var ReadInterface
24+
*/
25+
private $pubDirectory;
26+
27+
/**
28+
* @var Filesystem
2629
*/
27-
private $extensionAttributes;
30+
private $filesystem;
2831

2932
/**
3033
* @var GetInsertImageContent
3134
*/
3235
private $getInsertImageContent;
3336

3437
/**
35-
* @var Filesystem
38+
* @var InsertImageDataFactory
3639
*/
37-
private $filesystem;
40+
private $insertImageDataFactory;
3841

3942
/**
4043
* @var Mime
4144
*/
4245
private $mime;
4346

44-
/**
45-
* @var ReadInterface
46-
*/
47-
private $pubDirectory;
48-
4947
/**
5048
* GetInsertImageData constructor.
5149
*
5250
* @param GetInsertImageContent $getInsertImageContent
5351
* @param Filesystem $fileSystem
5452
* @param Mime $mime
53+
* @param InsertImageDataFactory $insertImageDataFactory
5554
*/
5655
public function __construct(
5756
GetInsertImageContent $getInsertImageContent,
5857
Filesystem $fileSystem,
59-
Mime $mime
58+
Mime $mime,
59+
InsertImageDataFactory $insertImageDataFactory
6060
) {
6161
$this->getInsertImageContent = $getInsertImageContent;
6262
$this->filesystem = $fileSystem;
6363
$this->mime = $mime;
64+
$this->insertImageDataFactory = $insertImageDataFactory;
6465
}
6566

6667
/**
67-
* Retrieves a content (just a link or an html block) for inserting image to the content
68+
* Returns image data object
6869
*
6970
* @param string $encodedFilename
7071
* @param bool $forceStaticPath
7172
* @param bool $renderAsTag
7273
* @param int|null $storeId
73-
* @return null|string
74+
* @return InsertImageDataInterface
7475
*/
75-
public function getImageContent(
76+
public function execute(
7677
string $encodedFilename,
7778
bool $forceStaticPath,
7879
bool $renderAsTag,
7980
?int $storeId = null
80-
): string {
81-
return $this->getInsertImageContent->execute($encodedFilename, $forceStaticPath, $renderAsTag, $storeId);
81+
): InsertImageDataInterface {
82+
$content = $this->getInsertImageContent->execute(
83+
$encodedFilename,
84+
$forceStaticPath,
85+
$renderAsTag,
86+
$storeId
87+
);
88+
$size = $forceStaticPath ? $this->getSize($content) : 0;
89+
$type = $forceStaticPath ? $this->getType($content) : '';
90+
return $this->insertImageDataFactory->create([
91+
'content' => $content,
92+
'size' => $size,
93+
'type' => $type
94+
]);
8295
}
8396

8497
/**
@@ -87,7 +100,7 @@ public function getImageContent(
87100
* @param string $path
88101
* @return int
89102
*/
90-
public function getFileSize(string $path): int
103+
private function getSize(string $path): int
91104
{
92105
$directory = $this->getPubDirectory();
93106

@@ -100,7 +113,7 @@ public function getFileSize(string $path): int
100113
* @param string $path
101114
* @return string
102115
*/
103-
public function getMimeType(string $path): string
116+
public function getType(string $path): string
104117
{
105118
$absoluteFilePath = $this->getPubDirectory()->getAbsolutePath($path);
106119

@@ -119,25 +132,4 @@ private function getPubDirectory(): ReadInterface
119132
}
120133
return $this->pubDirectory;
121134
}
122-
123-
/**
124-
* Get extension attributes
125-
*
126-
* @return GetInsertImageDataExtensionInterface|null
127-
*/
128-
public function getExtensionAttributes(): ?GetInsertImageDataExtensionInterface
129-
{
130-
return $this->extensionAttributes;
131-
}
132-
133-
/**
134-
* Set extension attributes
135-
*
136-
* @param GetInsertImageDataExtensionInterface|null $extensionAttributes
137-
* @return void
138-
*/
139-
public function setExtensionAttributes(?GetInsertImageDataExtensionInterface $extensionAttributes): void
140-
{
141-
$this->extensionAttributes = $extensionAttributes;
142-
}
143135
}

app/code/Magento/MediaGalleryUi/etc/di.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
99
<preference for="Magento\MediaGalleryUiApi\Api\ConfigInterface" type="Magento\MediaGalleryUi\Model\Config"/>
10-
<preference for="Magento\MediaGalleryUiApi\Api\GetInsertImageDataInterface" type="Magento\MediaGalleryUi\Model\GetInsertImageData"/>
10+
<preference for="Magento\MediaGalleryUiApi\Api\GetInsertImageDataInterface" type="\Magento\MediaGalleryUi\Model\InsertImageData\GetInsertImageData"/>
11+
<preference for="Magento\MediaGalleryUiApi\Api\Data\InsertImageDataInterface" type="\Magento\MediaGalleryUi\Model\InsertImageData"/>
1112
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
1213
<arguments>
1314
<argument name="collections" xsi:type="array">
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\MediaGalleryUiApi\Api\Data;
10+
11+
use Magento\Framework\Api\ExtensibleDataInterface;
12+
13+
/**
14+
* Class responsible to provide insert image details
15+
*/
16+
interface InsertImageDataInterface extends ExtensibleDataInterface
17+
{
18+
/**
19+
* Returns a content (just a link or an html block) for inserting image to the content
20+
*
21+
* @return null|string
22+
*/
23+
public function getContent(): ?string;
24+
25+
/**
26+
* Returns size of requested file
27+
*
28+
* @return int
29+
*/
30+
public function getSize(): int;
31+
32+
/**
33+
* Returns MIME type of requested file
34+
*
35+
* @return string
36+
*/
37+
public function getType(): string;
38+
39+
/**
40+
* Get extension attributes
41+
*
42+
* @return \Magento\MediaGalleryUiApi\Api\InsertImageDataExtensionInterface|null
43+
*/
44+
public function getExtensionAttributes(): ?\Magento\MediaGalleryUiApi\Api\InsertImageDataExtensionInterface;
45+
46+
/**
47+
* Set extension attributes
48+
*
49+
* @param \Magento\MediaGalleryUiApi\Api\InsertImageDataExtensionInterface|null $extensionAttributes
50+
* @return void
51+
*/
52+
public function setExtensionAttributes(
53+
?\Magento\MediaGalleryUiApi\Api\InsertImageDataExtensionInterface $extensionAttributes
54+
): void;
55+
}

0 commit comments

Comments
 (0)