Completed
Push — master ( 0c5b7b...2c9e38 )
by Tom
13:30
created

MakeControllerCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 6
Bugs 0 Features 5
Metric Value
wmc 9
c 6
b 0
f 5
lcom 1
cbo 2
dl 0
loc 110
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
B execute() 0 31 3
B createClassBody() 0 24 3
A createClassMethodDefinitions() 0 23 1
A createLayoutFile() 0 3 1
1
<?php
2
3
namespace N98\Magento\Command\Developer\Console;
4
5
use Magento\Framework\Code\Generator\ClassGenerator;
6
use Magento\Framework\Controller\ResultFactory;
7
use Magento\Framework\Module\Dir;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
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
     * @param InputInterface  $input
26
     * @param OutputInterface $output
27
     *
28
     * @return int|void
29
     */
30
    protected function execute(InputInterface $input, OutputInterface $output)
31
    {
32
        try {
33
            $actionFileName = $this->getNormalizedPathByArgument($input->getArgument('classpath'));
34
            $classNameToGenerate = $this->getCurrentModuleNamespace()
35
                . '\\Controller\\'
36
                . $this->getNormalizedClassnameByArgument($input->getArgument('classpath'));
37
            $filePathToGenerate = 'Controller/' . $actionFileName . '.php';
38
39
            $classGenerator = $this->create(ClassGenerator::class);
40
41
            /** @var $classGenerator ClassGenerator */
42
            $classGenerator->setExtendedClass('Action');
43
44
            $body = $this->createClassBody($input);
45
            $executeMethodDefinition = $this->createClassMethodDefinitions($body);
46
47
            $classGenerator->addMethods([$executeMethodDefinition]);
48
            $classGenerator->setName($classNameToGenerate);
49
            $classGenerator->addUse('Magento\Framework\App\Action\Action');
50
            $classGenerator->addUse('Magento\Framework\Controller\ResultFactory');
51
52
            $this->writeClassToFile($output, $classGenerator, $filePathToGenerate);
53
54
            if ($input->getOption('result') == ResultFactory::TYPE_PAGE) {
55
                $this->createLayoutFile();
56
            }
57
        } 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...
58
            $output->writeln('<error>' . $e->getMessage() . '</error>');
59
        }
60
    }
61
62
    /**
63
     * @param InputInterface $input
64
     * @return string
65
     */
66
    private function createClassBody(InputInterface $input)
67
    {
68
        $body = '';
69
70
        if ($input->getOption('result') == ResultFactory::TYPE_PAGE) {
71
            $body .= 'return $this->resultFactory->create(ResultFactory::TYPE_PAGE);';
72
        } elseif ($input->getOption('result') == ResultFactory::TYPE_RAW) {
73
            $body .= '$result = $this->resultFactory->create(ResultFactory::TYPE_RAW);';
74
            $body .= PHP_EOL;
75
            $body .= '$result->setContents(\'ok\');';
76
            $body .= PHP_EOL;
77
            $body .= PHP_EOL;
78
            $body .= 'return $result;';
79
        } else {
80
            $body .= '$result = $this->resultFactory->create(ResultFactory::TYPE_JSON);';
81
            $body .= PHP_EOL;
82
            $body .= '$result->setData(\'ok\');';
83
            $body .= PHP_EOL;
84
            $body .= PHP_EOL;
85
            $body .= 'return $result;';
86
        }
87
88
        return $body;
89
    }
90
91
    /**
92
     * @param $body
93
     * @return array
94
     */
95
    private function createClassMethodDefinitions($body)
96
    {
97
        $executeMethodDefinition = [
98
            'name' => 'execute',
99
            'parameters' => [],
100
            'body' => $body,
101
            'docblock' => [
102
                'shortDescription' => 'Dispatch request',
103
                'tags' => [
104
                    [
105
                        'name' => 'return',
106
                        'description' => '\Magento\Framework\Controller\ResultInterface|ResponseInterface',
107
                    ],
108
                    [
109
                        'name' => 'throws',
110
                        'description' => '\Magento\Framework\Exception\NotFoundException',
111
                    ],
112
                ],
113
            ],
114
        ];
115
116
        return $executeMethodDefinition;
117
    }
118
119
    protected function createLayoutFile()
120
    {
121
    }
122
}
123