CreateScriptCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 60
dl 0
loc 108
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 3 1
A execute() 0 13 2
A processRequirejs() 0 9 3
A processTemplate() 0 6 2
A processScriptFile() 0 5 1
1
<?php
2
3
namespace HotRodCli\Commands\Frontend;
4
5
use HotRodCli\Commands\BaseCommand;
6
use HotRodCli\Jobs\Filesystem\CopyFile;
7
use HotRodCli\Jobs\Js\AddJs;
8
use HotRodCli\Jobs\Js\AddMageInit;
9
use HotRodCli\Jobs\Module\IsModuleExists;
10
use HotRodCli\Processors\ProcessRequireJs;
11
use HotRodCli\Processors\ScriptFile\SimpleScript;
12
use HotRodCli\Processors\ScriptFile\WidgetScript;
13
use HotRodCli\Processors\ScriptTemplate\SimpleTemplate;
14
use HotRodCli\Processors\ScriptTemplate\WidgetTemplate;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Filesystem\Filesystem;
19
20
class CreateScriptCommand extends BaseCommand
21
{
22
    protected $jobs = [
23
        CopyFile::class => null,
24
        IsModuleExists::class => null,
25
        AddJs::class => null,
26
        AddMageInit::class => null
27
    ];
28
29
    protected $processors = [
30
        ProcessRequireJs::class => null
31
    ];
32
33
    protected $configs = [
34
        'arguments' => [
35
            [
36
                'name' => 'namespace',
37
                'mode' => InputArgument::REQUIRED,
38
                'description' => 'What is the namespace on the module'
39
            ],
40
            [
41
                'name' => 'script-name',
42
                'mode' => InputArgument::REQUIRED,
43
                'description' => 'What is the filename of the script'
44
            ]
45
        ],
46
        'options' => [
47
            [
48
                'name' => 'admin',
49
                'shortcut' => null,
50
                'mode' => InputArgument::OPTIONAL,
51
                'description' => 'Do You want to generate the JS in admin scope?'
52
            ],
53
            [
54
                'name' => 'template',
55
                'shortcut' => null,
56
                'mode' => InputArgument::OPTIONAL,
57
                'description' => 'Do You want to add mage-init for this script in a specific template?'
58
            ],
59
            [
60
                'name' => 'type',
61
                'shortcut' => null,
62
                'mode' => InputArgument::OPTIONAL,
63
                'description' => 'type of the script. Available types are: [simple], [widget]'
64
            ]
65
        ],
66
        'description' => 'Creates a new script',
67
        'name' => 'create:js-script',
68
        'help' => 'creates a new Java Script in a given namespace',
69
        'info' => ''
70
    ];
71
72
    protected $scriptFileProcessors = [
73
        'simple' => SimpleScript::class,
74
        'widget' => WidgetScript::class
75
    ];
76
77
    protected $scriptTemplateProcessors = [
78
        'simple' => SimpleTemplate::class,
79
        'widget' => WidgetTemplate::class
80
    ];
81
82
    protected function configure()
83
    {
84
        $this->config();
85
    }
86
87
    protected function execute(InputInterface $input, OutputInterface $output)
88
    {
89
        $this->setJobs();
90
        $this->setProcessors();
91
92
        try {
93
            $this->jobs[IsModuleExists::class]->handle($input->getArgument('namespace'), $output);
94
95
            $this->processRequirejs($input, $output);
96
            $this->processScriptFile($input, $output);
97
            $this->processTemplate($input, $output);
98
        } catch (\Throwable $e) {
99
            $output->writeln('<error>' . $e->getMessage() . '</error>');
100
        }
101
    }
102
103
    protected function processScriptFile(InputInterface $input, OutputInterface $output)
104
    {
105
        $type = $input->getOption('type') ?? 'simple';
106
107
        $this->appContainer->resolve($this->scriptFileProcessors[$type])($input, $output);
108
    }
109
110
    protected function processRequirejs(InputInterface $input, OutputInterface $output)
111
    {
112
        $namespace = explode('_', $input->getArgument('namespace'));
113
        $app = $this->appContainer;
114
        $filesystem = $app->resolve(Filesystem::class);
115
        $scope = $input->getOption('admin') ? 'adminhtml' : 'frontend';
116
117
        if (!$filesystem->exists($app->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/view/' . $scope . '/requirejs-config.js')) {
118
            $this->processors[ProcessRequireJs::class]($input, $output);
119
        }
120
    }
121
122
    protected function processTemplate(InputInterface $input, OutputInterface $output)
123
    {
124
        if ($input->getOption('template')) {
125
            $type = $input->getOption('type') ?? 'simple';
126
127
            $this->appContainer->resolve($this->scriptTemplateProcessors[$type])($input, $output);
128
        }
129
    }
130
}
131