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
7 
14 
19 {
24 
30  private $json;
31 
39  public function __construct(
40  Action\Context $context,
41  \Magento\Framework\App\Response\Http\FileFactory $fileResponseFactory,
42  Json $json = null
43  ) {
44  $this->_fileResponseFactory = $fileResponseFactory;
45  $this->json = $json ?: ObjectManager::getInstance()->get(Json::class);
46  parent::__construct($context);
47  }
48 
56  public function execute()
57  {
58  $option = $this->_objectManager->create(
59  \Magento\Wishlist\Model\Item\Option::class
60  )->load(
61  $this->getRequest()->getParam('id')
62  );
64  $resultForward = $this->resultFactory->create(ResultFactory::TYPE_FORWARD);
65  if (!$option->getId()) {
66  $resultForward->forward('noroute');
67  return $resultForward;
68  }
69 
70  $optionId = null;
71  if (strpos($option->getCode(), \Magento\Catalog\Model\Product\Type\AbstractType::OPTION_PREFIX) === 0) {
72  $optionId = str_replace(
73  \Magento\Catalog\Model\Product\Type\AbstractType::OPTION_PREFIX,
74  '',
75  $option->getCode()
76  );
77  if ((int)$optionId != $optionId) {
78  $resultForward->forward('noroute');
79  return $resultForward;
80  }
81  }
82  $productOption = $this->_objectManager->create(\Magento\Catalog\Model\Product\Option::class)->load($optionId);
83 
84  if (!$productOption ||
85  !$productOption->getId() ||
86  $productOption->getProductId() != $option->getProductId() ||
87  $productOption->getType() != 'file'
88  ) {
89  $resultForward->forward('noroute');
90  return $resultForward;
91  }
92 
93  try {
94  $info = $this->json->unserialize($option->getValue());
95  $secretKey = $this->getRequest()->getParam('key');
96 
97  if ($secretKey == $info['secret_key']) {
98  $this->_fileResponseFactory->create(
99  $info['title'],
100  ['value' => $info['quote_path'], 'type' => 'filename'],
102  $info['type']
103  );
104  }
105  } catch (\Exception $e) {
106  $resultForward->forward('noroute');
107  return $resultForward;
108  }
109  }
110 }
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
__construct(Action\Context $context, \Magento\Framework\App\Response\Http\FileFactory $fileResponseFactory, Json $json=null)