Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Sequence.php
Go to the documentation of this file.
1 <?php
7 
10 
17 class Sequence implements SequenceInterface
18 {
22  const DEFAULT_PATTERN = "%s%'.09d%s";
23 
27  private $lastIncrementId;
28 
32  private $meta;
33 
37  private $connection;
38 
42  private $pattern;
43 
49  public function __construct(
50  Meta $meta,
52  $pattern = self::DEFAULT_PATTERN
53  ) {
54  $this->meta = $meta;
55  $this->connection = $resource->getConnection('sales');
56  $this->pattern = $pattern;
57  }
58 
64  public function getCurrentValue()
65  {
66  if (!isset($this->lastIncrementId)) {
67  return null;
68  }
69 
70  return sprintf(
71  $this->pattern,
72  $this->meta->getActiveProfile()->getPrefix(),
73  $this->calculateCurrentValue(),
74  $this->meta->getActiveProfile()->getSuffix()
75  );
76  }
77 
83  public function getNextValue()
84  {
85  $this->connection->insert($this->meta->getSequenceTable(), []);
86  $this->lastIncrementId = $this->connection->lastInsertId($this->meta->getSequenceTable());
87  return $this->getCurrentValue();
88  }
89 
95  private function calculateCurrentValue()
96  {
97  return ($this->lastIncrementId - $this->meta->getActiveProfile()->getStartValue())
98  * $this->meta->getActiveProfile()->getStep() + $this->meta->getActiveProfile()->getStartValue();
99  }
100 }
$resource
Definition: bulk.php:12
__construct(Meta $meta, AppResource $resource, $pattern=self::DEFAULT_PATTERN)
Definition: Sequence.php:49