Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DownloadCustomOption.php
Go to the documentation of this file.
1 <?php
9 
13 use Magento\Framework\Controller\Result\ForwardFactory;
14 
20 {
25 
29  protected $download;
30 
35  protected $unserialize;
36 
40  private $serializer;
41 
49  public function __construct(
50  Context $context,
51  ForwardFactory $resultForwardFactory,
52  \Magento\Sales\Model\Download $download,
53  \Magento\Framework\Unserialize\Unserialize $unserialize,
54  \Magento\Framework\Serialize\Serializer\Json $serializer = null
55  ) {
56  parent::__construct($context);
57  $this->resultForwardFactory = $resultForwardFactory;
58  $this->download = $download;
59  $this->unserialize = $unserialize;
60  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(
61  \Magento\Framework\Serialize\Serializer\Json::class
62  );
63  }
64 
72  public function execute()
73  {
74  $quoteItemOptionId = $this->getRequest()->getParam('id');
76  $option = $this->_objectManager->create(
77  \Magento\Quote\Model\Quote\Item\Option::class
78  )->load($quoteItemOptionId);
80  $resultForward = $this->resultForwardFactory->create();
81 
82  if (!$option->getId()) {
83  return $resultForward->forward('noroute');
84  }
85 
86  $optionId = null;
87  if (strpos($option->getCode(), AbstractType::OPTION_PREFIX) === 0) {
88  $optionId = str_replace(AbstractType::OPTION_PREFIX, '', $option->getCode());
89  if ((int)$optionId != $optionId) {
90  $optionId = null;
91  }
92  }
93  $productOption = null;
94  if ($optionId) {
96  $productOption = $this->_objectManager->create(
97  \Magento\Catalog\Model\Product\Option::class
98  )->load($optionId);
99  }
100 
101  if (!$productOption || !$productOption->getId() || $productOption->getType() != 'file') {
102  return $resultForward->forward('noroute');
103  }
104 
105  try {
106  $info = $this->serializer->unserialize($option->getValue());
107  if ($this->getRequest()->getParam('key') != $info['secret_key']) {
108  return $resultForward->forward('noroute');
109  }
110  $this->download->downloadFile($info);
111  } catch (\Exception $e) {
112  return $resultForward->forward('noroute');
113  }
114  $this->endExecute();
115  }
116 
123  protected function endExecute()
124  {
125  exit(0);
126  }
127 }
__construct(Context $context, ForwardFactory $resultForwardFactory, \Magento\Sales\Model\Download $download, \Magento\Framework\Unserialize\Unserialize $unserialize, \Magento\Framework\Serialize\Serializer\Json $serializer=null)
exit
Definition: redirect.phtml:12
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52