CreateLayoutCommand::execute()   B
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 57
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 34
c 2
b 0
f 0
nc 12
nop 2
dl 0
loc 57
ccs 38
cts 38
cp 1
crap 7
rs 8.4426

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Rougin\Combustor\Commands;
4
5
use Symfony\Component\Console\Input\InputOption;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
use Rougin\Combustor\Common\File;
11
use Rougin\Combustor\Common\Tools;
12
13
/**
14
 * Create Layout Command
15
 *
16
 * Creates a new header and footer file for CodeIgniter
17
 *
18
 * @package Combustor
19
 * @author  Rougin Gutib <[email protected]>
20
 */
21
class CreateLayoutCommand extends AbstractCommand
22
{
23
    /**
24
     * Checks whether the command is enabled or not in the current environment.
25
     *
26
     * @return bool
27
     */
28 3
    public function isEnabled()
29
    {
30 3
        return ! Tools::hasLayout();
31
    }
32
33
    /**
34
     * Sets the configurations of the specified command.
35
     *
36
     * @return void
37
     */
38 9
    protected function configure()
39
    {
40 9
        $this->setName('create:layout')
41 9
            ->setDescription('Creates a new header and footer file')
42 9
            ->addOption(
43 9
                'bootstrap',
44 9
                null,
45 9
                InputOption::VALUE_NONE,
46 3
                'Includes the Bootstrap CSS/JS Framework tags'
47 6
            );
48 9
    }
49
50
    /**
51
     * Executes the command.
52
     *
53
     * @param \Symfony\Component\Console\Input\InputInterface   $input
54
     * @param \Symfony\Component\Console\Output\OutputInterface $output
55
     * @return object|\Symfony\Component\Console\Output\OutputInterface
56
     */
57 6
    protected function execute(InputInterface $input, OutputInterface $output)
58
    {
59 6
        $layoutPath = APPPATH . 'views/layout';
0 ignored issues
show
Bug introduced by
The constant Rougin\Combustor\Commands\APPPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
60
61 6
        $data = [];
62
63 6
        $data['bootstrapContainer'] = '';
64 6
        $data['scripts'] = [];
65 6
        $data['styleSheets'] = [];
66
67 6
        $css = '//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css';
68
69 6
        if (! is_dir('bower_components/font-awesome') && system('bower install font-awesome --save')) {
70 6
            $css = '<?php echo base_url(\'bower_components/font-awesome/css/font-awesome.min.css\'); ?>';
71 4
        }
72
73 6
        $data['styleSheets'][0] = $css;
74
75 6
        if ($input->getOption('bootstrap')) {
76 6
            $data['bootstrapContainer'] = 'container';
77
78 6
            $css = 'https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css';
79 6
            $js = 'https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.js';
80 6
            $jquery = 'https://code.jquery.com/jquery-2.1.1.min.js';
81
82 6
            if (! is_dir('bower_components/bootstrap') && system('bower install bootstrap#3.3.7 --save')) {
83 6
                $css = '<?php echo base_url(\'bower_components/bootstrap/dist/css/bootstrap.min.css\'); ?>';
84 6
                $js = '<?php echo base_url(\'bower_components/bootstrap/dist/js/bootstrap.min.js\'); ?>';
85 6
                $jquery = '<?php echo base_url(\'bower_components/jquery/dist/jquery.min.js\'); ?>';
86 4
            }
87
 
88 6
            array_push($data['styleSheets'], $css);
89 6
            array_push($data['scripts'], $jquery);
90 6
            array_push($data['scripts'], $js);
91 4
        }
92
93 6
        if (! @mkdir($layoutPath, 0777, true)) {
94 3
            $message = 'The layout directory already exists!';
95
96 3
            return $output->writeln('<error>' . $message . '</error>');
97
        }
98
99 6
        $header = $this->renderer->render('Views/Layout/header.tpl', $data);
100 6
        $footer = $this->renderer->render('Views/Layout/footer.tpl', $data);
101
102 6
        $headerFile = new File($layoutPath . '/header.php');
103 6
        $footerFile = new File($layoutPath . '/footer.php');
104
105 6
        $headerFile->putContents($header);
106 6
        $footerFile->putContents($footer);
107
108 6
        $headerFile->close();
109 6
        $footerFile->close();
110
111 6
        $message = 'The layout folder has been created successfully!';
112
113 6
        return $output->writeln('<info>' . $message . '</info>');
114
    }
115
}
116