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

MakeControllerCommand::execute()   B

Complexity

Conditions 3
Paths 15

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 4
Metric Value
c 4
b 0
f 4
dl 0
loc 32
rs 8.8571
cc 3
eloc 20
nc 15
nop 2
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
13
class MakeControllerCommand extends AbstractGeneratorCommand
14
{
15
    protected function configure()
16
    {
17
        $this
18
            ->setName('make:controller')
19
            ->addArgument('classpath', InputArgument::REQUIRED)
20
            ->addOption('result', 'r', InputOption::VALUE_OPTIONAL, 'Result type', 'page')
21
            ->setDescription('Creates a controller action 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
            $actionFileName = $this->getNormalizedPathByArgument($input->getArgument('classpath'));
35
            $classNameToGenerate = $this->getCurrentModuleNamespace()
36
                . '\\Controller\\'
37
                . $this->getNormalizedClassnameByArgument($input->getArgument('classpath'));
38
            $filePathToGenerate = 'Controller/' . $actionFileName . '.php';
39
40
            $classGenerator = $this->create(ClassGenerator::class);
41
42
            /** @var $classGenerator ClassGenerator */
43
            $classGenerator->setExtendedClass('Action');
44
45
            $body = $this->createClassBody($input);
46
            $executeMethodDefinition = $this->createClassMethodDefinitions($body);
47
48
            $classGenerator->addMethods([$executeMethodDefinition]);
49
            $classGenerator->setName($classNameToGenerate);
50
            $classGenerator->addUse('Magento\Framework\App\Action\Action');
51
            $classGenerator->addUse('Magento\Framework\Controller\ResultFactory');
52
53
            $this->writeClassToFile($output, $classGenerator, $filePathToGenerate);
54
55
            if ($input->getOption('result') == ResultFactory::TYPE_PAGE) {
56
                $this->createLayoutFile();
57
            }
58
59
        } 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...
60
            $output->writeln('<error>' . $e->getMessage() . '</error>');
61
        }
62
    }
63
64
    /**
65
     * @param InputInterface $input
66
     * @return string
67
     */
68
    private function createClassBody(InputInterface $input)
69
    {
70
        $body = '';
71
72
        if ($input->getOption('result') == ResultFactory::TYPE_PAGE) {
73
            $body .= 'return $this->resultFactory->create(ResultFactory::TYPE_PAGE);';
74
        } elseif ($input->getOption('result') == ResultFactory::TYPE_RAW) {
75
            $body .= '$result = $this->resultFactory->create(ResultFactory::TYPE_RAW);';
76
            $body .= PHP_EOL;
77
            $body .= '$result->setContents(\'ok\');';
78
            $body .= PHP_EOL;
79
            $body .= PHP_EOL;
80
            $body .= 'return $result;';
81
        } else {
82
            $body .= '$result = $this->resultFactory->create(ResultFactory::TYPE_JSON);';
83
            $body .= PHP_EOL;
84
            $body .= '$result->setData(\'ok\');';
85
            $body .= PHP_EOL;
86
            $body .= PHP_EOL;
87
            $body .= 'return $result;';
88
        }
89
90
        return $body;
91
    }
92
93
    /**
94
     * @param $body
95
     * @return array
96
     */
97
    private function createClassMethodDefinitions($body)
98
    {
99
        $executeMethodDefinition = [
100
            'name' => 'execute',
101
            'parameters' => [],
102
            'body' => $body,
103
            'docblock' => [
104
                'shortDescription' => 'Dispatch request',
105
                'tags' => [
106
                    [
107
                        'name' => 'return',
108
                        'description' => '\Magento\Framework\Controller\ResultInterface|ResponseInterface',
109
                    ],
110
                    [
111
                        'name' => 'throws',
112
                        'description' => '\Magento\Framework\Exception\NotFoundException'
113
                    ]
114
                ],
115
            ],
116
        ];
117
118
        return $executeMethodDefinition;
119
    }
120
121
    protected function createLayoutFile()
122
    {
123
124
    }
125
126
}