123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Downloadable\Model;
- use Magento\Catalog\Api\Data\ProductInterface;
- use Magento\Downloadable\Api\Data\LinkInterface;
- use Magento\Downloadable\Model\Product\Type;
- use Magento\Downloadable\Api\Data\File\ContentUploaderInterface;
- use Magento\Downloadable\Model\Product\TypeHandler\Link as LinkHandler;
- use Magento\Framework\EntityManager\MetadataPool;
- use Magento\Framework\Exception\InputException;
- use Magento\Framework\Exception\NoSuchEntityException;
- use Magento\Framework\Exception\StateException;
- use Magento\Framework\Json\EncoderInterface;
- use Magento\Framework\App\ObjectManager;
- /**
- * Class LinkRepository
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class LinkRepository implements \Magento\Downloadable\Api\LinkRepositoryInterface
- {
- /**
- * @var \Magento\Catalog\Api\ProductRepositoryInterface
- */
- protected $productRepository;
- /**
- * @var \Magento\Downloadable\Api\Data\LinkInterfaceFactory
- */
- protected $linkDataObjectFactory;
- /**
- * @var \Magento\Downloadable\Model\LinkFactory
- */
- protected $linkFactory;
- /**
- * @var \Magento\Downloadable\Model\Link\ContentValidator
- */
- protected $contentValidator;
- /**
- * @var Type
- */
- protected $downloadableType;
- /**
- * @var ContentUploaderInterface
- */
- protected $fileContentUploader;
- /**
- * @var EncoderInterface
- */
- protected $jsonEncoder;
- /**
- * @var LinkHandler
- */
- private $linkTypeHandler;
- /**
- * @var MetadataPool
- */
- private $metadataPool;
- /**
- * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
- * @param \Magento\Downloadable\Model\Product\Type $downloadableType
- * @param \Magento\Downloadable\Api\Data\LinkInterfaceFactory $linkDataObjectFactory
- * @param LinkFactory $linkFactory
- * @param Link\ContentValidator $contentValidator
- * @param EncoderInterface $jsonEncoder
- * @param ContentUploaderInterface $fileContentUploader
- */
- public function __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
- ) {
- $this->productRepository = $productRepository;
- $this->downloadableType = $downloadableType;
- $this->linkDataObjectFactory = $linkDataObjectFactory;
- $this->linkFactory = $linkFactory;
- $this->contentValidator = $contentValidator;
- $this->jsonEncoder = $jsonEncoder;
- $this->fileContentUploader = $fileContentUploader;
- }
- /**
- * {@inheritdoc}
- */
- public function getList($sku)
- {
- /** @var \Magento\Catalog\Model\Product $product */
- $product = $this->productRepository->get($sku);
- return $this->getLinksByProduct($product);
- }
- /**
- * @param \Magento\Catalog\Api\Data\ProductInterface $product
- * @return array
- */
- public function getLinksByProduct(\Magento\Catalog\Api\Data\ProductInterface $product)
- {
- $linkList = [];
- $links = $this->downloadableType->getLinks($product);
- /** @var \Magento\Downloadable\Model\Link $link */
- foreach ($links as $link) {
- $linkList[] = $this->buildLink($link);
- }
- return $linkList;
- }
- /**
- * Build a link data object
- *
- * @param \Magento\Downloadable\Model\Link $resourceData
- * @return \Magento\Downloadable\Model\Link
- */
- protected function buildLink($resourceData)
- {
- /** @var \Magento\Downloadable\Model\Link $link */
- $link = $this->linkDataObjectFactory->create();
- $this->setBasicFields($resourceData, $link);
- $link->setPrice($resourceData->getPrice());
- $link->setNumberOfDownloads($resourceData->getNumberOfDownloads());
- $link->setIsShareable($resourceData->getIsShareable());
- $link->setLinkType($resourceData->getLinkType());
- $link->setLinkFile($resourceData->getLinkFile());
- $link->setLinkUrl($resourceData->getLinkUrl());
- return $link;
- }
- /**
- * Subroutine for build link
- *
- * @param \Magento\Downloadable\Model\Link $resourceData
- * @param \Magento\Downloadable\Api\Data\LinkInterface $dataObject
- * @return null
- */
- protected function setBasicFields($resourceData, $dataObject)
- {
- $dataObject->setId($resourceData->getId());
- $storeTitle = $resourceData->getStoreTitle();
- $title = $resourceData->getTitle();
- if (!empty($storeTitle)) {
- $dataObject->setTitle($storeTitle);
- } else {
- $dataObject->setTitle($title);
- }
- $dataObject->setSortOrder($resourceData->getSortOrder());
- $dataObject->setSampleType($resourceData->getSampleType());
- $dataObject->setSampleFile($resourceData->getSampleFile());
- $dataObject->setSampleUrl($resourceData->getSampleUrl());
- }
- /**
- * {@inheritdoc}
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- * @SuppressWarnings(PHPMD.NPathComplexity)
- */
- public function save($sku, LinkInterface $link, $isGlobalScopeContent = true)
- {
- $product = $this->productRepository->get($sku, true);
- if ($link->getId() !== null) {
- return $this->updateLink($product, $link, $isGlobalScopeContent);
- } else {
- if ($product->getTypeId() !== \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) {
- throw new InputException(
- __('The product needs to be the downloadable type. Verify the product and try again.')
- );
- }
- $validateLinkContent = !($link->getLinkType() === 'file' && $link->getLinkFile());
- $validateSampleContent = !($link->getSampleType() === 'file' && $link->getSampleFile());
- if (!$this->contentValidator->isValid($link, $validateLinkContent, $validateSampleContent)) {
- throw new InputException(__('The link information is invalid. Verify the link and try again.'));
- }
- if (!in_array($link->getLinkType(), ['url', 'file'], true)) {
- throw new InputException(__('The link type is invalid. Verify and try again.'));
- }
- $title = $link->getTitle();
- if (empty($title)) {
- throw new InputException(__('The link title is empty. Enter the link title and try again.'));
- }
- return $this->saveLink($product, $link, $isGlobalScopeContent);
- }
- }
- /**
- * @param \Magento\Catalog\Api\Data\ProductInterface $product
- * @param LinkInterface $link
- * @param bool $isGlobalScopeContent
- * @return int
- */
- protected function saveLink(
- \Magento\Catalog\Api\Data\ProductInterface $product,
- LinkInterface $link,
- $isGlobalScopeContent
- ) {
- $linkData = [
- 'link_id' => (int)$link->getId(),
- 'is_delete' => 0,
- 'type' => $link->getLinkType(),
- 'sort_order' => $link->getSortOrder(),
- 'title' => $link->getTitle(),
- 'price' => $link->getPrice(),
- 'number_of_downloads' => $link->getNumberOfDownloads(),
- 'is_shareable' => $link->getIsShareable(),
- ];
- if ($link->getLinkType() == 'file' && $link->getLinkFile() === null) {
- $linkData['file'] = $this->jsonEncoder->encode(
- [
- $this->fileContentUploader->upload($link->getLinkFileContent(), 'link_file'),
- ]
- );
- } elseif ($link->getLinkType() === 'url') {
- $linkData['link_url'] = $link->getLinkUrl();
- } else {
- //existing link file
- $linkData['file'] = $this->jsonEncoder->encode(
- [
- [
- 'file' => $link->getLinkFile(),
- 'status' => 'old',
- ]
- ]
- );
- }
- if ($link->getSampleType() == 'file') {
- $linkData['sample']['type'] = 'file';
- if ($link->getSampleFile() === null) {
- $fileData = [
- $this->fileContentUploader->upload($link->getSampleFileContent(), 'link_sample_file'),
- ];
- } else {
- $fileData = [
- [
- 'file' => $link->getSampleFile(),
- 'status' => 'old',
- ]
- ];
- }
- $linkData['sample']['file'] = $this->jsonEncoder->encode($fileData);
- } elseif ($link->getSampleType() == 'url') {
- $linkData['sample']['type'] = 'url';
- $linkData['sample']['url'] = $link->getSampleUrl();
- }
- $downloadableData = ['link' => [$linkData]];
- if ($isGlobalScopeContent) {
- $product->setStoreId(0);
- }
- $this->getLinkTypeHandler()->save($product, $downloadableData);
- return $product->getLastAddedLinkId();
- }
- /**
- * @param \Magento\Catalog\Api\Data\ProductInterface $product
- * @param LinkInterface $link
- * @param bool $isGlobalScopeContent
- * @return mixed
- * @throws InputException
- * @throws NoSuchEntityException
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- * @SuppressWarnings(PHPMD.NPathComplexity)
- */
- protected function updateLink(
- \Magento\Catalog\Api\Data\ProductInterface $product,
- LinkInterface $link,
- $isGlobalScopeContent
- ) {
- /** @var $existingLink \Magento\Downloadable\Model\Link */
- $existingLink = $this->linkFactory->create()->load($link->getId());
- if (!$existingLink->getId()) {
- throw new NoSuchEntityException(
- __('No downloadable link with the provided ID was found. Verify the ID and try again.')
- );
- }
- $linkFieldValue = $product->getData(
- $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField()
- );
- if ($existingLink->getProductId() != $linkFieldValue) {
- throw new InputException(
- __("The downloadable link isn't related to the product. Verify the link and try again.")
- );
- }
- $validateLinkContent = !($link->getLinkFileContent() === null);
- $validateSampleContent = !($link->getSampleFileContent() === null);
- if (!$this->contentValidator->isValid($link, $validateLinkContent, $validateSampleContent)) {
- throw new InputException(__('The link information is invalid. Verify the link and try again.'));
- }
- if ($isGlobalScopeContent) {
- $product->setStoreId(0);
- }
- $title = $link->getTitle();
- if (empty($title)) {
- if ($isGlobalScopeContent) {
- throw new InputException(__('The link title is empty. Enter the link title and try again.'));
- }
- }
- if ($link->getLinkType() == 'file' && $link->getLinkFileContent() === null && !$link->getLinkFile()) {
- $link->setLinkFile($existingLink->getLinkFile());
- }
- if ($link->getSampleType() == 'file' && $link->getSampleFileContent() === null && !$link->getSampleFile()) {
- $link->setSampleFile($existingLink->getSampleFile());
- }
- $this->saveLink($product, $link, $isGlobalScopeContent);
- return $existingLink->getId();
- }
- /**
- * {@inheritdoc}
- */
- public function delete($id)
- {
- /** @var $link \Magento\Downloadable\Model\Link */
- $link = $this->linkFactory->create()->load($id);
- if (!$link->getId()) {
- throw new NoSuchEntityException(
- __('No downloadable link with the provided ID was found. Verify the ID and try again.')
- );
- }
- try {
- $link->delete();
- } catch (\Exception $exception) {
- throw new StateException(__('The link with "%1" ID can\'t be deleted.', $link->getId()), $exception);
- }
- return true;
- }
- /**
- * Get MetadataPool instance
- *
- * @deprecated 100.1.0 MAGETWO-52273
- * @return MetadataPool
- */
- private function getMetadataPool()
- {
- if (!$this->metadataPool) {
- $this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
- }
- return $this->metadataPool;
- }
- /**
- * Get LinkTypeHandler instance
- *
- * @deprecated 100.1.0 MAGETWO-52273
- * @return LinkHandler
- */
- private function getLinkTypeHandler()
- {
- if (!$this->linkTypeHandler) {
- $this->linkTypeHandler = ObjectManager::getInstance()->get(LinkHandler::class);
- }
- return $this->linkTypeHandler;
- }
- }
|