Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
GalleryManagement Class Reference
Inheritance diagram for GalleryManagement:
ProductAttributeMediaGalleryManagementInterface

Public Member Functions

 __construct (\Magento\Catalog\Api\ProductRepositoryInterface $productRepository, ImageContentValidatorInterface $contentValidator)
 
 update ($sku, ProductAttributeMediaGalleryEntryInterface $entry)
 
 remove ($sku, $entryId)
 
 get ($sku, $entryId)
 

Protected Attributes

 $productRepository
 
 $contentValidator
 

Detailed Description

Class GalleryManagement

Provides implementation of api interface ProductAttributeMediaGalleryManagementInterface

@SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 22 of file GalleryManagement.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Catalog\Api\ProductRepositoryInterface  $productRepository,
ImageContentValidatorInterface  $contentValidator 
)
Parameters
\Magento\Catalog\Api\ProductRepositoryInterface$productRepository
ImageContentValidatorInterface$contentValidator@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 40 of file GalleryManagement.php.

Member Function Documentation

◆ get()

get (   $sku,
  $entryId 
)

Return information about gallery entry

Parameters
string$sku
int$entryId
Exceptions

Implements ProductAttributeMediaGalleryManagementInterface.

Definition at line 162 of file GalleryManagement.php.

163  {
164  try {
165  $product = $this->productRepository->get($sku);
166  } catch (\Exception $exception) {
167  throw new NoSuchEntityException(__("The product doesn't exist. Verify and try again."));
168  }
169 
170  $mediaGalleryEntries = $product->getMediaGalleryEntries();
171  foreach ($mediaGalleryEntries as $entry) {
172  if ($entry->getId() == $entryId) {
173  return $entry;
174  }
175  }
176 
177  throw new NoSuchEntityException(__("The image doesn't exist. Verify and try again."));
178  }
__()
Definition: __.php:13

◆ remove()

remove (   $sku,
  $entryId 
)

Remove gallery entry

Parameters
string$sku
int$entryId
Returns
bool
Exceptions

Implements ProductAttributeMediaGalleryManagementInterface.

Definition at line 132 of file GalleryManagement.php.

133  {
134  $product = $this->productRepository->get($sku, true);
135  $existingMediaGalleryEntries = $product->getMediaGalleryEntries();
136  if ($existingMediaGalleryEntries == null) {
137  throw new NoSuchEntityException(
138  __('No image with the provided ID was found. Verify the ID and try again.')
139  );
140  }
141  $found = false;
142  foreach ($existingMediaGalleryEntries as $key => $entry) {
143  if ($entry->getId() == $entryId) {
144  unset($existingMediaGalleryEntries[$key]);
145  $found = true;
146  break;
147  }
148  }
149  if (!$found) {
150  throw new NoSuchEntityException(
151  __('No image with the provided ID was found. Verify the ID and try again.')
152  );
153  }
154  $product->setMediaGalleryEntries($existingMediaGalleryEntries);
155  $this->productRepository->save($product);
156  return true;
157  }
__()
Definition: __.php:13

◆ update()

update (   $sku,
ProductAttributeMediaGalleryEntryInterface  $entry 
)

Definition at line 91 of file GalleryManagement.php.

92  {
93  $product = $this->productRepository->get($sku, true);
94  $existingMediaGalleryEntries = $product->getMediaGalleryEntries();
95  if ($existingMediaGalleryEntries == null) {
96  throw new NoSuchEntityException(
97  __('No image with the provided ID was found. Verify the ID and try again.')
98  );
99  }
100  $found = false;
101  foreach ($existingMediaGalleryEntries as $key => $existingEntry) {
102  $entryTypes = (array)$entry->getTypes();
103  $existingEntryTypes = (array)$existingMediaGalleryEntries[$key]->getTypes();
104  $existingMediaGalleryEntries[$key]->setTypes(array_diff($existingEntryTypes, $entryTypes));
105 
106  if ($existingEntry->getId() == $entry->getId()) {
107  $found = true;
108  if ($entry->getFile()) {
109  $entry->setId(null);
110  }
111  $existingMediaGalleryEntries[$key] = $entry;
112  }
113  }
114  if (!$found) {
115  throw new NoSuchEntityException(
116  __('No image with the provided ID was found. Verify the ID and try again.')
117  );
118  }
119  $product->setMediaGalleryEntries($existingMediaGalleryEntries);
120 
121  try {
122  $this->productRepository->save($product);
123  } catch (\Exception $exception) {
124  throw new StateException(__("The product can't be saved."));
125  }
126  return true;
127  }
__()
Definition: __.php:13

Field Documentation

◆ $contentValidator

$contentValidator
protected

Definition at line 32 of file GalleryManagement.php.

◆ $productRepository

$productRepository
protected

Definition at line 27 of file GalleryManagement.php.


The documentation for this class was generated from the following file: