CreateBlockCommand::getScopeNamespace()   A
last analyzed

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\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 Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class CreateBlockCommand extends BaseCommand
14
{
15
    protected $jobs = [
16
        IsModuleExists::class => null,
17
        CopyFile::class => null,
18
        ReplaceText::class => null
19
    ];
20
21
    protected function configure()
22
    {
23
        $this->setName('create:block')
24
            ->setDescription('Creates a new block')
25
            ->addArgument(
26
                'namespace',
27
                InputArgument::REQUIRED,
28
                'What is the namespace on the module'
29
            )
30
            ->addArgument(
31
                'blockname',
32
                InputArgument::REQUIRED,
33
                'block\'s class name'
34
            )
35
            ->addOption(
36
                'admin',
37
                null,
38
                InputArgument::OPTIONAL,
39
                'Is this block for admin part?'
40
            )
41
            ->setHelp('creates a new block in a given namespace with a given name');
42
    }
43
44
    protected function execute(InputInterface $input, OutputInterface $output)
45
    {
46
        $this->setJobs();
47
48
        try {
49
            $this->processBlockFile($input, $output);
50
        } catch (\Throwable $e) {
51
            $output->writeln('<error>' . $e->getMessage() . '</error>');
52
        }
53
    }
54
55
    public function processBlockFile(InputInterface $input, OutputInterface $output)
56
    {
57
        $namespace = explode('_', $input->getArgument('namespace'));
58
        $name = $input->getArgument('blockname');
59
60
        $this->jobs[IsModuleExists::class]->handle($input->getArgument('namespace'), $output);
61
        $this->jobs[CopyFile::class]->handle($this->appContainer->get('resource_dir') . '/classes/Block.tphp', $this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Block/' . $this->getScope($input) . ucwords($name) . '.php');
62
        $this->replaceTextsSequence(['{{namespace}}' => $namespace[0] . '\\' . $namespace[1] . '\\Block' . $this->getScopeNamespace($input), '{{blockName}}' => ucwords($name)], $this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Block/');
63
64
        $output->writeln('<info>' . $namespace[0] . '\\' . $namespace[1] . '\\Block\\' . ucwords($name) . ' was successfully created</info>');
65
    }
66
67
    /**
68
     * @param InputInterface $input
69
     * @return string
70
     */
71
    protected function getScope(InputInterface $input): string
72
    {
73
        return $input->getOption('admin') ? 'Adminhtml/' : '';
74
    }
75
76
    /**
77
     * @param InputInterface $input
78
     * @return string
79
     */
80
    protected function getScopeNamespace(InputInterface $input): string
81
    {
82
        return $input->getOption('admin') ? '\\Adminhtml' : '';
83
    }
84
}
85