Builder.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Model\Sample;
  7. use Magento\Downloadable\Api\Data\SampleInterface;
  8. use Magento\Catalog\Model\Product;
  9. use Magento\Downloadable\Helper\File;
  10. use Magento\Downloadable\Model\Sample;
  11. use Magento\Downloadable\Model\SampleFactory;
  12. use Magento\Framework\Api\DataObjectHelper;
  13. use Magento\Framework\DataObject\Copy;
  14. /**
  15. * Class Builder
  16. * @api
  17. * @since 100.1.0
  18. */
  19. class Builder
  20. {
  21. /**
  22. * @var Sample
  23. */
  24. private $component;
  25. /**
  26. * @var File
  27. */
  28. private $downloadableFile;
  29. /**
  30. * @var Copy
  31. */
  32. private $objectCopyService;
  33. /**
  34. * @var DataObjectHelper
  35. */
  36. private $dataObjectHelper;
  37. /**
  38. * @var SampleFactory
  39. */
  40. private $componentFactory;
  41. /**
  42. * @var array
  43. */
  44. private $data = [];
  45. /**
  46. * Mapper constructor.
  47. *
  48. * @param File $downloadableFile
  49. * @param Copy $objectCopyService
  50. * @param DataObjectHelper $dataObjectHelper
  51. * @param SampleFactory $componentFactory
  52. */
  53. public function __construct(
  54. File $downloadableFile,
  55. Copy $objectCopyService,
  56. DataObjectHelper $dataObjectHelper,
  57. SampleFactory $componentFactory
  58. ) {
  59. $this->downloadableFile = $downloadableFile;
  60. $this->objectCopyService = $objectCopyService;
  61. $this->dataObjectHelper = $dataObjectHelper;
  62. $this->componentFactory = $componentFactory;
  63. }
  64. /**
  65. * @param array $data
  66. * @return $this;
  67. * @since 100.1.0
  68. * @since 100.1.0
  69. */
  70. public function setData(array $data)
  71. {
  72. $this->data = $data;
  73. return $this;
  74. }
  75. /**
  76. * @param SampleInterface $sample
  77. * @return SampleInterface
  78. * @throws \Magento\Framework\Exception\LocalizedException
  79. * @since 100.1.0
  80. */
  81. public function build(SampleInterface $sample)
  82. {
  83. $downloadableData = $this->objectCopyService->getDataFromFieldset(
  84. 'downloadable_data',
  85. 'to_sample',
  86. $this->data
  87. );
  88. $this->dataObjectHelper->populateWithArray(
  89. $sample,
  90. array_merge(
  91. $this->data,
  92. $downloadableData
  93. ),
  94. SampleInterface::class
  95. );
  96. if ($sample->getSampleType() === \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE) {
  97. if (!isset($this->data['file'])) {
  98. throw new \Magento\Framework\Exception\LocalizedException(__('Sample file not provided'));
  99. }
  100. $fileName = $this->downloadableFile->moveFileFromTmp(
  101. $this->getComponent()->getBaseTmpPath(),
  102. $this->getComponent()->getBasePath(),
  103. $this->data['file']
  104. );
  105. $sample->setSampleFile($fileName);
  106. }
  107. if (!$sample->getSortOrder()) {
  108. $sample->setSortOrder(1);
  109. }
  110. $this->resetData();
  111. return $sample;
  112. }
  113. /**
  114. * @return void
  115. */
  116. private function resetData()
  117. {
  118. $this->data = [];
  119. }
  120. /**
  121. * @return Sample
  122. */
  123. private function getComponent()
  124. {
  125. if (!$this->component) {
  126. $this->component = $this->componentFactory->create();
  127. }
  128. return $this->component;
  129. }
  130. }