-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCreate.php
More file actions
145 lines (129 loc) · 4.09 KB
/
Copy pathCreate.php
File metadata and controls
145 lines (129 loc) · 4.09 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
<?php
/**
* @author Artemii Karkusha
* @copyright Copyright (c) (https://www.linkedin.com/in/artemiy-karkusha/)
*/
declare(strict_types=1);
namespace ArtemiiKarkusha\AdobeCommercePreparingToCertification\Console\Command\DeliveryService;
use Exception;
use Magento\Framework\Console\Cli;
use Magento\Framework\Exception\LocalizedException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
class Create extends Command
{
private const EXAMPLE_REQUIRED_OPTION = 'example_required_option';
private const EXAMPLE_OPTIONAL_OPTION = 'example_optional_option';
private const EXAMPLE_NONE_OPTION = 'example_none_option';
private const EXAMPLE_ARGUMENT = 'example_argument';
/**
* @var InputInterface
*/
private InputInterface $input;
/**
* @var OutputInterface
*/
private OutputInterface $output;
/**
* Initialization of the command.
*/
protected function configure(): void
{
$this->addOption(
self::EXAMPLE_REQUIRED_OPTION,
null,
InputOption::VALUE_REQUIRED,
self::EXAMPLE_REQUIRED_OPTION
);
$this->addOption(
self::EXAMPLE_OPTIONAL_OPTION,
null,
InputOption::VALUE_OPTIONAL,
self::EXAMPLE_OPTIONAL_OPTION
);
$this->addOption(
self::EXAMPLE_NONE_OPTION,
null,
InputOption::VALUE_NONE,
self::EXAMPLE_NONE_OPTION
);
$this->addArgument(
self::EXAMPLE_ARGUMENT,
InputArgument::OPTIONAL,
'Name of the module'
);
$this->setName('preparingToCertification:deliveryService:create');
$this->setDescription('Example console command');
parent::configure();
}
/**
* @inheritDoc
* @noinspection PhpMissingParentCallCommonInspection
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->initializeProperties($input, $output);
try {
$this->checkRequireOptionAndArguments();
$this->outputOptionInfo((string)$input->getOption(self::EXAMPLE_REQUIRED_OPTION));
$this->outputOptionInfo((string)$input->getOption(self::EXAMPLE_OPTIONAL_OPTION));
$this->doSomethingIfOptionNoneExist();
$output->writeln('<info>Success message.</info>');
$output->writeln('<comment>Some comment.</comment>');
return Cli::RETURN_SUCCESS;
} catch (Exception $exception) {
$output->writeln(
sprintf(
'<error>%s</error>',
$exception->getMessage()
)
);
return Cli::RETURN_FAILURE;
}
}
/**
* @return void
*
* @throws LocalizedException
*/
private function checkRequireOptionAndArguments(): void
{
if (!$this->input->getOption(self::EXAMPLE_REQUIRED_OPTION)) {
throw new LocalizedException(
__(sprintf('The option %s is required.', self::EXAMPLE_REQUIRED_OPTION))
);
}
}
/**
* @param string $optionValue
* @return void
*/
private function outputOptionInfo(string $optionValue): void
{
$this->output->writeln('<info>Provided optional option name is `' . $optionValue . '`</info>');
}
/**
* @return void
*/
private function doSomethingIfOptionNoneExist(): void
{
if (!$this->input->getOption(self::EXAMPLE_NONE_OPTION)) {
return;
}
$this->output->writeln('<info>None option has been called</info>');
}
/**
*
* @param InputInterface $input
* @param OutputInterface $output
* @return void
*/
private function initializeProperties(InputInterface $input, OutputInterface $output): void
{
$this->input = $input;
$this->output = $output;
}
}