/**
  * {@inheritdoc}
  */
 public function create($sku, ProductAttributeMediaGalleryEntryInterface $entry)
 {
     /** @var $entry ProductAttributeMediaGalleryEntryInterface */
     $entryContent = $entry->getContent();
     if (!$this->contentValidator->isValid($entryContent)) {
         throw new InputException(__('The image content is not valid.'));
     }
     $product = $this->productRepository->get($sku);
     $existingMediaGalleryEntries = $product->getMediaGalleryEntries();
     $existingEntryIds = [];
     if ($existingMediaGalleryEntries == null) {
         $existingMediaGalleryEntries = [$entry];
     } else {
         foreach ($existingMediaGalleryEntries as $existingEntries) {
             $existingEntryIds[$existingEntries->getId()] = $existingEntries->getId();
         }
         $existingMediaGalleryEntries[] = $entry;
     }
     $product->setMediaGalleryEntries($existingMediaGalleryEntries);
     try {
         $product = $this->productRepository->save($product);
     } catch (InputException $inputException) {
         throw $inputException;
     } catch (\Exception $e) {
         throw new StateException(__('Cannot save product.'));
     }
     foreach ($product->getMediaGalleryEntries() as $entry) {
         if (!isset($existingEntryIds[$entry->getId()])) {
             return $entry->getId();
         }
     }
     throw new StateException(__('Failed to save new media gallery entry.'));
 }
 /**
  * @expectedException \Magento\Framework\Exception\InputException
  * @expectedExceptionMessage Cannot recognize image extension.
  */
 public function testSaveWithoutFileExtension()
 {
     $imageContent = $this->getMockBuilder('Magento\\Framework\\Api\\Data\\ImageContentInterface')->disableOriginalConstructor()->getMock();
     $imageContent->expects($this->once())->method('getBase64EncodedData')->willReturn('testImageData');
     $imageContent->expects($this->once())->method('getName')->willReturn('testFileName');
     $imageDataObject = $this->getMockBuilder('Magento\\Framework\\Api\\AttributeValue')->disableOriginalConstructor()->getMock();
     $imageDataObject->expects($this->once())->method('getValue')->willReturn($imageContent);
     $imageData = $this->getMockForAbstractClass('Magento\\Framework\\Api\\CustomAttributesDataInterface');
     $imageData->expects($this->once())->method('getCustomAttributes')->willReturn([]);
     $this->dataObjectHelperMock->expects($this->once())->method('getCustomAttributeValueByType')->willReturn([$imageDataObject]);
     $this->contentValidatorMock->expects($this->once())->method('isValid')->willReturn(true);
     $this->assertEquals($imageData, $this->imageProcessor->save($imageData, 'testEntityType'));
 }