Completed
Push — master ( 36bc47...ddbb70 )
by Dmitrij
01:51
created

CreateObserverCommand::execute()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 4
nop 2
1
<?php
2
3
namespace HotRodCli\Commands\Classes;
4
5
use HotRodCli\Commands\BaseCommand;
6
use HotRodCli\Jobs\Filesystem\CopyFile;
7
use HotRodCli\Jobs\Module\IsModuleExists;
8
use HotRodCli\Jobs\Module\ReplaceText;
9
use HotRodCli\Jobs\Xml\AddEvent;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class CreateObserverCommand extends BaseCommand
15
{
16
    protected $jobs = [
17
        IsModuleExists::class => null,
18
        CopyFile::class => null,
19
        ReplaceText::class => null,
20
        AddEvent::class => null
21
    ];
22
23
    protected function configure()
24
    {
25
        $this->setName('create:observer')
26
            ->setDescription('Creates an observer')
27
            ->addArgument('namespace', InputArgument::REQUIRED, 'What is the namespace of the module')
28
            ->addArgument('event', InputArgument::REQUIRED, 'What is the event name')
29
            ->addArgument('observer', InputArgument::REQUIRED, 'What is the observer name')
30
            ->setHelp('creates an install schema script in a given namespace');
31
    }
32
33
    protected function execute(InputInterface $input, OutputInterface $output)
34
    {
35
        $this->setJobs();
36
37
        try {
38
            $this->jobs[IsModuleExists::class]->handle($input->getArgument('namespace'), $output);
39
40
            $this->processObserverFile($input, $output);
41
42
            $this->processEventsFile($input, $output);
43
        } catch (\Throwable $e) {
44
            $output->writeln('<error>' . $e->getMessage() . '</error>');
45
        }
46
    }
47
48
    protected function processObserverFile(InputInterface $input, OutputInterface $output)
49
    {
50
        $jobs = $this->jobs;
51
        $namespace = explode('_', $input->getArgument('namespace'));
52
        $app = $this->appContainer;
53
        $jobs[CopyFile::class]->handle(
54
            $app->get('resource_dir') . '/classes/Observer.tphp',
55
            $this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] .
56
            '/Observer/' . ucwords($input->getArgument('observer')) . '.php'
57
        );
58
59
        $jobs[ReplaceText::class]->handle(
60
            '{{namespace}}',
61
            $namespace[0] . '\\' . $namespace[1]  . '\\',
62
            $this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Observer/'
63
        );
64
65
        $jobs[ReplaceText::class]->handle(
66
            '{{observer-name}}',
67
            ucwords($input->getArgument('observer')),
68
            $this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Observer/'
69
        );
70
71
        $output->writeln('<info>' . $namespace[0] . '\\' . $namespace[1] . '\\Observer\\' . ucwords(ucwords($input->getArgument('observer'))) . ' was successfully created</info>');
72
    }
73
74
    protected function processEventsFile(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Unused Code introduced by
The parameter $output is not used and could be removed. ( Ignorable by Annotation )

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

74
    protected function processEventsFile(InputInterface $input, /** @scrutinizer ignore-unused */ OutputInterface $output)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76
        $jobs = $this->jobs;
77
        $namespace = explode('_', $input->getArgument('namespace'));
78
        $app = $this->appContainer;
79
        $jobs[CopyFile::class]->handle(
80
            $app->get('resource_dir') . '/xml/events.xml',
81
            $this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/etc/events.xml'
82
        );
83
84
        $jobs[AddEvent::class]->handle($this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/etc/events.xml', [
85
            'event-name' => $input->getArgument('event'),
86
            'observer-name' => strtolower($namespace[0] . '_' . $namespace[1] . '_' . $input->getArgument('observer')),
87
            'instance' => $namespace[0] . '\\' . $namespace[1] . '\\Observer\\' . $input->getArgument('observer'),
88
        ]);
89
    }
90
}
91