AbstractCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 74
rs 10
c 1
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A interact() 0 5 1
A initialize() 0 9 1
A configure() 0 10 1
A getSynopsis() 0 3 1
A execute() 0 8 1
A formatHelp() 0 14 2
1
<?php declare(strict_types=1);
2
3
namespace Cocotte\Console;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9
10
abstract class AbstractCommand extends Command implements CommandInterface
11
{
12
    public function getSynopsis($short = false)
13
    {
14
        return "docker run -it --rm chrif/cocotte ".parent::getSynopsis($short);
15
    }
16
17
    protected function formatHelp(
18
        string $description,
19
        string $exampleWithRequiredOptions,
20
        string $interactiveExample = null
21
    ): string {
22
        $help = /** @lang text */
23
            $description."\n\n<info>Example with required options:</info>\n```\n$ {$exampleWithRequiredOptions}\n```";
24
25
        if ($interactiveExample) {
26
            $help .= /** @lang text */
27
                "\n<info>Or run interactively:</info>\n```\n$ {$interactiveExample}\n```";
28
        }
29
30
        return $help;
31
    }
32
33
    /**
34
     * @return EventDispatcherInterface
35
     */
36
    abstract protected function eventDispatcher(): EventDispatcherInterface;
37
38
    final protected function configure()
39
    {
40
        $this->eventDispatcher()->dispatch(
41
            CommandEventStore::COMMAND_CONFIGURE,
42
            new CommandConfigureEvent($this, $this->getDefinition())
43
        );
44
        $this->doConfigure();
45
        $this->eventDispatcher()->dispatch(
46
            CommandEventStore::COMMAND_CONFIGURED,
47
            new CommandConfiguredEvent($this)
48
        );
49
    }
50
51
    abstract protected function doConfigure(): void;
52
53
    final protected function initialize(InputInterface $input, OutputInterface $output)
54
    {
55
        $this->eventDispatcher()->dispatch(
56
            CommandEventStore::COMMAND_BEFORE_INITIALIZE,
57
            new CommandBeforeInitializeEvent($input)
58
        );
59
        $this->eventDispatcher()->dispatch(
60
            CommandEventStore::COMMAND_INITIALIZE,
61
            new CommandInitializeEvent($this, $input)
62
        );
63
    }
64
65
    final protected function interact(InputInterface $input, OutputInterface $output)
66
    {
67
        $this->eventDispatcher()->dispatch(
68
            CommandEventStore::COMMAND_INTERACT,
69
            new CommandInteractEvent($this, $input)
70
        );
71
    }
72
73
    final protected function execute(InputInterface $input, OutputInterface $output)
74
    {
75
        $this->eventDispatcher()->dispatch(
76
            CommandEventStore::COMMAND_EXECUTE,
77
            new CommandExecuteEvent($this, $input)
78
        );
79
80
        return $this->doExecute($input, $output);
81
    }
82
83
    abstract protected function doExecute(InputInterface $input, OutputInterface $output);
84
85
}
86