1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||
6 | */ |
||
7 | |||
8 | namespace Spryker\Zed\StateMachine\Communication\Console; |
||
9 | |||
10 | use Spryker\Zed\Kernel\Communication\Console\Console; |
||
11 | use Symfony\Component\Console\Input\InputArgument; |
||
12 | use Symfony\Component\Console\Input\InputInterface; |
||
13 | use Symfony\Component\Console\Input\InputOption; |
||
14 | use Symfony\Component\Console\Output\OutputInterface; |
||
15 | |||
16 | /** |
||
17 | * @method \Spryker\Zed\StateMachine\Business\StateMachineFacadeInterface getFacade() |
||
18 | * @method \Spryker\Zed\StateMachine\Persistence\StateMachineQueryContainerInterface getQueryContainer() |
||
19 | * @method \Spryker\Zed\StateMachine\Communication\StateMachineCommunicationFactory getFactory() |
||
20 | * @method \Spryker\Zed\StateMachine\Persistence\StateMachineRepositoryInterface getRepository() |
||
21 | */ |
||
22 | class CheckTimeoutConsole extends Console |
||
23 | { |
||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | public const COMMAND_NAME = 'state-machine:check-timeout'; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | public const COMMAND_DESCRIPTION = 'Check timeouts'; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | public const ARGUMENT_STATE_MACHINE_NAME = 'state machine name'; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | public const OPTION_STATE_MACHINE_NAME = 'state-machine-name'; |
||
43 | |||
44 | /** |
||
45 | * @return void |
||
46 | */ |
||
47 | protected function configure(): void |
||
48 | { |
||
49 | $this->setName(static::COMMAND_NAME); |
||
50 | $this->setDescription(static::COMMAND_DESCRIPTION); |
||
51 | |||
52 | $this->addArgument( |
||
53 | static::ARGUMENT_STATE_MACHINE_NAME, |
||
54 | InputArgument::OPTIONAL, |
||
55 | 'Name of state machine to trigger timeout expired items', |
||
56 | ); |
||
57 | |||
58 | $this->addOption( |
||
59 | static::OPTION_STATE_MACHINE_NAME, |
||
60 | 's', |
||
61 | InputOption::VALUE_REQUIRED, |
||
62 | '(deprecated) Name of state machine to trigger timeout expired items', |
||
63 | ); |
||
64 | |||
65 | parent::configure(); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
70 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
71 | * |
||
72 | * @return int |
||
73 | */ |
||
74 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
75 | { |
||
76 | /** @var string|null $optionStateMachineName */ |
||
77 | $optionStateMachineName = $this->input->getOption(static::OPTION_STATE_MACHINE_NAME); |
||
78 | /** @var string|null $argumentStateMachineName */ |
||
79 | $argumentStateMachineName = $this->input->getArgument(static::ARGUMENT_STATE_MACHINE_NAME); |
||
80 | |||
81 | $isValidArgument = $this->validateStateMachineNameArgument($argumentStateMachineName); |
||
82 | if ($isValidArgument === null) { |
||
83 | $this->validateStateMachineNameOption($optionStateMachineName); |
||
84 | } |
||
85 | if ($isValidArgument === false) { |
||
86 | return static::CODE_SUCCESS; |
||
87 | } |
||
88 | |||
89 | $affected = $this->getFacade()->checkTimeouts($isValidArgument === null ? $optionStateMachineName : $argumentStateMachineName); |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
90 | |||
91 | if ($output->isVerbose()) { |
||
92 | $output->writeln('Affected: ' . $affected); |
||
93 | } |
||
94 | |||
95 | return static::CODE_SUCCESS; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param string|null $stateMachineName |
||
100 | * |
||
101 | * @return void |
||
102 | */ |
||
103 | protected function validateStateMachineNameOption($stateMachineName) |
||
104 | { |
||
105 | if ($stateMachineName === null) { |
||
106 | $this->info('No state machine name was provided.'); |
||
107 | |||
108 | return; |
||
109 | } |
||
110 | |||
111 | if (!$this->getFacade()->stateMachineExists($stateMachineName)) { |
||
112 | $this->info(sprintf('State machine "%s" was not found. ', $stateMachineName)); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param string|null $stateMachineName |
||
118 | * |
||
119 | * @return bool|null |
||
120 | */ |
||
121 | protected function validateStateMachineNameArgument($stateMachineName) |
||
122 | { |
||
123 | if ($stateMachineName === null) { |
||
124 | return null; |
||
125 | } |
||
126 | |||
127 | if ($this->getFacade()->stateMachineExists($stateMachineName)) { |
||
128 | return true; |
||
129 | } |
||
130 | |||
131 | $this->error(sprintf('State machine "%s" was not found.', $stateMachineName)); |
||
132 | |||
133 | return false; |
||
134 | } |
||
135 | } |
||
136 |