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 
9 use Magento\Framework\DB\Ddl\Sequence as DdlSequence;
12 use Psr\Log\LoggerInterface as Logger;
13 
21 class Builder
22 {
26  protected $resourceMetadata;
27 
31  protected $profileFactory;
32 
36  protected $metaFactory;
37 
41  protected $appResource;
42 
46  protected $ddlSequence;
47 
53  protected $required = [
54  'entityType',
55  'storeId'
56  ];
57 
63  protected $pattern = [
64  'entity_type',
65  'store_id',
66  'prefix',
67  'suffix',
68  'start_value',
69  'step',
70  'max_value',
71  'warning_value',
72  ];
73 
79  protected $data = [];
80 
84  protected $logger;
85 
94  public function __construct(
95  ResourceMetadata $resourceMetadata,
96  MetaFactory $metaFactory,
97  ProfileFactory $profileFactory,
99  DdlSequence $ddlSequence,
100  Logger $logger
101  ) {
102  $this->resourceMetadata = $resourceMetadata;
103  $this->metaFactory = $metaFactory;
104  $this->profileFactory = $profileFactory;
105  $this->appResource = $appResource;
106  $this->ddlSequence = $ddlSequence;
107  $this->logger = $logger;
108  $this->data = array_flip($this->pattern);
109  }
110 
115  public function setEntityType($entityType)
116  {
117  $this->data['entity_type'] = $entityType;
118  return $this;
119  }
120 
125  public function setStoreId($storeId)
126  {
127  $this->data['store_id'] = $storeId;
128  return $this;
129  }
130 
135  public function setPrefix($prefix)
136  {
137  $this->data['prefix'] = $prefix;
138  return $this;
139  }
140 
145  public function setSuffix($suffix)
146  {
147  $this->data['suffix'] = $suffix;
148  return $this;
149  }
150 
155  public function setStartValue($startValue)
156  {
157  $this->data['start_value'] = $startValue;
158  return $this;
159  }
160 
165  public function setStep($step)
166  {
167  $this->data['step'] = $step;
168  return $this;
169  }
170 
175  public function setMaxValue($maxValue)
176  {
177  $this->data['max_value'] = $maxValue;
178  return $this;
179  }
180 
185  public function setWarningValue($warningValue)
186  {
187  $this->data['warning_value'] = $warningValue;
188  return $this;
189  }
190 
196  protected function getSequenceName()
197  {
198  return $this->appResource->getTableName(
199  sprintf(
200  'sequence_%s_%s',
201  $this->data['entity_type'],
202  $this->data['store_id']
203  )
204  );
205  }
206 
214  public function create()
215  {
216  $metadata = $this->resourceMetadata->loadByEntityTypeAndStore(
217  $this->data['entity_type'],
218  $this->data['store_id']
219  );
220  if ($metadata->getSequenceTable() == $this->getSequenceName()) {
221  return;
222  }
223  $this->data['sequence_table'] = $this->getSequenceName();
224  $this->data['is_active'] = 1;
225  $profile = $this->profileFactory->create(
226  [
227  'data' => array_intersect_key(
228  $this->data,
229  array_flip(
230  [
231  'prefix', 'suffix', 'start_value', 'step', 'max_value', 'warning_value',
232  'is_active', 'active_profile'
233  ]
234  )
235  )
236  ]
237  );
238  $profile->setHasDataChanges(true);
239  $this->data['active_profile'] = $profile;
240  $metadata = $this->metaFactory->create(
241  [
242  'data' => array_intersect_key(
243  $this->data,
244  array_flip(['entity_type', 'store_id', 'sequence_table', 'active_profile'])
245  )
246  ]
247  );
248  $metadata->setHasDataChanges(true);
249  try {
250  $this->resourceMetadata->save($metadata);
251  $connection = $this->appResource->getConnection('sales');
252  if (!$connection->isTableExists($this->data['sequence_table'])) {
253  $connection->query(
254  $this->ddlSequence->getCreateSequenceDdl(
255  $this->data['sequence_table'],
256  $this->data['start_value']
257  )
258  );
259  }
260  } catch (Exception $e) {
261  $this->resourceMetadata->delete($metadata);
262  $this->logger->critical($e);
263  throw $e;
264  }
265  $this->data = array_flip($this->pattern);
266  }
267 }
$suffix
Definition: name.phtml:27
$prefix
Definition: name.phtml:25
$connection
Definition: bulk.php:13
__construct(ResourceMetadata $resourceMetadata, MetaFactory $metaFactory, ProfileFactory $profileFactory, AppResource $appResource, DdlSequence $ddlSequence, Logger $logger)
Definition: Builder.php:94