-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathOrderSimulate.php
More file actions
166 lines (148 loc) · 4.55 KB
/
Copy pathOrderSimulate.php
File metadata and controls
166 lines (148 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* Copyright © Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magmodules\Channable\Console\Command;
use Magento\Framework\App\Area;
use Magento\Framework\Console\Cli;
use Magmodules\Channable\Service\Order\ImportSimulator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\Framework\App\State as AppState;
/**
* CLI command to simulate an order import
*/
class OrderSimulate extends Command
{
/**
* Names of input arguments or options
*/
private const INPUT_KEY_STORE_ID = 'store-id';
private const INPUT_KEY_PRODUCT_ID = 'product-id';
private const INPUT_KEY_PRODUCT_QTY = 'qty';
private const INPUT_KEY_COUNTRY_CODE = 'country-code';
private const INPUT_KEY_LVB = 'lvb';
/**
* Command call name
*/
private const COMMAND_NAME = 'channable:order:simulate';
/**
* @var ImportSimulator
*/
private $importSimulator;
/**
* @var AppState
*/
private $appState;
/**
* @param ImportSimulator $importSimulator
* @param AppState $appState
*/
public function __construct(
ImportSimulator $importSimulator,
AppState $appState
) {
$this->importSimulator = $importSimulator;
$this->appState = $appState;
parent::__construct();
}
/**
* @inheritdoc
*/
public function configure()
{
$this->setName(self::COMMAND_NAME);
$this->setDescription('Simulate an order import');
$this->addOption(
self::INPUT_KEY_STORE_ID,
null,
InputOption::VALUE_REQUIRED,
'store id of the store where the order should be imported'
);
$this->addOption(
self::INPUT_KEY_PRODUCT_ID,
null,
InputOption::VALUE_OPTIONAL,
'product id of the product that should be used for the order'
);
$this->addOption(
self::INPUT_KEY_PRODUCT_QTY,
null,
InputOption::VALUE_OPTIONAL,
'product qty that should be used for the order'
);
$this->addOption(
self::INPUT_KEY_COUNTRY_CODE,
null,
InputOption::VALUE_OPTIONAL,
'country code for billing and shipping address'
);
$this->addOption(
self::INPUT_KEY_LVB,
null,
InputOption::VALUE_OPTIONAL,
'should order be treated as lvd (1) or not (0)'
);
$this->addOption(
self::INPUT_KEY_LVB,
null,
InputOption::VALUE_OPTIONAL,
'should order be treated as lvd (1) or not (0)'
);
parent::configure();
}
/**
* @inheritdoc
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
if (!$input->getOption(self::INPUT_KEY_STORE_ID)) {
throw new \InvalidArgumentException('Please add ' . self::INPUT_KEY_STORE_ID . ' param.');
} else {
$storeId = (int)$input->getOption(self::INPUT_KEY_STORE_ID);
}
try {
$this->appState->setAreaCode(Area::AREA_FRONTEND);
$order = $this->importSimulator->execute(
$storeId,
$this->getOptions($input)
);
$output->writeln(
sprintf('<info>Test order #%s created</info>', $order->getIncrementId())
);
return Cli::RETURN_SUCCESS;
} catch (\Exception $exception) {
$output->writeln(
sprintf('<error>%s</error>', $exception->getMessage())
);
return Cli::RETURN_FAILURE;
}
}
/**
* Process input options to array
*
* @param InputInterface $input
* @return array
*/
private function getOptions(InputInterface $input): array
{
$options = [];
if ($productId = $input->getOption(self::INPUT_KEY_PRODUCT_ID)) {
$options['product_id'] = $productId;
}
if ($qty = $input->getOption(self::INPUT_KEY_PRODUCT_QTY)) {
$options['qty'] = (float)$qty;
}
if ($lvb = $input->getOption(self::INPUT_KEY_LVB)) {
$options['lvb'] = (bool)$lvb;
}
if ($country = $input->getOption(self::INPUT_KEY_COUNTRY_CODE)) {
$options['country'] = $country;
}
return $options;
}
}