Skip to content

Commit 29d83b2

Browse files
authored
Merge pull request #5599 from magento-engcom/media-gallery-api
[Magento Community Engineering] MediaGallery API
2 parents c163118 + 371be02 commit 29d83b2

File tree

84 files changed

+3547
-739
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+3547
-739
lines changed

app/code/Magento/MediaGallery/Model/Asset.php

Lines changed: 111 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,123 +10,200 @@
1010

1111
use Magento\MediaGalleryApi\Api\Data\AssetExtensionInterface;
1212
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
13-
use Magento\Framework\Model\AbstractExtensibleModel;
1413

1514
/**
1615
* Media Gallery Asset
16+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
1717
*/
18-
class Asset extends AbstractExtensibleModel implements AssetInterface
18+
class Asset implements AssetInterface
1919
{
20-
private const ID = 'id';
21-
private const PATH = 'path';
22-
private const TITLE = 'title';
23-
private const SOURCE = 'source';
24-
private const CONTENT_TYPE = 'content_type';
25-
private const WIDTH = 'width';
26-
private const HEIGHT = 'height';
27-
private const SIZE = 'size';
28-
private const CREATED_AT = 'created_at';
29-
private const UPDATED_AT = 'updated_at';
20+
/**
21+
* @var int|null
22+
*/
23+
private $id;
24+
25+
/**
26+
* @var string
27+
*/
28+
private $path;
29+
30+
/**
31+
* @var string|null
32+
*/
33+
private $title;
34+
35+
/**
36+
* @var string|null
37+
*/
38+
private $source;
39+
40+
/**
41+
* @var string
42+
*/
43+
private $contentType;
44+
45+
/**
46+
* @var int
47+
*/
48+
private $width;
49+
50+
/**
51+
* @var int
52+
*/
53+
private $height;
54+
55+
/**
56+
* @var int
57+
*/
58+
private $size;
59+
60+
/**
61+
* @var string|null
62+
*/
63+
private $createdAt;
64+
65+
/**
66+
* @var string|null
67+
*/
68+
private $updatedAt;
69+
70+
/**
71+
* @var AssetExtensionInterface|null
72+
*/
73+
private $extensionAttributes;
74+
75+
/**
76+
* @param string $path
77+
* @param string $contentType
78+
* @param int $width
79+
* @param int $height
80+
* @param int $size
81+
* @param int|null $id
82+
* @param string|null $title
83+
* @param string|null $source
84+
* @param string|null $createdAt
85+
* @param string|null $updatedAt
86+
* @param AssetExtensionInterface|null $extensionAttributes
87+
*/
88+
public function __construct(
89+
string $path,
90+
string $contentType,
91+
int $width,
92+
int $height,
93+
int $size,
94+
?int $id = null,
95+
?string $title = null,
96+
?string $source = null,
97+
?string $createdAt = null,
98+
?string $updatedAt = null,
99+
?AssetExtensionInterface $extensionAttributes = null
100+
) {
101+
$this->path = $path;
102+
$this->contentType = $contentType;
103+
$this->width = $width;
104+
$this->height = $height;
105+
$this->size = $size;
106+
$this->id = $id;
107+
$this->title = $title;
108+
$this->source = $source;
109+
$this->createdAt = $createdAt;
110+
$this->updatedAt = $updatedAt;
111+
$this->extensionAttributes = $extensionAttributes;
112+
}
30113

31114
/**
32115
* @inheritdoc
33116
*/
34117
public function getId(): ?int
35118
{
36-
$id = $this->getData(self::ID);
37-
38-
if (!$id) {
39-
return null;
40-
}
41-
42-
return (int) $id;
119+
return $this->id;
43120
}
44121

45122
/**
46123
* @inheritdoc
47124
*/
48125
public function getPath(): string
49126
{
50-
return (string) $this->getData(self::PATH);
127+
return $this->path;
51128
}
52129

53130
/**
54131
* @inheritdoc
55132
*/
56133
public function getTitle(): ?string
57134
{
58-
return $this->getData(self::TITLE);
135+
return $this->title;
59136
}
60137

61138
/**
62139
* @inheritdoc
63140
*/
64141
public function getSource(): ?string
65142
{
66-
return $this->getData(self::SOURCE);
143+
return $this->source;
67144
}
68145

69146
/**
70147
* @inheritdoc
71148
*/
72149
public function getContentType(): string
73150
{
74-
return (string) $this->getData(self::CONTENT_TYPE);
151+
return $this->contentType;
75152
}
76153

77154
/**
78155
* @inheritdoc
79156
*/
80157
public function getWidth(): int
81158
{
82-
return (int) $this->getData(self::WIDTH);
159+
return $this->width;
83160
}
84161

85162
/**
86163
* @inheritdoc
87164
*/
88165
public function getHeight(): int
89166
{
90-
return (int) $this->getData(self::HEIGHT);
167+
return $this->height;
91168
}
92169

93170
/**
94171
* @inheritdoc
95172
*/
96173
public function getSize(): int
97174
{
98-
return (int) $this->getData(self::SIZE);
175+
return $this->size;
99176
}
100177

101178
/**
102179
* @inheritdoc
103180
*/
104-
public function getCreatedAt(): string
181+
public function getCreatedAt(): ?string
105182
{
106-
return (string) $this->getData(self::CREATED_AT);
183+
return $this->createdAt;
107184
}
108185

109186
/**
110187
* @inheritdoc
111188
*/
112-
public function getUpdatedAt(): string
189+
public function getUpdatedAt(): ?string
113190
{
114-
return (string) $this->getData(self::UPDATED_AT);
191+
return $this->updatedAt;
115192
}
116193

117194
/**
118195
* @inheritdoc
119196
*/
120-
public function getExtensionAttributes(): AssetExtensionInterface
197+
public function getExtensionAttributes(): ?AssetExtensionInterface
121198
{
122-
return $this->_getExtensionAttributes();
199+
return $this->extensionAttributes;
123200
}
124201

125202
/**
126203
* @inheritdoc
127204
*/
128-
public function setExtensionAttributes(AssetExtensionInterface $extensionAttributes): void
205+
public function setExtensionAttributes(?AssetExtensionInterface $extensionAttributes): void
129206
{
130-
$this->_setExtensionAttributes($extensionAttributes);
207+
$this->extensionAttributes = $extensionAttributes;
131208
}
132209
}

