Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GalleryManagement.php
Go to the documentation of this file.
1 <?php
8 
14 
23 {
27  protected $productRepository;
28 
32  protected $contentValidator;
33 
40  public function __construct(
43  ) {
44  $this->productRepository = $productRepository;
45  $this->contentValidator = $contentValidator;
46  }
47 
51  public function create($sku, ProductAttributeMediaGalleryEntryInterface $entry)
52  {
54  $entryContent = $entry->getContent();
55 
56  if (!$this->contentValidator->isValid($entryContent)) {
57  throw new InputException(__('The image content is invalid. Verify the content and try again.'));
58  }
59  $product = $this->productRepository->get($sku, true);
60 
61  $existingMediaGalleryEntries = $product->getMediaGalleryEntries();
62  $existingEntryIds = [];
63  if ($existingMediaGalleryEntries == null) {
64  $existingMediaGalleryEntries = [$entry];
65  } else {
66  foreach ($existingMediaGalleryEntries as $existingEntries) {
67  $existingEntryIds[$existingEntries->getId()] = $existingEntries->getId();
68  }
69  $existingMediaGalleryEntries[] = $entry;
70  }
71  $product->setMediaGalleryEntries($existingMediaGalleryEntries);
72  try {
73  $product = $this->productRepository->save($product);
74  } catch (InputException $inputException) {
75  throw $inputException;
76  } catch (\Exception $e) {
77  throw new StateException(__("The product can't be saved."));
78  }
79 
80  foreach ($product->getMediaGalleryEntries() as $entry) {
81  if (!isset($existingEntryIds[$entry->getId()])) {
82  return $entry->getId();
83  }
84  }
85  throw new StateException(__('The new media gallery entry failed to save.'));
86  }
87 
91  public function update($sku, ProductAttributeMediaGalleryEntryInterface $entry)
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  }
128 
132  public function remove($sku, $entryId)
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  }
158 
162  public function get($sku, $entryId)
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  }
179 
183  public function getList($sku)
184  {
186  $product = $this->productRepository->get($sku);
187 
188  return $product->getMediaGalleryEntries();
189  }
190 }
getId()
__()
Definition: __.php:13
getTypes()
getFile()
getContent()
setId($id)