DownloadCustomOption.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Controller\Index;
  7. use Magento\Framework\App\Action;
  8. use Magento\Framework\App\Action\HttpGetActionInterface;
  9. use Magento\Framework\App\Filesystem\DirectoryList;
  10. use Magento\Framework\App\ObjectManager;
  11. use Magento\Framework\Controller\ResultFactory;
  12. use Magento\Framework\Serialize\Serializer\Json;
  13. /**
  14. * Class DownloadCustomOption. Represents request-flow logic for option's file download
  15. */
  16. class DownloadCustomOption extends \Magento\Wishlist\Controller\AbstractIndex implements HttpGetActionInterface
  17. {
  18. /**
  19. * @var \Magento\Framework\App\Response\Http\FileFactory
  20. */
  21. protected $_fileResponseFactory;
  22. /**
  23. * Json Serializer Instance
  24. *
  25. * @var Json
  26. */
  27. private $json;
  28. /**
  29. * Constructor method
  30. *
  31. * @param Action\Context $context
  32. * @param \Magento\Framework\App\Response\Http\FileFactory $fileResponseFactory
  33. * @param Json|null $json
  34. */
  35. public function __construct(
  36. Action\Context $context,
  37. \Magento\Framework\App\Response\Http\FileFactory $fileResponseFactory,
  38. Json $json = null
  39. ) {
  40. $this->_fileResponseFactory = $fileResponseFactory;
  41. $this->json = $json ?: ObjectManager::getInstance()->get(Json::class);
  42. parent::__construct($context);
  43. }
  44. /**
  45. * Custom options download action
  46. *
  47. * @return \Magento\Framework\Controller\Result\Forward
  48. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  49. * @SuppressWarnings(PHPMD.ExitExpression)
  50. */
  51. public function execute()
  52. {
  53. $option = $this->_objectManager->create(
  54. \Magento\Wishlist\Model\Item\Option::class
  55. )->load(
  56. $this->getRequest()->getParam('id')
  57. );
  58. /** @var \Magento\Framework\Controller\Result\Forward $resultForward */
  59. $resultForward = $this->resultFactory->create(ResultFactory::TYPE_FORWARD);
  60. if (!$option->getId()) {
  61. $resultForward->forward('noroute');
  62. return $resultForward;
  63. }
  64. $optionId = null;
  65. if (strpos($option->getCode(), \Magento\Catalog\Model\Product\Type\AbstractType::OPTION_PREFIX) === 0) {
  66. $optionId = str_replace(
  67. \Magento\Catalog\Model\Product\Type\AbstractType::OPTION_PREFIX,
  68. '',
  69. $option->getCode()
  70. );
  71. if ((int)$optionId != $optionId) {
  72. $resultForward->forward('noroute');
  73. return $resultForward;
  74. }
  75. }
  76. $productOption = $this->_objectManager->create(\Magento\Catalog\Model\Product\Option::class)->load($optionId);
  77. if (!$productOption ||
  78. !$productOption->getId() ||
  79. $productOption->getProductId() != $option->getProductId() ||
  80. $productOption->getType() != 'file'
  81. ) {
  82. $resultForward->forward('noroute');
  83. return $resultForward;
  84. }
  85. try {
  86. $info = $this->json->unserialize($option->getValue());
  87. $secretKey = $this->getRequest()->getParam('key');
  88. if ($secretKey == $info['secret_key']) {
  89. $this->_fileResponseFactory->create(
  90. $info['title'],
  91. ['value' => $info['quote_path'], 'type' => 'filename'],
  92. DirectoryList::ROOT,
  93. $info['type']
  94. );
  95. }
  96. } catch (\Exception $e) {
  97. $resultForward->forward('noroute');
  98. return $resultForward;
  99. }
  100. }
  101. }