Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Builder.php
Go to the documentation of this file.
1 <?php
7 
12 use Magento\Downloadable\Model\SampleFactory;
15 
21 class Builder
22 {
26  private $component;
27 
31  private $downloadableFile;
32 
36  private $objectCopyService;
37 
41  private $dataObjectHelper;
42 
46  private $componentFactory;
47 
51  private $data = [];
52 
61  public function __construct(
62  File $downloadableFile,
63  Copy $objectCopyService,
64  DataObjectHelper $dataObjectHelper,
65  SampleFactory $componentFactory
66  ) {
67  $this->downloadableFile = $downloadableFile;
68  $this->objectCopyService = $objectCopyService;
69  $this->dataObjectHelper = $dataObjectHelper;
70  $this->componentFactory = $componentFactory;
71  }
72 
79  public function setData(array $data)
80  {
81  $this->data = $data;
82  return $this;
83  }
84 
91  public function build(SampleInterface $sample)
92  {
93  $downloadableData = $this->objectCopyService->getDataFromFieldset(
94  'downloadable_data',
95  'to_sample',
96  $this->data
97  );
98  $this->dataObjectHelper->populateWithArray(
99  $sample,
100  array_merge(
101  $this->data,
103  ),
104  SampleInterface::class
105  );
107  if (!isset($this->data['file'])) {
108  throw new \Magento\Framework\Exception\LocalizedException(__('Sample file not provided'));
109  }
110  $fileName = $this->downloadableFile->moveFileFromTmp(
111  $this->getComponent()->getBaseTmpPath(),
112  $this->getComponent()->getBasePath(),
113  $this->data['file']
114  );
115  $sample->setSampleFile($fileName);
116  }
117  if (!$sample->getSortOrder()) {
118  $sample->setSortOrder(1);
119  }
120  $this->resetData();
121 
122  return $sample;
123  }
124 
128  private function resetData()
129  {
130  $this->data = [];
131  }
132 
136  private function getComponent()
137  {
138  if (!$this->component) {
139  $this->component = $this->componentFactory->create();
140  }
141  return $this->component;
142  }
143 }
build(SampleInterface $sample)
Definition: Builder.php:91
__()
Definition: __.php:13
$fileName
Definition: translate.phtml:15
__construct(File $downloadableFile, Copy $objectCopyService, DataObjectHelper $dataObjectHelper, SampleFactory $componentFactory)
Definition: Builder.php:61