CreateRequireJsCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 32
dl 0
loc 62
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getScope() 0 3 2
A configure() 0 3 1
A processRequireJsFile() 0 12 1
A execute() 0 9 2
1
<?php
2
3
namespace HotRodCli\Commands\Frontend;
4
5
use HotRodCli\Commands\BaseCommand;
6
use HotRodCli\Jobs\Filesystem\CopyFile;
7
use HotRodCli\Jobs\Module\IsModuleExists;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class CreateRequireJsCommand extends BaseCommand
13
{
14
    protected $jobs = [
15
        CopyFile::class => null,
16
        IsModuleExists::class => null
17
    ];
18
19
    protected $configs = [
20
        'arguments' => [
21
            [
22
                'name' => 'namespace',
23
                'mode' => InputArgument::REQUIRED,
24
                'description' => 'What is the namespace on the module'
25
            ]
26
        ],
27
        'options' => [
28
            [
29
                'name' => 'admin',
30
                'shortcut' => null,
31
                'mode' => InputArgument::OPTIONAL,
32
                'description' => 'Do You want to generate the requirejs-config.js file in admin scope?'
33
            ]
34
        ],
35
        'description' => 'Initiates a requirejs-config.js',
36
        'name' => 'create:requirejs-config',
37
        'help' => 'Initiates a new requirejs-config.js in a given namespace'
38
    ];
39
40
    protected function configure()
41
    {
42
        $this->config();
43
    }
44
45
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47
        $this->setJobs();
48
49
        try {
50
            $this->jobs[IsModuleExists::class]->handle($input->getArgument('namespace'), $output);
51
            $this->processRequireJsFile($input, $output);
52
        } catch (\Throwable $e) {
53
            $output->writeln('<error>' . $e->getMessage() . '</error>');
54
        }
55
    }
56
57
    protected function processRequireJsFile(InputInterface $input, OutputInterface $output)
58
    {
59
        $namespace = explode('_', $input->getArgument('namespace'));
60
        $scope = $this->getScope($input);
61
62
        $this->jobs[CopyFile::class]->handle(
63
            $this->appContainer->get('resource_dir') . '/frontend/requirejs-config.js',
64
            $this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] .
65
            '/view/' . $scope . '/requirejs-config.js'
66
        );
67
68
        $output->writeln('<info>The ' . $namespace[0] . '/' . $namespace[1] . '/' . 'view/' . $scope . '/requirejs-config.js was created</info>');
69
    }
70
71
    protected function getScope(InputInterface $input): string
72
    {
73
        return $input->getOption('admin') ? 'adminhtml' : 'frontend';
74
    }
75
}
76