Conditions | 31 |
Paths | 413 |
Total Lines | 122 |
Code Lines | 80 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
57 | protected function execute(InputInterface $input, OutputInterface $output) |
||
58 | { |
||
59 | if (!$input->getOption('delete') && !$input->getOption('info') && !$input->getOption('fail')) { |
||
60 | throw new \InvalidArgumentException('You must specify whether you want to --delete, --fail or --info the specified workflow.'); |
||
61 | } |
||
62 | |||
63 | $workflowService = $this->getWorkflowService(); |
||
64 | $workflowNameOrPath = $input->getArgument('workflow'); |
||
65 | |||
66 | if ($input->getOption('info')) { |
||
67 | $output->writeln(''); |
||
68 | |||
69 | $workflow = $workflowService->getWorkflow($workflowNameOrPath); |
||
|
|||
70 | if ($workflow == null) { |
||
71 | throw new \InvalidArgumentException(sprintf('The workflow "%s" does not exist in the database table.', $workflowNameOrPath)); |
||
72 | } |
||
73 | |||
74 | switch ($workflow->status) { |
||
75 | case Migration::STATUS_DONE: |
||
76 | $status = '<info>executed</info>'; |
||
77 | break; |
||
78 | case Migration::STATUS_STARTED: |
||
79 | $status = '<comment>execution started</comment>'; |
||
80 | break; |
||
81 | case Migration::STATUS_TODO: |
||
82 | // bold to-migrate! |
||
83 | $status = '<error>not executed</error>'; |
||
84 | break; |
||
85 | case Migration::STATUS_SKIPPED: |
||
86 | $status = '<comment>skipped</comment>'; |
||
87 | break; |
||
88 | case Migration::STATUS_PARTIALLY_DONE: |
||
89 | $status = '<comment>partially executed</comment>'; |
||
90 | break; |
||
91 | case Migration::STATUS_SUSPENDED: |
||
92 | $status = '<comment>suspended</comment>'; |
||
93 | break; |
||
94 | case Migration::STATUS_FAILED: |
||
95 | $status = '<error>failed</error>'; |
||
96 | break; |
||
97 | } |
||
98 | |||
99 | $output->writeln('<info>Workflow: ' . $workflow->name . '</info>'); |
||
100 | $output->writeln('Status: ' . $status); |
||
101 | $output->writeln('Executed on: <info>' . ($workflow->executionDate != null ? date("Y-m-d H:i:s", $workflow->executionDate) : '--'). '</info>'); |
||
102 | $output->writeln('Execution notes: <info>' . $workflow->executionError . '</info>'); |
||
103 | $output->writeln('Signal: <info>' . $workflow->signalName . '</info>'); |
||
104 | |||
105 | if ($workflow->status == Migration::STATUS_SUSPENDED) { |
||
106 | /// @todo decode the suspension context: date, step, ... |
||
107 | } |
||
108 | |||
109 | $output->writeln('Definition path: <info>' . $workflow->path . '</info>'); |
||
110 | $output->writeln('Definition md5: <info>' . $workflow->md5 . '</info>'); |
||
111 | |||
112 | if ($workflow->path != '') { |
||
113 | // q: what if we have a loader which does not work with is_file? We could probably remove this check... |
||
114 | if (is_file($workflow->path)) { |
||
115 | try { |
||
116 | $workflowDefinitionCollection = $workflowService->getWorkflowsDefinitions(array($workflow->path)); |
||
117 | if (count($workflowDefinitionCollection)) { |
||
118 | $workflowDefinition = $workflowDefinitionCollection->reset(); |
||
119 | $workflowDefinition = $workflowService->parseWorkflowDefinition($workflowDefinition); |
||
120 | |||
121 | if ($workflowDefinition->status != MigrationDefinition::STATUS_PARSED) { |
||
122 | $output->writeln('Definition error: <error>' . $workflowDefinition->parsingError . '</error>'); |
||
123 | } |
||
124 | |||
125 | if (md5($workflowDefinition->rawDefinition) != $workflow->md5) { |
||
126 | $output->writeln('Notes: <comment>The workflow definition file has now a different checksum</comment>'); |
||
127 | } |
||
128 | |||
129 | $output->writeln('Switch User: <info>' . (($workflowDefinition->runAs === false) ? '-' : $workflowDefinition->runAs) . '</info>'); |
||
130 | $output->writeln('Use transaction: <info>' . ($workflowDefinition->useTransaction ? 'Y' : 'N') . '</info>'); |
||
131 | |||
132 | } else { |
||
133 | $output->writeln('Definition error: <error>The workflow definition file can not be loaded</error>'); |
||
134 | } |
||
135 | } catch (\Exception $e) { |
||
136 | /// @todo one day we should be able to limit the kind of exceptions we have to catch here... |
||
137 | $output->writeln('Definition parsing error: <error>' . $e->getMessage() . '</error>'); |
||
138 | } |
||
139 | } else { |
||
140 | $output->writeln('Definition error: <error>The workflow definition file can not be found any more</error>'); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | $output->writeln(''); |
||
145 | return 0; |
||
146 | } |
||
147 | |||
148 | // ask user for confirmation to make changes |
||
149 | if ($input->isInteractive() && !$input->getOption('no-interaction')) { |
||
150 | $dialog = $this->getHelperSet()->get('dialog'); |
||
151 | if (!$dialog->askConfirmation( |
||
152 | $output, |
||
153 | '<question>Careful, the database will be modified. Do you want to continue Y/N ?</question>', |
||
154 | false |
||
155 | ) |
||
156 | ) { |
||
157 | $output->writeln('<error>Workflow change cancelled!</error>'); |
||
158 | return 0; |
||
159 | } |
||
160 | } |
||
161 | |||
162 | if ($input->getOption('delete') || $input->getOption('fail')) { |
||
163 | $workflow = $workflowService->getWorkflow($workflowNameOrPath); |
||
164 | if ($workflow == null) { |
||
165 | throw new \InvalidArgumentException(sprintf('The workflow "%s" does not exist in the database table.', $workflowNameOrPath)); |
||
166 | } |
||
167 | |||
168 | if ($input->getOption('delete')) { |
||
169 | $workflowService->deleteWorkflow($workflow); |
||
170 | } else { |
||
171 | $errorMessage = 'Manually failed on ' . date("Y-m-d H:i:s"); |
||
172 | if ($workflow->executionError != '') { |
||
173 | $errorMessage .= ". Previous notes: " . $workflow->executionError; |
||
174 | } |
||
175 | $workflowService->failWorkflow($workflow, $errorMessage); |
||
176 | } |
||
177 | |||
178 | return 0; |
||
179 | } |
||
182 |