MakeEventListener   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 99
c 1
b 0
f 0
dl 0
loc 138
ccs 0
cts 38
cp 0
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 37 2
A configure() 0 8 1
A getEventName() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\DevTools\Application\Command;
6
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Question\Question;
11
use Throwable;
12
13
class MakeEventListener extends AbstractMakerCommand
14
{
15
    public const NAME = 'make:event-listener';
16
    protected const COMMAND_DESCRIPTION = 'Creates an event listener class.';
17
    protected const FQCN_ARGUMENT_DESCRIPTION = 'Add Full qualified class name for Event Listener.';
18
    protected const QUESTION =
19
        '<fg=blue>Please enter the name of the Event Listener class <info>[App\Listener\DoSomething]</info>: </>';
20
    protected const DEFAULT_RESPONSE = 'App\Listener\DoSomething';
21
    protected const TEMPLATE = '<?php
22
23
declare(strict_types=1);
24
25
namespace %s;
26
27
class %s
28
{
29
    public function __invoke(\\%s $event): void
30
    {
31
        // do something with the event
32
    }
33
}
34
';
35
    protected const SUCCESS_HELP_TEMPLATE = '<comment>
36
To activate the newly created Event Listener you must register it in the configuration. (This examples are valid for'
37
    . ' Antidot Framework)
38
39
PHP style config (Antidot Framework)
40
41
=====================================
42
43
<?php
44
// %1$s/some-file.prod.php
45
46
return [
47
    \'services\' => [
48
        \'%2$s\' => \'%2$s\'
49
    ],
50
    \'app-events\' => [
51
        \'event-listeners\' => [
52
            \'%3$s\' => [
53
                \'%2$s\',
54
            ]
55
        ]
56
    ]    
57
];
58
59
======================================
60
61
YAML style config (Antidot Framework)
62
63
======================================
64
65
# %1$s/some-file.prod.yaml
66
services:
67
  %2$s: %2$s
68
app-events:
69
  event-listeners:
70
    %3$s:
71
      - %2$s        
72
======================================
73
74
YAML style config (Antidot Framework Symfony style)
75
76
======================================
77
78
# %1$s/some-file.prod.yaml
79
services:
80
  %2$s:
81
  tags:
82
    - { name: \'event_listener\', event: \'%3$s\' }
83
          
84
======================================
85
</comment>';
86
87
    protected function configure(): void
88
    {
89
        parent::configure();
90
        $this
91
            ->addArgument(
92
                'event-name',
93
                InputArgument::OPTIONAL,
94
                'Add the event class name that listener is listening to'
95
            );
96
    }
97
98
    protected function execute(InputInterface $input, OutputInterface $output): ?int
99
    {
100
        /** @var string $fqcn */
101
        $fqcn = $this->getFQCN($input, $output);
102
        /** @var string $eventName */
103
        $eventName = $this->getEventName($input, $output);
104
        $getClassNameFromFQCN = $this->getClassNameFromFQCN;
105
        $getNamespaceFromFQCN = $this->getNamespaceFromFQCN;
106
        $getRealPathFromNamespace = $this->getRealPathFromNamespace;
107
        $createClassFile = $this->createClassFile;
108
        try {
109
            $className = $getClassNameFromFQCN($fqcn);
110
            $namespace = $getNamespaceFromFQCN($fqcn);
111
            $classDir = $getRealPathFromNamespace($namespace);
112
            $realFilePath = $createClassFile(
113
                $classDir,
114
                $className,
115
                sprintf(static::TEMPLATE, $namespace, $className, $eventName)
116
            );
117
        } catch (Throwable $exception) {
118
            $output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
119
            return 1;
120
        }
121
122
        $output->writeln(sprintf(
123
            '<info>Command %s successfully created in file %s</info>',
124
            $eventName,
125
            $realFilePath
126
        ));
127
        $output->writeln(sprintf(
128
            static::SUCCESS_HELP_TEMPLATE,
129
            $this->config['config_dir'],
130
            $fqcn,
131
            $eventName
132
        ));
133
134
        return 0;
135
    }
136
137
138
    protected function getEventName(InputInterface $input, OutputInterface $output): string
139
    {
140
        $eventName = $input->getArgument('event-name');
141
        if (null === $eventName) {
142
            $questionHelper = $this->getHelper('question');
143
            $question = new Question(
144
                '<fg=blue>Please enter the name of the evnt class <info>[App\My\EventName]</info>: </>',
145
                'App\My\EventName'
146
            );
147
            $eventName = $questionHelper->ask($input, $output, $question);
148
        }
149
150
        return $eventName;
151
    }
152
}
153