Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Sources.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
11 use Magento\ImportExport\Helper\Data as DataHelper;
20 
24 class Sources extends AbstractEntity
25 {
33 
37  protected $jsonHelper;
38 
42  private $validator;
43 
47  private $commands = [];
48 
59  public function __construct(
62  ResourceHelper $resourceHelper,
63  DataHelper $dataHelper,
64  ImportData $importData,
65  ValidatorInterface $validator,
66  array $commands = []
67  ) {
68  $this->jsonHelper = $jsonHelper;
69  $this->errorAggregator = $errorAggregator;
70  $this->_resourceHelper = $resourceHelper;
71  $this->_importExportData = $dataHelper;
72  $this->_dataSourceModel = $importData;
73  $this->validator = $validator;
74 
75  foreach ($commands as $command) {
76  if (!$command instanceof CommandInterface) {
77  throw new LocalizedException(
78  __('Source Import Commands must implement %interface.', ['interface' => CommandInterface::class])
79  );
80  }
81  }
82  $this->commands = $commands;
83  }
84 
90  protected function _importData()
91  {
92  $command = $this->getCommandByBehavior($this->getBehavior());
93  while ($bunch = $this->_dataSourceModel->getNextBunch()) {
94  $command->execute($bunch);
95  }
96 
97  return true;
98  }
99 
105  private function getCommandByBehavior($behavior)
106  {
107  if (!isset($this->commands[$behavior])) {
108  throw new LocalizedException(
109  __('There is no command registered for behavior "%behavior".', ['behavior' => $behavior])
110  );
111  }
112 
113  return $this->commands[$behavior];
114  }
115 
121  public function getEntityTypeCode()
122  {
123  return 'stock_sources';
124  }
125 
133  public function validateRow(array $rowData, $rowNum)
134  {
135  $result = $this->validator->validate($rowData, $rowNum);
136  if ($result->isValid()) {
137  return true;
138  }
139 
140  foreach ($result->getErrors() as $error) {
141  $this->addRowError($error, $rowNum);
142  }
143 
144  return false;
145  }
146 }
__construct(Json $jsonHelper, ProcessingErrorAggregatorInterface $errorAggregator, ResourceHelper $resourceHelper, DataHelper $dataHelper, ImportData $importData, ValidatorInterface $validator, array $commands=[])
Definition: Sources.php:59
__()
Definition: __.php:13
addRowError( $errorCode, $errorRowNum, $colName=null, $errorMessage=null, $errorLevel=ProcessingError::ERROR_LEVEL_CRITICAL, $errorDescription=null)