Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/code/Magento/SalesSequence/Model/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
* Class Meta
*
* @api
* @method Profile getActiveProfile()
* @method string getEntityType()
* @method int getStoreId()
* @method string getSequenceTable()
* @since 100.0.2
*/
class Meta extends AbstractModel
Expand Down
4 changes: 4 additions & 0 deletions app/code/Magento/SalesSequence/Model/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
* Class Profile
*
* @api
* @method string getPrefix()
* @method string getSuffix()
* @method int getStartValue()
* @method int getStep()
* @since 100.0.2
*/
class Profile extends AbstractModel
Expand Down
44 changes: 36 additions & 8 deletions app/code/Magento/SalesSequence/Model/Sequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Sequence implements SequenceInterface
public function __construct(
Meta $meta,
AppResource $resource,
$pattern = self::DEFAULT_PATTERN
string $pattern = self::DEFAULT_PATTERN
) {
$this->meta = $meta;
$this->connection = $resource->getConnection('sales');
Expand All @@ -59,9 +59,9 @@ public function __construct(
/**
* Retrieve current value
*
* @return string
* @return ?string
*/
public function getCurrentValue()
public function getCurrentValue(): ?string
{
if (!isset($this->lastIncrementId)) {
return null;
Expand All @@ -76,11 +76,9 @@ public function getCurrentValue()
}

/**
* Retrieve next value
*
* @return string
* @inheritdoc
*/
public function getNextValue()
public function getNextValue(): string
{
$this->connection->insert($this->meta->getSequenceTable(), []);
$this->lastIncrementId = $this->connection->lastInsertId($this->meta->getSequenceTable());
Expand All @@ -92,9 +90,39 @@ public function getNextValue()
*
* @return string
*/
private function calculateCurrentValue()
public function calculateCurrentValue(): string
{
return ($this->lastIncrementId - $this->meta->getActiveProfile()->getStartValue())
* $this->meta->getActiveProfile()->getStep() + $this->meta->getActiveProfile()->getStartValue();
}

/**
* Retrieve the current entity type
*
* @return string
*/
public function getEntityType(): string
{
return $this->meta->getEntityType();
}

/**
* Retrieve the current store id
*
* @return int
*/
public function getStoreId(): int
{
return $this->meta->getStoreId();
}

/**
* Retrieve the currently active meta profile
*
* @return Profile
*/
public function getMetaProfile(): Profile
{
return $this->meta->getActiveProfile();
}
}
26 changes: 25 additions & 1 deletion app/code/Magento/SalesSequence/Test/Unit/Model/SequenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class SequenceTest extends TestCase
protected function setUp(): void
{
$this->meta = $this->getMockBuilder(Meta::class)
->addMethods(['getSequenceTable', 'getActiveProfile'])
->addMethods(['getSequenceTable', 'getActiveProfile', 'getStoreId', 'getEntityType'])
->disableOriginalConstructor()
->getMock();
$this->profile = $this->getMockBuilder(Profile::class)
Expand Down Expand Up @@ -85,6 +85,30 @@ public function testSequenceInitialNull(): void
$this->assertNull($this->sequence->getCurrentValue());
}

/**
* Test that the meta getters return values as expected
*
* @return void
*/
public function testMetaGetters(): void
{
$exampleEntityType = 'order';
$exampleStoreId = 0;
$this->meta->expects($this->atLeastOnce())
->method('getActiveProfile')
->willReturn($this->profile);

$this->meta->expects($this->atLeastOnce())
->method('getStoreId')
->willReturn($exampleStoreId);
$this->meta->expects($this->atLeastOnce())
->method('getEntityType')
->willReturn($exampleEntityType);

$this->assertEquals($exampleEntityType, $this->sequence->getEntityType());
$this->assertEquals($exampleStoreId, $this->sequence->getStoreId());
}

/**
* @return void
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ interface SequenceInterface
/**
* Retrieve current value
*
* @return string
* @return ?string
*/
public function getCurrentValue();
public function getCurrentValue(): ?string;

/**
* Retrieve next value
*
* @return string
*/
public function getNextValue();
public function getNextValue(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class Sequence implements SequenceInterface
*/
public function __construct(
ResourceConnection $resource,
$connectionName,
$sequenceTable
string $connectionName,
string $sequenceTable
) {
$this->resource = $resource;
$this->connectionName = $connectionName;
Expand All @@ -47,7 +47,7 @@ public function __construct(
/**
* @inheritdoc
*/
public function getNextValue()
public function getNextValue(): string
{
$this->resource->getConnection($this->connectionName)
->insert($this->resource->getTableName($this->sequenceTable), []);
Expand All @@ -58,7 +58,7 @@ public function getNextValue()
/**
* @inheritdoc
*/
public function getCurrentValue()
public function getCurrentValue(): string
{
$select = $this->resource->getConnection($this->connectionName)->select();
$select->from($this->resource->getTableName($this->sequenceTable));
Expand Down