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

MakeConfigRoutesCommand::execute()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 3
eloc 15
nc 3
nop 2
1
<?php
2
/**
3
 * netz98 magento module
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject of netz98.
8
 * You may be not allowed to change the sources
9
 * without authorization of netz98 new media GmbH.
10
 *
11
 * @copyright  Copyright (c) 1999-2016 netz98 new media GmbH (http://www.netz98.de)
12
 * @author netz98 new media GmbH <[email protected]>
13
 * @category N98
14
 * @package N98\Magento\Command\Developer\Console
15
 */
16
17
namespace N98\Magento\Command\Developer\Console\Config;
18
19
use N98\Magento\Command\Developer\Console\Util\Xml;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\OutputInterface;
24
25
class MakeConfigRoutesCommand extends AbstractSimpleConfigFileGeneratorCommand
26
{
27
    const CONFIG_FILENAME = 'routes.xml';
28
29
    protected function configure()
30
    {
31
        $this
32
            ->setName('make:config:routes')
33
            ->addArgument('area', InputArgument::OPTIONAL, 'Area of routes.xml file', 'frontend')
34
            ->addOption('type', 't', InputOption::VALUE_OPTIONAL, 'Type', 'standard')
35
            ->addOption('frontname', 'f', InputOption::VALUE_OPTIONAL, 'Frontname')
36
            ->setDescription('Creates a new routes.xml file')
37
        ;
38
    }
39
40
    /**
41
     * @param InputInterface  $input
42
     * @param OutputInterface $output
43
     *
44
     * @return int|void
45
     */
46
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48
        $selectedArea = $input->getArgument('area');
49
        $relativeConfigFilePath = $this->getRelativeConfigFilePath(self::CONFIG_FILENAME, $selectedArea);
50
51
        if ($this->getCurrentModuleDirectoryReader()->isExist($relativeConfigFilePath)) {
52
            $output->writeln('<warning>File already exists. Skiped generation</warning>');
53
54
            return;
55
        }
56
57
        $referenceConfigFileContent = file_get_contents(__DIR__ . '/_files/reference_routes.xml');
58
59
        if ($input->getOption('frontname') !== null) {
60
            // add route
61
            $referenceConfigFileContent = $this->addRoute(
62
                $referenceConfigFileContent,
63
                $input->getOption('type'),
64
                $input->getOption('frontname')
65
            );
66
        }
67
68
        $referenceConfigFileContent = Xml::formatString($referenceConfigFileContent);
0 ignored issues
show
Security Bug introduced by
It seems like $referenceConfigFileContent can also be of type false; however, N98\Magento\Command\Deve...til\Xml::formatString() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
69
        $this->getCurrentModuleDirectoryWriter()->writeFile($relativeConfigFilePath, $referenceConfigFileContent);
70
71
        $output->writeln('<info>generated </info><comment>' . $relativeConfigFilePath . '</comment>');
72
    }
73
74
    /**
75
     * @param string $xml
76
     * @param string $type
77
     * @param string $frontname
78
     *
79
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
80
     */
81
    private function addRoute($xml, $type, $frontname)
82
    {
83
        /*<router id="standard">
84
            <route id="vendorExample" frontName="example">
85
                <module name="Vendor_Example" />
86
            </route>
87
        </router>*/
88
89
        $xmlObj = simplexml_load_string($xml);
90
91
        $routeId = $this->getCurrentModuleId();
92
        $moduleName = $this->getCurrentModuleName();
93
94
        $xmlObj = Xml::addSimpleXmlNodesByXPath(
95
            $xmlObj,
96
            "router[@id=$type]/route[@id=$routeId,@frontName=$frontname]/module[@name=$moduleName]"
97
        );
98
99
        return $xmlObj->asXML();
100
    }
101
}
102