Builder.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Model\Link;
  7. use Magento\Downloadable\Helper\File;
  8. use Magento\Downloadable\Model\Link;
  9. use Magento\Downloadable\Model\LinkFactory;
  10. use Magento\Framework\Api\DataObjectHelper;
  11. use Magento\Framework\DataObject\Copy;
  12. /**
  13. * Class Builder
  14. * @api
  15. * @since 100.1.0
  16. */
  17. class Builder
  18. {
  19. /**
  20. * @var Link
  21. */
  22. private $component;
  23. /**
  24. * @var File
  25. */
  26. private $downloadableFile;
  27. /**
  28. * @var Copy
  29. */
  30. private $objectCopyService;
  31. /**
  32. * @var DataObjectHelper
  33. */
  34. private $dataObjectHelper;
  35. /**
  36. * @var LinkFactory
  37. */
  38. private $componentFactory;
  39. /**
  40. * @var array
  41. */
  42. private $data = [];
  43. /**
  44. * Mapper constructor.
  45. *
  46. * @param File $downloadableFile
  47. * @param Copy $objectCopyService
  48. * @param DataObjectHelper $dataObjectHelper
  49. * @param LinkFactory $componentFactory
  50. */
  51. public function __construct(
  52. File $downloadableFile,
  53. Copy $objectCopyService,
  54. DataObjectHelper $dataObjectHelper,
  55. LinkFactory $componentFactory
  56. ) {
  57. $this->downloadableFile = $downloadableFile;
  58. $this->objectCopyService = $objectCopyService;
  59. $this->dataObjectHelper = $dataObjectHelper;
  60. $this->componentFactory = $componentFactory;
  61. }
  62. /**
  63. * @param array $data
  64. * @return $this
  65. * @since 100.1.0
  66. */
  67. public function setData(array $data)
  68. {
  69. $this->data = $data;
  70. return $this;
  71. }
  72. /**
  73. * @param \Magento\Downloadable\Api\Data\LinkInterface $link
  74. * @return \Magento\Downloadable\Api\Data\LinkInterface
  75. * @throws \Magento\Framework\Exception\LocalizedException
  76. * @since 100.1.0
  77. */
  78. public function build(\Magento\Downloadable\Api\Data\LinkInterface $link)
  79. {
  80. $downloadableData = $this->objectCopyService->getDataFromFieldset(
  81. 'downloadable_data',
  82. 'to_link',
  83. $this->data
  84. );
  85. $this->dataObjectHelper->populateWithArray(
  86. $link,
  87. array_merge(
  88. $this->data,
  89. $downloadableData
  90. ),
  91. \Magento\Downloadable\Api\Data\LinkInterface::class
  92. );
  93. if ($link->getLinkType() === \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE) {
  94. if (!isset($this->data['file'])) {
  95. throw new \Magento\Framework\Exception\LocalizedException(__('Link file not provided'));
  96. }
  97. $linkFileName = $this->downloadableFile->moveFileFromTmp(
  98. $this->getComponent()->getBaseTmpPath(),
  99. $this->getComponent()->getBasePath(),
  100. $this->data['file']
  101. );
  102. $link->setLinkFile($linkFileName);
  103. $link->setLinkUrl(null);
  104. }
  105. if (isset($this->data['sample'])) {
  106. $link = $this->buildSample($link, $this->data['sample']);
  107. }
  108. if (!$link->getSortOrder()) {
  109. $link->setSortOrder(1);
  110. }
  111. if (!is_numeric($link->getPrice())) {
  112. $link->setPrice(0);
  113. }
  114. if (isset($this->data['is_unlimited']) && $this->data['is_unlimited']) {
  115. $link->setNumberOfDownloads(0);
  116. }
  117. $this->resetData();
  118. return $link;
  119. }
  120. /**
  121. * @return void
  122. */
  123. private function resetData()
  124. {
  125. $this->data = [];
  126. }
  127. /**
  128. * @return Link
  129. */
  130. private function getComponent()
  131. {
  132. if (!$this->component) {
  133. $this->component = $this->componentFactory->create();
  134. }
  135. return $this->component;
  136. }
  137. /**
  138. * @param \Magento\Downloadable\Api\Data\LinkInterface $link
  139. * @param array $sample
  140. * @return \Magento\Downloadable\Api\Data\LinkInterface
  141. * @throws \Magento\Framework\Exception\LocalizedException
  142. */
  143. private function buildSample(\Magento\Downloadable\Api\Data\LinkInterface $link, array $sample)
  144. {
  145. if (!empty($sample['url']) || !empty($sample['file'])) {
  146. $downloadableLinkSampleData = $this->objectCopyService->getDataFromFieldset(
  147. 'downloadable_link_sample_data',
  148. 'to_link_sample',
  149. $this->data['sample']
  150. );
  151. $this->dataObjectHelper->populateWithArray(
  152. $link,
  153. array_merge(
  154. $this->data,
  155. $downloadableLinkSampleData
  156. ),
  157. \Magento\Downloadable\Api\Data\LinkInterface::class
  158. );
  159. if ($link->getSampleType() === \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE) {
  160. $linkSampleFileName = $this->downloadableFile->moveFileFromTmp(
  161. $this->getComponent()->getBaseSampleTmpPath(),
  162. $this->getComponent()->getBaseSamplePath(),
  163. $sample['file']
  164. );
  165. $link->setSampleFile($linkSampleFileName);
  166. }
  167. }
  168. return $link;
  169. }
  170. }