Passed
Push — master ( 1307b7...d93a6d )
by Dmitrij
02:42
created

CreateLayoutCommand::getScope()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
eloc 1
nc 2
nop 1
1
<?php
2
3
namespace HotRodCli\Commands\Xml;
4
5
use HotRodCli\Commands\BaseCommand;
6
use HotRodCli\Jobs\Filesystem\CopyFile;
7
use HotRodCli\Jobs\Module\ReplaceText;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class CreateLayoutCommand extends BaseCommand
13
{
14
    protected $jobs = [
15
        CopyFile::class => null,
16
        ReplaceText::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
                'name' => 'layout-name',
28
                'mode' => InputArgument::REQUIRED,
29
                'description' => 'name of the layout'
30
            ],
31
            [
32
                'name' => 'layout-file-name',
33
                'mode' => InputArgument::REQUIRED,
34
                'description' => 'name of the layout\'s file'
35
            ],
36
            [
37
                'name' => 'block-class',
38
                'mode' => InputArgument::REQUIRED,
39
                'description' => 'full class name of the block'
40
            ],
41
            [
42
                'name' => 'template',
43
                'mode' => InputArgument::REQUIRED,
44
                'description' => 'template file'
45
            ]
46
        ],
47
        'options' => [
48
            [
49
                'name' => 'admin',
50
                'shortcut' => null,
51
                'mode' => InputArgument::OPTIONAL,
52
                'description' => 'Is this layout for admin part?'
53
            ]
54
        ],
55
        'description' => 'Creates a new layout file',
56
        'name' => 'create:layout',
57
        'help' => 'creates a new controller in a given namespace with a given route'
58
    ];
59
60
    public function configure()
61
    {
62
        $this->config();
63
    }
64
65
    public function execute(InputInterface $input, OutputInterface $output)
66
    {
67
        $this->setJobs();
68
        $namespace = explode('_', $input->getArgument('namespace'));
69
70
        $this->jobs[CopyFile::class]->handle(
71
            $this->appContainer->get('resource_dir') . '/xml/layout.xml',
72
            $this->appContainer->get('app_dir')
73
            . '/app/code/' . $namespace[0] . '/' . $namespace[1]
74
            . '/view/' . $this->getScope($input) . '/layout/' . $input->getArgument('layout-file-name') . '.xml'
75
        );
76
77
        $this->replaceTextsSequence([
78
            '{{block_class}}' => $input->getArgument('block-class'),
79
            '{{block_name}}' => strtolower($input->getArgument('layout-name')),
80
            '{{block_template}}' => $namespace[0] . '_' . $namespace[1] . '::' . $input->getArgument('template') . '.phtml'
81
        ], $this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/view/');
82
83
        $output->writeln('<info>' . $namespace[0] . '/' . $namespace[1] . '/view/frontend/layout/'
84
            . strtolower($input->getArgument('layout-file-name')) . '.xml was successfully created</info>');
85
    }
86
87
    /**
88
     * @param InputInterface $input
89
     * @return string
90
     */
91
    protected function getScope(InputInterface $input): string
92
    {
93
        return $input->getOption('admin') ? 'adminhtml' : 'frontend';
94
    }
95
}
96