Completed
Push — feature/merge-build-stop-step ( d59daf )
by Tom
8s
created

MakeBlockCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 4
c 2
b 0
f 2
lcom 1
cbo 2
dl 0
loc 59
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
A execute() 0 8 2
B createClassFile() 0 27 1
1
<?php
2
3
namespace N98\Magento\Command\Developer\Console;
4
5
use Magento\Framework\Controller\ResultFactory;
6
use Magento\Framework\Module\Dir;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Magento\Framework\Code\Generator\ClassGenerator;
12
use Zend\Code\Generator\FileGenerator;
13
14
class MakeBlockCommand extends AbstractGeneratorCommand
15
{
16
    protected function configure()
17
    {
18
        $this
19
            ->setName('make:block')
20
            ->addArgument('classpath', InputArgument::REQUIRED)
21
            ->setDescription('Creates a generic block class')
22
        ;
23
    }
24
25
    /**
26
     * @param InputInterface  $input
27
     * @param OutputInterface $output
28
     *
29
     * @return int|void
30
     */
31
    protected function execute(InputInterface $input, OutputInterface $output)
32
    {
33
        try {
34
            $this->createClassFile($input, $output);
35
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class N98\Magento\Command\Developer\Console\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
36
            $output->writeln('<error>' . $e->getMessage() . '</error>');
37
        }
38
    }
39
40
    /**
41
     * @param InputInterface $input
42
     * @param OutputInterface $output
43
     */
44
    private function createClassFile(InputInterface $input, OutputInterface $output)
45
    {
46
        $blockFileName = $this->getNormalizedPathByArgument($input->getArgument('classpath'));
47
        $classNameToGenerate = $this->getCurrentModuleNamespace()
48
            . '\\Block\\'
49
            . $this->getNormalizedClassnameByArgument($input->getArgument('classpath'));
50
        $filePathToGenerate = 'Block/' . $blockFileName . '.php';
51
52
        $classGenerator = $this->create(ClassGenerator::class);
53
54
        /** @var $classGenerator ClassGenerator */
55
        $classGenerator->setExtendedClass('Template');
56
        $classGenerator->addUse('Magento\Framework\View\Element\Template');
57
58
        $classGenerator->setName($classNameToGenerate);
59
60
        $fileGenerator = FileGenerator::fromArray(
61
            [
62
                'classes' => [$classGenerator]
63
            ]
64
        );
65
66
        $directoryWriter = $this->getCurrentModuleDirectoryWriter();
67
        $directoryWriter->writeFile($filePathToGenerate, $fileGenerator->generate());
68
69
        $output->writeln('<info>generated </info><comment>' . $filePathToGenerate . '</comment>');
70
    }
71
72
}
73