Passed
Branch main (b0ee7b)
by Gaetano
09:00
created

ResumeCommand::execute()   B

Complexity

Conditions 11
Paths 30

Size

Total Lines 65
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 11
eloc 40
c 1
b 0
f 1
nc 30
nop 2
dl 0
loc 65
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace Kaliop\eZWorkflowEngineBundle\Command;
4
5
use Kaliop\eZMigrationBundle\API\Value\Migration;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Input\InputArgument;
10
11
/**
12
 * Command to resume suspended workflows.
13
 *
14
 * @todo add support for resuming a set based on path
15
 * @todo add support for the separate-process cli switch
16
 */
17
class ResumeCommand extends AbstractCommand
18
{
19
    /**
20
     * Set up the command.
21
     *
22
     * Define the name, options and help text.
23
     */
24
    protected function configure()
25
    {
26
        parent::configure();
27
28
        $this
29
            ->setName('kaliop:workflows:resume')
30
            ->setDescription('Restarts any suspended workflows.')
31
            ->addOption('no-interaction', 'n', InputOption::VALUE_NONE, "Do not ask any interactive question.")
32
            ->addOption('no-transactions', 'u', InputOption::VALUE_NONE, "Do not use a repository transaction to wrap each workflow. Unsafe, but needed for legacy slot handlers")
33
            ->addOption('workflow', 'w', InputOption::VALUE_REQUIRED, 'A single workflow to resume (workflow name).', null)
34
            ->setHelp(<<<EOT
35
The <info>kaliop:workflows:resume</info> command allows you to resume any suspended workflow
36
EOT
37
            );
38
    }
39
40
    /**
41
     * Execute the command.
42
     *
43
     * @param InputInterface $input
44
     * @param OutputInterface $output
45
     * @return null|int null or 0 if everything went fine, or an error code
46
     * @throws \Exception
47
     */
48
    protected function execute(InputInterface $input, OutputInterface $output)
49
    {
50
        $start = microtime(true);
51
52
        $this->getContainer()->get('ez_migration_bundle.step_executed_listener.tracing')->setOutput($output);
53
54
        $workflowService = $this->getWorkflowService();
55
56
        $workflowName = $input->getOption('workflow');
57
        if ($workflowName != null) {
58
            $suspendedWorkflow = $workflowService->getWorkflow($workflowName);
59
            if (!$suspendedWorkflow) {
60
                throw new \Exception("Workflow '$workflowName' not found");
61
            }
62
            if ($suspendedWorkflow->status != Migration::STATUS_SUSPENDED) {
63
                throw new \Exception("Workflow '$workflowName' is not suspended, can not resume it");
64
            }
65
66
            $suspendedWorkflows = array($suspendedWorkflow);
67
        } else {
68
            $suspendedWorkflows = $workflowService->getWorkflowsByStatus(Migration::STATUS_SUSPENDED);
69
        };
70
71
        $output->writeln('<info>Found ' . count($suspendedWorkflows) . ' suspended workflows</info>');
72
73
        if (!count($suspendedWorkflows)) {
74
            $output->writeln('Nothing to do');
75
            return;
76
        }
77
78
        // ask user for confirmation to make changes
79
        if ($input->isInteractive() && !$input->getOption('no-interaction')) {
80
            $dialog = $this->getHelperSet()->get('dialog');
81
            if (!$dialog->askConfirmation(
0 ignored issues
show
Bug introduced by
The method askConfirmation() does not exist on Symfony\Component\Console\Helper\Helper. It seems like you code against a sub-type of Symfony\Component\Console\Helper\Helper such as Symfony\Component\Console\Helper\DialogHelper. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
            if (!$dialog->/** @scrutinizer ignore-call */ askConfirmation(
Loading history...
82
                $output,
83
                '<question>Careful, the database will be modified. Do you want to continue Y/N ?</question>',
84
                false
85
            )
86
            ) {
87
                $output->writeln('<error>Workflow resuming cancelled!</error>');
88
                return 0;
89
            }
90
        }
91
92
        $executed = 0;
93
        $failed = 0;
94
95
        foreach($suspendedWorkflows as $suspendedWorkflow) {
96
            $output->writeln("<info>Resuming {$suspendedWorkflow->name}</info>");
97
98
            try {
99
                $workflowService->resumeWorkflow($suspendedWorkflow, !$input->getOption('no-transactions'));
100
                $executed++;
101
            } catch (\Exception $e) {
102
                $output->writeln("\n<error>Workflow failed! Reason: " . $e->getMessage() . "</error>\n");
103
                $failed++;
104
            }
105
        }
106
107
        $time = microtime(true) - $start;
108
        $output->writeln("Resumed $executed workflows, failed $failed");
109
        $output->writeln("Time taken: ".sprintf('%.2f', $time)." secs, memory: ".sprintf('%.2f', (memory_get_peak_usage(true) / 1000000)). ' MB');
110
111
        if ($failed) {
112
            return 2;
113
        }
114
    }
115
}
116