app/code/Magento/MediaGallery/Model/Asset/Command/DeleteByDirectoryPath.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
use Psr\Log\LoggerInterface;
1515

1616
/**
17-
* Class DeleteByDirectoryPath
18-
*
1917
* Remove asset(s) that correspond the provided directory path
18+
* @deprecated use \Magento\MediaGalleryApi\Api\DeleteAssetsByPathInterface instead
19+
* @see \Magento\MediaGalleryApi\Api\DeleteAssetsByPathInterfac
2020
*/
2121
class DeleteByDirectoryPath implements DeleteByDirectoryPathInterface
2222
{

app/code/Magento/MediaGallery/Model/Asset/Command/DeleteByPath.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
use Psr\Log\LoggerInterface;
1515

1616
/**
17-
* Class DeleteByPath
17+
* Delete media asset by path
18+
*
19+
* @deprecated use \Magento\MediaGalleryApi\Api\DeleteAssetsByPathInterface instead
20+
* @see \Magento\MediaGalleryApi\Api\DeleteAssetsByPathInterface
1821
*/
1922
class DeleteByPath implements DeleteByPathInterface
2023
{

app/code/Magento/MediaGallery/Model/Asset/Command/GetById.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
use Psr\Log\LoggerInterface;
1717

1818
/**
19-
* Class GetById
19+
* Get media asset by id
20+
* @deprecated use \Magento\MediaGalleryApi\Api\GetAssetsByIdsInterface instead
21+
* @see \Magento\MediaGalleryApi\Api\GetAssetsByIdsInterface
2022
*/
2123
class GetById implements GetByIdInterface
2224
{
@@ -28,7 +30,7 @@ class GetById implements GetByIdInterface
2830
private $resourceConnection;
2931

3032
/**
31-
* @var AssetInterface
33+
* @var AssetInterfaceFactory
3234
*/
3335
private $assetFactory;
3436

@@ -87,7 +89,20 @@ public function execute(int $mediaAssetId): AssetInterface
8789
}
8890

8991
try {
90-
return $this->assetFactory->create(['data' => $mediaAssetData]);
92+
return $this->assetFactory->create(
93+
[
94+
'id' => $mediaAssetData['id'],
95+
'path' => $mediaAssetData['path'],
96+
'title' => $mediaAssetData['title'],
97+
'source' => $mediaAssetData['source'],
98+
'contentType' => $mediaAssetData['content_type'],
99+
'width' => $mediaAssetData['width'],
100+
'height' => $mediaAssetData['height'],
101+
'size' => $mediaAssetData['size'],
102+
'createdAt' => $mediaAssetData['created_at'],
103+
'updatedAt' => $mediaAssetData['updated_at'],
104+
]
105+
);
91106
} catch (\Exception $exception) {
92107
$this->logger->critical($exception);
93108
$message = __(

app/code/Magento/MediaGallery/Model/Asset/Command/GetByPath.php

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
use Psr\Log\LoggerInterface;
1717

1818
/**
19-
* Class GetListByIds
19+
* Provide media asset by path
20+
*
21+
* @deprecated use \Magento\MediaGalleryApi\Api\GetAssetsByPathsInterface instead
22+
* @see \Magento\MediaGalleryApi\Api\GetAssetsByPathsInterface
2023
*/
2124
class GetByPath implements GetByPathInterface
2225
{
@@ -30,7 +33,7 @@ class GetByPath implements GetByPathInterface
3033
private $resourceConnection;
3134

3235
/**
33-
* @var AssetInterface
36+
* @var AssetInterfaceFactory
3437
*/
3538
private $mediaAssetFactory;
3639

@@ -57,30 +60,41 @@ public function __construct(
5760
}
5861

5962
/**
60-
* Return media asset asset list
63+
* Return media asset
6164
*
62-
* @param string $mediaFilePath
65+
* @param string $path
6366
*
6467
* @return AssetInterface
6568
* @throws IntegrationException
6669
*/
67-
public function execute(string $mediaFilePath): AssetInterface
70+
public function execute(string $path): AssetInterface
6871
{
6972
try {
7073
$connection = $this->resourceConnection->getConnection();
7174
$select = $connection->select()
7275
->from($this->resourceConnection->getTableName(self::TABLE_MEDIA_GALLERY_ASSET))
73-
->where(self::MEDIA_GALLERY_ASSET_PATH . ' = ?', $mediaFilePath);
76+
->where(self::MEDIA_GALLERY_ASSET_PATH . ' = ?', $path);
7477
$data = $connection->query($select)->fetch();
7578

7679
if (empty($data)) {
77-
$message = __('There is no such media asset with path "%1"', $mediaFilePath);
80+
$message = __('There is no such media asset with path "%1"', $path);
7881
throw new NoSuchEntityException($message);
7982
}
8083

81-
$mediaAssets = $this->mediaAssetFactory->create(['data' => $data]);
82-
83-
return $mediaAssets;
84+
return $this->mediaAssetFactory->create(
85+
[
86+
'id' => $data['id'],
87+
'path' => $data['path'],
88+
'title' => $data['title'],
89+
'source' => $data['source'],
90+
'contentType' => $data['content_type'],
91+
'width' => $data['width'],
92+
'height' => $data['height'],
93+
'size' => $data['size'],
94+
'createdAt' => $data['created_at'],
95+
'updatedAt' => $data['updated_at'],
96+
]
97+
);
8498
} catch (\Exception $exception) {
8599
$this->logger->critical($exception);
86100
$message = __('An error occurred during get media asset list: %1', $exception->getMessage());

0 commit comments

Comments
 (0)