Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Link.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Downloadable\Helper\Download as DownloadHelper;
12 
14 {
20  protected function _getCustomerSession()
21  {
22  return $this->_objectManager->get(\Magento\Customer\Model\Session::class);
23  }
24 
34  public function execute()
35  {
36  $session = $this->_getCustomerSession();
37 
38  $id = $this->getRequest()->getParam('id', 0);
40  $linkPurchasedItem = $this->_objectManager->create(
41  \Magento\Downloadable\Model\Link\Purchased\Item::class
42  )->load(
43  $id,
44  'link_hash'
45  );
46  if (!$linkPurchasedItem->getId()) {
47  $this->messageManager->addNotice(__("We can't find the link you requested."));
48  return $this->_redirect('*/customer/products');
49  }
50  if (!$this->_objectManager->get(\Magento\Downloadable\Helper\Data::class)->getIsShareable($linkPurchasedItem)) {
51  $customerId = $session->getCustomerId();
52  if (!$customerId) {
54  $product = $this->_objectManager->create(
55  \Magento\Catalog\Model\Product::class
56  )->load(
57  $linkPurchasedItem->getProductId()
58  );
59  if ($product->getId()) {
60  $notice = __(
61  'Please sign in to download your product or purchase <a href="%1">%2</a>.',
62  $product->getProductUrl(),
63  $product->getName()
64  );
65  } else {
66  $notice = __('Please sign in to download your product.');
67  }
68  $this->messageManager->addNotice($notice);
69  $session->authenticate();
70  $session->setBeforeAuthUrl(
71  $this->_objectManager->create(
72  \Magento\Framework\UrlInterface::class
73  )->getUrl(
74  'downloadable/customer/products/',
75  ['_secure' => true]
76  )
77  );
78  return;
79  }
81  $linkPurchased = $this->_objectManager->create(
82  \Magento\Downloadable\Model\Link\Purchased::class
83  )->load(
84  $linkPurchasedItem->getPurchasedId()
85  );
86  if ($linkPurchased->getCustomerId() != $customerId) {
87  $this->messageManager->addNotice(__("We can't find the link you requested."));
88  return $this->_redirect('*/customer/products');
89  }
90  }
91  $downloadsLeft = $linkPurchasedItem->getNumberOfDownloadsBought() -
92  $linkPurchasedItem->getNumberOfDownloadsUsed();
93 
94  $status = $linkPurchasedItem->getStatus();
95  if ($status == PurchasedLink::LINK_STATUS_AVAILABLE && ($downloadsLeft ||
96  $linkPurchasedItem->getNumberOfDownloadsBought() == 0)
97  ) {
98  $resource = '';
99  $resourceType = '';
100  if ($linkPurchasedItem->getLinkType() == DownloadHelper::LINK_TYPE_URL) {
101  $resource = $linkPurchasedItem->getLinkUrl();
102  $resourceType = DownloadHelper::LINK_TYPE_URL;
103  } elseif ($linkPurchasedItem->getLinkType() == DownloadHelper::LINK_TYPE_FILE) {
104  $resource = $this->_objectManager->get(
105  \Magento\Downloadable\Helper\File::class
106  )->getFilePath(
107  $this->_getLink()->getBasePath(),
108  $linkPurchasedItem->getLinkFile()
109  );
110  $resourceType = DownloadHelper::LINK_TYPE_FILE;
111  }
112  try {
113  $this->_processDownload($resource, $resourceType);
114  $linkPurchasedItem->setNumberOfDownloadsUsed($linkPurchasedItem->getNumberOfDownloadsUsed() + 1);
115 
116  if ($linkPurchasedItem->getNumberOfDownloadsBought() != 0 && !($downloadsLeft - 1)) {
117  $linkPurchasedItem->setStatus(PurchasedLink::LINK_STATUS_EXPIRED);
118  }
119  $linkPurchasedItem->save();
120  exit(0);
121  } catch (\Exception $e) {
122  $this->messageManager->addError(__('Something went wrong while getting the requested content.'));
123  }
124  } elseif ($status == PurchasedLink::LINK_STATUS_EXPIRED) {
125  $this->messageManager->addNotice(__('The link has expired.'));
126  } elseif ($status == PurchasedLink::LINK_STATUS_PENDING || $status == PurchasedLink::LINK_STATUS_PAYMENT_REVIEW
127  ) {
128  $this->messageManager->addNotice(__('The link is not available.'));
129  } else {
130  $this->messageManager->addError(__('Something went wrong while getting the requested content.'));
131  }
132  return $this->_redirect('*/customer/products');
133  }
134 }
_redirect($path, $arguments=[])
Definition: Action.php:167
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
exit
Definition: redirect.phtml:12
$status
Definition: order_status.php:8