WorkflowDumpCommand::execute()   B
last analyzed

Complexity

Conditions 11
Paths 51

Size

Total Lines 52
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
eloc 32
nc 51
nop 2
dl 0
loc 52
ccs 0
cts 29
cp 0
crap 132
rs 7.3166
c 0
b 0
f 0

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 declare(strict_types=1);
2
3
/*
4
 * This file is part of Biurad opensource projects.
5
 *
6
 * @copyright 2019 Biurad Group (https://biurad.com/)
7
 * @license   https://opensource.org/licenses/BSD-3-Clause License
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Flange\Commands\Symfony;
14
15
use Symfony\Component\Console\Attribute\AsCommand;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Completion\CompletionInput;
18
use Symfony\Component\Console\Completion\CompletionSuggestions;
19
use Symfony\Component\Console\Exception\InvalidArgumentException;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\OutputInterface;
24
use Symfony\Component\Workflow\Definition;
25
use Symfony\Component\Workflow\Dumper\GraphvizDumper;
26
use Symfony\Component\Workflow\Dumper\MermaidDumper;
27
use Symfony\Component\Workflow\Dumper\PlantUmlDumper;
28
use Symfony\Component\Workflow\Dumper\StateMachineGraphvizDumper;
29
use Symfony\Component\Workflow\Marking;
30
31
/**
32
 * @author Grégoire Pineau <[email protected]>
33
 *
34
 * @final
35
 */
36
#[AsCommand('workflow:dump', 'Dumps a workflow definition')]
37
class WorkflowDumpCommand extends Command
38
{
39
    protected static $defaultName = 'workflow:dump';
40
    protected static $defaultDescription = 'Dumps a workflow definition';
41
42
    private const DUMP_FORMAT_OPTIONS = ['puml', 'mermaid', 'dot'];
43
44
    /**
45
     * @param array<string,Definition> $workflows
46
     */
47
    public function __construct(private array $workflows = [])
48
    {
49
        parent::__construct();
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function configure(): void
56
    {
57
        $this
58
            ->setDefinition([
59
                new InputArgument('name', InputArgument::REQUIRED, 'A workflow name'),
60
                new InputArgument('marking', InputArgument::IS_ARRAY, 'A marking (a list of places)'),
61
                new InputOption('label', 'l', InputOption::VALUE_REQUIRED, 'Label a graph'),
62
                new InputOption('dump-format', null, InputOption::VALUE_REQUIRED, 'The dump format ['.\implode('|', self::DUMP_FORMAT_OPTIONS).']', 'dot'),
63
            ])
64
            ->setHelp(
65
                <<<'EOF'
66
The <info>%command.name%</info> command dumps the graphical representation of a
67
workflow in different formats
68
69
<info>DOT</info>:  %command.full_name% <workflow name> | dot -Tpng > workflow.png
70
<info>PUML</info>: %command.full_name% <workflow name> --dump-format=puml | java -jar plantuml.jar -p > workflow.png
71
<info>MERMAID</info>: %command.full_name% <workflow name> --dump-format=mermaid | mmdc -o workflow.svg
72
EOF
73
            )
74
        ;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    protected function execute(InputInterface $input, OutputInterface $output): int
81
    {
82
        $workflowName = $input->getArgument('name');
83
84
        $workflow = null;
85
86
        if (isset($this->workflows['workflow.'.$workflowName])) {
87
            $workflow = $this->workflows['workflow.'.$workflowName];
88
            $type = 'workflow';
89
        } elseif (isset($this->workflows['state_machine.'.$workflowName])) {
90
            $workflow = $this->workflows['state_machine.'.$workflowName];
91
            $type = 'state_machine';
92
        }
93
94
        if (null === $workflow) {
95
            throw new InvalidArgumentException(\sprintf('No service found for "workflow.%1$s" nor "state_machine.%1$s".', $workflowName));
96
        }
97
98
        switch ($input->getOption('dump-format')) {
99
            case 'puml':
100
                $transitionType = 'workflow' === $type ? PlantUmlDumper::WORKFLOW_TRANSITION : PlantUmlDumper::STATEMACHINE_TRANSITION;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $type does not seem to be defined for all execution paths leading up to this point.
Loading history...
101
                $dumper = new PlantUmlDumper($transitionType);
102
103
                break;
104
105
            case 'mermaid':
106
                $transitionType = 'workflow' === $type ? MermaidDumper::TRANSITION_TYPE_WORKFLOW : MermaidDumper::TRANSITION_TYPE_STATEMACHINE;
107
                $dumper = new MermaidDumper($transitionType);
108
109
                break;
110
111
            case 'dot':
112
            default:
113
                $dumper = ('workflow' === $type) ? new GraphvizDumper() : new StateMachineGraphvizDumper();
114
        }
115
116
        $marking = new Marking();
117
118
        foreach ($input->getArgument('marking') as $place) {
119
            $marking->mark($place);
120
        }
121
122
        $options = [
123
            'name' => $workflowName,
124
            'nofooter' => true,
125
            'graph' => [
126
                'label' => $input->getOption('label'),
127
            ],
128
        ];
129
        $output->writeln($dumper->dump($workflow, $marking, $options));
130
131
        return self::SUCCESS;
132
    }
133
134
    public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
135
    {
136
        if ($input->mustSuggestArgumentValuesFor('name')) {
137
            $suggestions->suggestValues(\array_keys($this->workflows));
138
        }
139
140
        if ($input->mustSuggestOptionValuesFor('dump-format')) {
141
            $suggestions->suggestValues(self::DUMP_FORMAT_OPTIONS);
142
        }
143
    }
144
}
145