Link.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Downloadable\Controller\Download;
  8. use Magento\Downloadable\Helper\Download as DownloadHelper;
  9. use Magento\Downloadable\Model\Link\Purchased\Item as PurchasedLink;
  10. use Magento\Framework\App\ResponseInterface;
  11. class Link extends \Magento\Downloadable\Controller\Download
  12. {
  13. /**
  14. * Return customer session object
  15. *
  16. * @return \Magento\Customer\Model\Session
  17. */
  18. protected function _getCustomerSession()
  19. {
  20. return $this->_objectManager->get(\Magento\Customer\Model\Session::class);
  21. }
  22. /**
  23. * Download link action
  24. *
  25. * @return void|ResponseInterface
  26. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  27. * @SuppressWarnings(PHPMD.NPathComplexity)
  28. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  29. * @SuppressWarnings(PHPMD.ExitExpression)
  30. */
  31. public function execute()
  32. {
  33. $session = $this->_getCustomerSession();
  34. $id = $this->getRequest()->getParam('id', 0);
  35. /** @var PurchasedLink $linkPurchasedItem */
  36. $linkPurchasedItem = $this->_objectManager->create(
  37. \Magento\Downloadable\Model\Link\Purchased\Item::class
  38. )->load(
  39. $id,
  40. 'link_hash'
  41. );
  42. if (!$linkPurchasedItem->getId()) {
  43. $this->messageManager->addNotice(__("We can't find the link you requested."));
  44. return $this->_redirect('*/customer/products');
  45. }
  46. if (!$this->_objectManager->get(\Magento\Downloadable\Helper\Data::class)->getIsShareable($linkPurchasedItem)) {
  47. $customerId = $session->getCustomerId();
  48. if (!$customerId) {
  49. /** @var \Magento\Catalog\Model\Product $product */
  50. $product = $this->_objectManager->create(
  51. \Magento\Catalog\Model\Product::class
  52. )->load(
  53. $linkPurchasedItem->getProductId()
  54. );
  55. if ($product->getId()) {
  56. $notice = __(
  57. 'Please sign in to download your product or purchase <a href="%1">%2</a>.',
  58. $product->getProductUrl(),
  59. $product->getName()
  60. );
  61. } else {
  62. $notice = __('Please sign in to download your product.');
  63. }
  64. $this->messageManager->addNotice($notice);
  65. $session->authenticate();
  66. $session->setBeforeAuthUrl(
  67. $this->_objectManager->create(
  68. \Magento\Framework\UrlInterface::class
  69. )->getUrl(
  70. 'downloadable/customer/products/',
  71. ['_secure' => true]
  72. )
  73. );
  74. return;
  75. }
  76. /** @var \Magento\Downloadable\Model\Link\Purchased $linkPurchased */
  77. $linkPurchased = $this->_objectManager->create(
  78. \Magento\Downloadable\Model\Link\Purchased::class
  79. )->load(
  80. $linkPurchasedItem->getPurchasedId()
  81. );
  82. if ($linkPurchased->getCustomerId() != $customerId) {
  83. $this->messageManager->addNotice(__("We can't find the link you requested."));
  84. return $this->_redirect('*/customer/products');
  85. }
  86. }
  87. $downloadsLeft = $linkPurchasedItem->getNumberOfDownloadsBought() -
  88. $linkPurchasedItem->getNumberOfDownloadsUsed();
  89. $status = $linkPurchasedItem->getStatus();
  90. if ($status == PurchasedLink::LINK_STATUS_AVAILABLE && ($downloadsLeft ||
  91. $linkPurchasedItem->getNumberOfDownloadsBought() == 0)
  92. ) {
  93. $resource = '';
  94. $resourceType = '';
  95. if ($linkPurchasedItem->getLinkType() == DownloadHelper::LINK_TYPE_URL) {
  96. $resource = $linkPurchasedItem->getLinkUrl();
  97. $resourceType = DownloadHelper::LINK_TYPE_URL;
  98. } elseif ($linkPurchasedItem->getLinkType() == DownloadHelper::LINK_TYPE_FILE) {
  99. $resource = $this->_objectManager->get(
  100. \Magento\Downloadable\Helper\File::class
  101. )->getFilePath(
  102. $this->_getLink()->getBasePath(),
  103. $linkPurchasedItem->getLinkFile()
  104. );
  105. $resourceType = DownloadHelper::LINK_TYPE_FILE;
  106. }
  107. try {
  108. $this->_processDownload($resource, $resourceType);
  109. $linkPurchasedItem->setNumberOfDownloadsUsed($linkPurchasedItem->getNumberOfDownloadsUsed() + 1);
  110. if ($linkPurchasedItem->getNumberOfDownloadsBought() != 0 && !($downloadsLeft - 1)) {
  111. $linkPurchasedItem->setStatus(PurchasedLink::LINK_STATUS_EXPIRED);
  112. }
  113. $linkPurchasedItem->save();
  114. exit(0);
  115. } catch (\Exception $e) {
  116. $this->messageManager->addError(__('Something went wrong while getting the requested content.'));
  117. }
  118. } elseif ($status == PurchasedLink::LINK_STATUS_EXPIRED) {
  119. $this->messageManager->addNotice(__('The link has expired.'));
  120. } elseif ($status == PurchasedLink::LINK_STATUS_PENDING || $status == PurchasedLink::LINK_STATUS_PAYMENT_REVIEW
  121. ) {
  122. $this->messageManager->addNotice(__('The link is not available.'));
  123. } else {
  124. $this->messageManager->addError(__('Something went wrong while getting the requested content.'));
  125. }
  126. return $this->_redirect('*/customer/products');
  127. }
  128. }