Skip to content

Commit 906f023

Browse files
authored
Merge pull request #116 from ctidigital/sequence-component
Sequence component
2 parents 3bf6d90 + fd589f0 commit 906f023

File tree

5 files changed

+149
-1
lines changed

5 files changed

+149
-1
lines changed

Component/Sequence.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
namespace CtiDigital\Configurator\Component;
4+
5+
use CtiDigital\Configurator\Api\ComponentInterface;
6+
use CtiDigital\Configurator\Exception\ComponentException;
7+
use Magento\SalesSequence\Model\Builder;
8+
use Magento\SalesSequence\Model\EntityPool;
9+
use Magento\SalesSequence\Model\Config;
10+
use Magento\Store\Api\StoreRepositoryInterface;
11+
use CtiDigital\Configurator\Api\LoggerInterface;
12+
13+
class Sequence implements ComponentInterface
14+
{
15+
/**
16+
* @var Builder
17+
*/
18+
protected $sequenceBuilder;
19+
20+
/**
21+
* @var EntityPool
22+
*/
23+
protected $entityPool;
24+
25+
/**
26+
* @var Config
27+
*/
28+
protected $sequenceConfig;
29+
30+
/**
31+
* @var StoreRepositoryInterface
32+
*/
33+
protected $storeRepository;
34+
35+
protected $logger;
36+
37+
protected $alias = 'sequence';
38+
protected $description = 'Component to allow manual configuring of the sequence tables.';
39+
40+
public function __construct(
41+
Builder $sequenceBuilder,
42+
EntityPool $entityPool,
43+
Config $sequenceConfig,
44+
StoreRepositoryInterface $repository,
45+
LoggerInterface $logger
46+
) {
47+
$this->sequenceBuilder = $sequenceBuilder;
48+
$this->entityPool = $entityPool;
49+
$this->sequenceConfig = $sequenceConfig;
50+
$this->storeRepository = $repository;
51+
$this->logger = $logger;
52+
}
53+
54+
public function execute($data)
55+
{
56+
if (!isset($data['stores'])) {
57+
throw new ComponentException("No stores found.");
58+
}
59+
60+
foreach ($data['stores'] as $code => $overrides) {
61+
try {
62+
$this->logger->logInfo(__("Starting creating sequence tables for %1", $code));
63+
$store = $this->storeRepository->get($code);
64+
$this->newSequenceTable($store, $overrides);
65+
$this->logger->logInfo(__("Finished creating sequence tables for %1", $code));
66+
// todo handle existing sequence tables
67+
} catch (\Exception $exception) {
68+
$this->logger->logError($exception->getMessage());
69+
}
70+
}
71+
}
72+
73+
public function getAlias()
74+
{
75+
return $this->alias;
76+
}
77+
78+
public function getDescription()
79+
{
80+
return $this->description;
81+
}
82+
83+
protected function newSequenceTable($store, $overrides)
84+
{
85+
$configKeys = ['suffix', 'startValue', 'step', 'warningValue', 'maxValue'];
86+
$configValues = [];
87+
foreach ($configKeys as $key) {
88+
$configValues[$key] = $this->sequenceConfig->get($key);
89+
if (isset($overrides[$key])) {
90+
$configValues[$key] = $overrides[$key];
91+
}
92+
}
93+
94+
// Prefix Value
95+
$configValues['prefix'] = $store->getId();
96+
if (isset($overrides['prefix'])) {
97+
$configValues['prefix'] = $overrides['prefix'];
98+
}
99+
100+
foreach ($this->entityPool->getEntities() as $entityType) {
101+
try {
102+
$this->logger->logComment(__(
103+
'Store: %1 '.
104+
'Prefix: %2, '.
105+
'Suffix: %3, '.
106+
'Start Value: %4, '.
107+
'Step: %5, '.
108+
'Warning Value: %6, '.
109+
'Max Value: %7, '.
110+
'Entity Type: %8',
111+
$store->getCode(),
112+
$configValues['prefix'],
113+
$configValues['suffix'],
114+
$configValues['startValue'],
115+
$configValues['step'],
116+
$configValues['warningValue'],
117+
$configValues['maxValue'],
118+
$entityType
119+
), 1);
120+
$this->sequenceBuilder->setPrefix($configValues['prefix'])
121+
->setSuffix($configValues['suffix'])
122+
->setStartValue($configValues['startValue'])
123+
->setStoreId($store->getId())
124+
->setStep($configValues['step'])
125+
->setWarningValue($configValues['warningValue'])
126+
->setMaxValue($configValues['maxValue'])
127+
->setEntityType($entityType)
128+
->create();
129+
$this->logger->logInfo(__("Sequence table created for %1", $entityType), 1);
130+
} catch (\Exception $exception) {
131+
$this->logger->logError($exception->getMessage());
132+
}
133+
}
134+
}
135+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
stores:
2+
default:
3+
prefix: PREFIX_
4+
startValue: 5000
5+
usa_en_us:
6+
prefix: USA_
7+
startValue: 1000

Samples/master.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ config:
1818
sources:
1919
- ../configurator/Configuration/global.yaml
2020
- ../configurator/Configuration/base-website-config.yaml
21+
sequence:
22+
enabled: 1
23+
method: code
24+
sources:
25+
- ../configurator/Sequence/sequence.yaml
2126
attributes:
2227
enabled: 1
2328
method: code

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"phpunit/phpunit": "^6.2",
2222
"magento/magento-coding-standard": "5"
2323
},
24-
"version": "3.0.0",
24+
"version": "3.1.0",
2525
"autoload": {
2626
"files": [ "registration.php" ],
2727
"psr-4": {

etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<argument name="components" xsi:type="array">
2525
<item name="websites" xsi:type="object">CtiDigital\Configurator\Component\Websites</item>
2626
<item name="config" xsi:type="object">CtiDigital\Configurator\Component\Config</item>
27+
<item name="sequence" xsi:type="object">CtiDigital\Configurator\Component\Sequence</item>
2728
<item name="attributes" xsi:type="object">CtiDigital\Configurator\Component\Attributes</item>
2829
<item name="attribute_sets" xsi:type="object">CtiDigital\Configurator\Component\AttributeSets</item>
2930
<item name="adminroles" xsi:type="object">CtiDigital\Configurator\Component\AdminRoles</item>

0 commit comments

Comments
 (0)