Completed
Push — develop ( 73bd0a...ccb498 )
by Tom
05:04
created

MakeBlockCommand::createClassFile()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 27
rs 8.8571
cc 1
eloc 15
nc 1
nop 2
1
<?php
2
3
namespace N98\Magento\Command\Developer\Console;
4
5
use Magento\Framework\Code\Generator\ClassGenerator;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Zend\Code\Generator\FileGenerator;
10
11
class MakeBlockCommand extends AbstractGeneratorCommand
12
{
13
    protected function configure()
14
    {
15
        $this
16
            ->setName('make:block')
17
            ->addArgument('classpath', InputArgument::REQUIRED)
18
            ->setDescription('Creates a generic block class');
19
    }
20
21
    /**
22
     * @param InputInterface $input
23
     * @param OutputInterface $output
24
     *
25
     * @return int|void
26
     */
27
    protected function execute(InputInterface $input, OutputInterface $output)
28
    {
29
        try {
30
            $this->createClassFile($input, $output);
31
        } 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...
32
            $output->writeln('<error>' . $e->getMessage() . '</error>');
33
        }
34
    }
35
36
    /**
37
     * @param InputInterface $input
38
     * @param OutputInterface $output
39
     */
40
    private function createClassFile(InputInterface $input, OutputInterface $output)
41
    {
42
        $blockFileName = $this->getNormalizedPathByArgument($input->getArgument('classpath'));
43
        $classNameToGenerate = $this->getCurrentModuleNamespace()
44
            . '\\Block\\'
45
            . $this->getNormalizedClassnameByArgument($input->getArgument('classpath'));
46
        $filePathToGenerate = 'Block/' . $blockFileName . '.php';
47
48
        $classGenerator = $this->create(ClassGenerator::class);
49
50
        /** @var $classGenerator ClassGenerator */
51
        $classGenerator->setExtendedClass('Template');
52
        $classGenerator->addUse('Magento\Framework\View\Element\Template');
53
54
        $classGenerator->setName($classNameToGenerate);
55
56
        $fileGenerator = FileGenerator::fromArray(
57
            [
58
                'classes' => [$classGenerator],
59
            ]
60
        );
61
62
        $directoryWriter = $this->getCurrentModuleDirectoryWriter();
63
        $directoryWriter->writeFile($filePathToGenerate, $fileGenerator->generate());
64
65
        $output->writeln('<info>generated </info><comment>' . $filePathToGenerate . '</comment>');
66
    }
67
}
68