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

Public Member Functions

 __construct (\Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Downloadable\Model\Product\Type $downloadableType, \Magento\Downloadable\Api\Data\LinkInterfaceFactory $linkDataObjectFactory, LinkFactory $linkFactory, Link\ContentValidator $contentValidator, EncoderInterface $jsonEncoder, ContentUploaderInterface $fileContentUploader)
 
 save ($sku, LinkInterface $link, $isGlobalScopeContent=true)
 

Protected Member Functions

 setBasicFields ($resourceData, $dataObject)
 
 saveLink (\Magento\Catalog\Api\Data\ProductInterface $product, LinkInterface $link, $isGlobalScopeContent)
 

Protected Attributes

 $productRepository
 
 $linkDataObjectFactory
 
 $linkFactory
 
 $contentValidator
 
 $downloadableType
 
 $fileContentUploader
 
 $jsonEncoder
 

Detailed Description

Class LinkRepository @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 25 of file LinkRepository.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Catalog\Api\ProductRepositoryInterface  $productRepository,
\Magento\Downloadable\Model\Product\Type  $downloadableType,
\Magento\Downloadable\Api\Data\LinkInterfaceFactory  $linkDataObjectFactory,
LinkFactory  $linkFactory,
Link\ContentValidator  $contentValidator,
EncoderInterface  $jsonEncoder,
ContentUploaderInterface  $fileContentUploader 
)
Parameters
\Magento\Catalog\Api\ProductRepositoryInterface$productRepository
\Magento\Downloadable\Model\Product\Type$downloadableType
\Magento\Downloadable\Api\Data\LinkInterfaceFactory$linkDataObjectFactory
LinkFactory$linkFactory
Link\ContentValidator$contentValidator
EncoderInterface$jsonEncoder
ContentUploaderInterface$fileContentUploader

Definition at line 81 of file LinkRepository.php.

89  {
90  $this->productRepository = $productRepository;
91  $this->downloadableType = $downloadableType;
92  $this->linkDataObjectFactory = $linkDataObjectFactory;
93  $this->linkFactory = $linkFactory;
94  $this->contentValidator = $contentValidator;
95  $this->jsonEncoder = $jsonEncoder;
96  $this->fileContentUploader = $fileContentUploader;
97  }

Member Function Documentation

◆ save()

save (   $sku,
LinkInterface  $link,
  $isGlobalScopeContent = true 
)

{Update downloadable link of the given product (link type and its resources cannot be changed)

Parameters
string$sku
\Magento\Downloadable\Api\Data\LinkInterface$link
bool$isGlobalScopeContent
Returns
int
} @SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)

Implements LinkRepositoryInterface.

Definition at line 173 of file LinkRepository.php.

174  {
175  $product = $this->productRepository->get($sku, true);
176  if ($link->getId() !== null) {
177  return $this->updateLink($product, $link, $isGlobalScopeContent);
178  } else {
180  throw new InputException(
181  __('The product needs to be the downloadable type. Verify the product and try again.')
182  );
183  }
184  $validateLinkContent = !($link->getLinkType() === 'file' && $link->getLinkFile());
185  $validateSampleContent = !($link->getSampleType() === 'file' && $link->getSampleFile());
186  if (!$this->contentValidator->isValid($link, $validateLinkContent, $validateSampleContent)) {
187  throw new InputException(__('The link information is invalid. Verify the link and try again.'));
188  }
189 
190  if (!in_array($link->getLinkType(), ['url', 'file'], true)) {
191  throw new InputException(__('The link type is invalid. Verify and try again.'));
192  }
193  $title = $link->getTitle();
194  if (empty($title)) {
195  throw new InputException(__('The link title is empty. Enter the link title and try again.'));
196  }
197  return $this->saveLink($product, $link, $isGlobalScopeContent);
198  }
199  }
$title
Definition: default.phtml:14
__()
Definition: __.php:13

◆ saveLink()

