Passed
Push — master ( 813977...bea400 )
by Dmitrij
01:59
created

CreateCollectionCommand::processCollectionFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
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 Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Input\InputArgument;
12
13
class CreateCollectionCommand extends BaseCommand
14
{
15
    protected $jobs = [
16
        CopyFile::class => null,
17
        ReplaceText::class => null,
18
        IsModuleExists::class => null
19
    ];
20
21
    protected function configure()
22
    {
23
        $this->setName('create:collection')
24
            ->setDescription('Creates a new collection')
25
            ->addArgument(
26
                'namespace',
27
                InputArgument::REQUIRED,
28
                'What is the namespace on the module'
29
            )
30
            ->addArgument(
31
                'name',
32
                InputArgument::REQUIRED,
33
                'What is the name of the model'
34
            )
35
            ->addArgument(
36
                'table-name',
37
                InputArgument::REQUIRED,
38
                'What is the table name for the model'
39
            )
40
            ->addArgument(
41
                'id-field',
42
                InputArgument::REQUIRED,
43
                'What is the id-field of the table'
44
            )
45
            ->setHelp('creates a new Collection in a given namespace');
46
    }
47
48
    protected function execute(InputInterface $input, OutputInterface $output)
49
    {
50
        $this->setJobs();
51
52
        try {
53
            $this->jobs[IsModuleExists::class]->handle($input->getArgument('namespace'), $output);
54
55
            $this->processCollectionFile($input, $output);
56
        } catch (\Throwable $e) {
57
            $output->writeln('<error>' . $e->getMessage() . '</error>');
58
        }
59
    }
60
61
    protected function processCollectionFile(InputInterface $input, OutputInterface $output)
62
    {
63
        $namespace = explode('_', $input->getArgument('namespace'));
64
65
        $this->jobs[CopyFile::class]->handle(
66
            $this->appContainer->get('resource_dir') . '/classes/Collection.tphp',
67
            $this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Model/ResourceModel/' . $input->getArgument('name') . '/Collection.php'
68
        );
69
70
        $this->replaceTextsSequence([
71
            '{{namespace}}' => str_replace('_', '\\', $input->getArgument('namespace')),
72
            '{{name}}' => $input->getArgument('name'),
73
            '{{table_name}}' => $input->getArgument('table-name'),
74
            '{{id_name}}' => $input->getArgument('id-field'),
75
            '{{ResourceModel}}' => str_replace('_', '\\', $input->getArgument('namespace')) . '\\Model\\ResourceModel\\' . $input->getArgument('name'),
76
            '{{Model}}' => str_replace('_', '\\', $input->getArgument('namespace')) . '\\Model\\' . $input->getArgument('name')
77
        ], $this->appContainer->get('app_dir') . '/app/code/' . $namespace[0] . '/' . $namespace[1] . '/Model/');
78
79
        $output->writeln('<info>Collection ' . $input->getArgument('name') . ' was successfully created</info>');
80
    }
81
}
82