saveLink ( \Magento\Catalog\Api\Data\ProductInterface  $product,
LinkInterface  $link,
  $isGlobalScopeContent 
)
protected
Parameters
\Magento\Catalog\Api\Data\ProductInterface$product
LinkInterface$link
bool$isGlobalScopeContent
Returns
int

Definition at line 207 of file LinkRepository.php.

211  {
212  $linkData = [
213  'link_id' => (int)$link->getId(),
214  'is_delete' => 0,
215  'type' => $link->getLinkType(),
216  'sort_order' => $link->getSortOrder(),
217  'title' => $link->getTitle(),
218  'price' => $link->getPrice(),
219  'number_of_downloads' => $link->getNumberOfDownloads(),
220  'is_shareable' => $link->getIsShareable(),
221  ];
222 
223  if ($link->getLinkType() == 'file' && $link->getLinkFile() === null) {
224  $linkData['file'] = $this->jsonEncoder->encode(
225  [
226  $this->fileContentUploader->upload($link->getLinkFileContent(), 'link_file'),
227  ]
228  );
229  } elseif ($link->getLinkType() === 'url') {
230  $linkData['link_url'] = $link->getLinkUrl();
231  } else {
232  //existing link file
233  $linkData['file'] = $this->jsonEncoder->encode(
234  [
235  [
236  'file' => $link->getLinkFile(),
237  'status' => 'old',
238  ]
239  ]
240  );
241  }
242 
243  if ($link->getSampleType() == 'file') {
244  $linkData['sample']['type'] = 'file';
245  if ($link->getSampleFile() === null) {
246  $fileData = [
247  $this->fileContentUploader->upload($link->getSampleFileContent(), 'link_sample_file'),
248  ];
249  } else {
250  $fileData = [
251  [
252  'file' => $link->getSampleFile(),
253  'status' => 'old',
254  ]
255  ];
256  }
257  $linkData['sample']['file'] = $this->jsonEncoder->encode($fileData);
258  } elseif ($link->getSampleType() == 'url') {
259  $linkData['sample']['type'] = 'url';
260  $linkData['sample']['url'] = $link->getSampleUrl();
261  }
262 
263  $downloadableData = ['link' => [$linkData]];
264  if ($isGlobalScopeContent) {
265  $product->setStoreId(0);
266  }
267  $this->getLinkTypeHandler()->save($product, $downloadableData);
268  return $product->getLastAddedLinkId();
269  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ setBasicFields()

setBasicFields (   $resourceData,
  $dataObject 
)
protected

Subroutine for build link

Parameters
\Magento\Downloadable\Model\Link$resourceData
\Magento\Downloadable\Api\Data\LinkInterface$dataObject
Returns
null

Definition at line 152 of file LinkRepository.php.

153  {
154  $dataObject->setId($resourceData->getId());
155  $storeTitle = $resourceData->getStoreTitle();
156  $title = $resourceData->getTitle();
157  if (!empty($storeTitle)) {
158  $dataObject->setTitle($storeTitle);
159  } else {
160  $dataObject->setTitle($title);
161  }
162  $dataObject->setSortOrder($resourceData->getSortOrder());
163  $dataObject->setSampleType($resourceData->getSampleType());
164  $dataObject->setSampleFile($resourceData->getSampleFile());
165  $dataObject->setSampleUrl($resourceData->getSampleUrl());
166  }
$title
Definition: default.phtml:14

Field Documentation

◆ $contentValidator

$contentValidator
protected

Definition at line 45 of file LinkRepository.php.

◆ $downloadableType

$downloadableType
protected

Definition at line 50 of file LinkRepository.php.

◆ $fileContentUploader

$fileContentUploader
protected

Definition at line 55 of file LinkRepository.php.

◆ $jsonEncoder

$jsonEncoder
protected

Definition at line 60 of file LinkRepository.php.

◆ $linkDataObjectFactory

$linkDataObjectFactory
protected

Definition at line 35 of file LinkRepository.php.

◆ $linkFactory

$linkFactory
protected

Definition at line 40 of file LinkRepository.php.

◆ $productRepository

$productRepository
protected

Definition at line 30 of file LinkRepository.php.


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