Completed
Push — master ( da9ea2...cfe8fe )
by Rougin
08:08
created

CreateLayoutCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 87.76%

Importance

Changes 8
Bugs 3 Features 1
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 98
ccs 43
cts 49
cp 0.8776
rs 10
c 8
b 3
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isEnabled() 0 4 1
A configure() 0 11 1
B execute() 0 58 7
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\Tools;
11
12
/**
13
 * Create Layout Command
14
 *
15
 * Creates a new header and footer file for CodeIgniter
16
 * 
17
 * @package Combustor
18
 * @author  Rougin Royce Gutib <[email protected]>
19
 */
20
class CreateLayoutCommand extends AbstractCommand
21
{
22
    /**
23
     * Checks whether the command is enabled or not in the current environment.
24
     *
25
     * Override this to check for x or y and return false if the command can not
26
     * run properly under the current conditions.
27
     *
28
     * @return bool
29
     */
30 3
    public function isEnabled()
31
    {
32 3
        return ! Tools::hasLayout();
33
    }
34
35
    /**
36
     * Sets the configurations of the specified command.
37
     *
38
     * @return void
39
     */
40 9
    protected function configure()
41
    {
42 9
        $this->setName('create:layout')
43 9
            ->setDescription('Creates a new header and footer file')
44 9
            ->addOption(
45 9
                'bootstrap',
46 9
                NULL,
47 9
                InputOption::VALUE_NONE,
48
                'Includes the Bootstrap CSS/JS Framework tags'
49 9
            );
50 9
    }
51
52
    /**
53
     * Executes the command.
54
     * 
55
     * @param \Symfony\Component\Console\Input\InputInterface   $input
56
     * @param \Symfony\Component\Console\Output\OutputInterface $output
57
     * @return object|\Symfony\Component\Console\Output\OutputInterface
58
     */
59 6
    protected function execute(InputInterface $input, OutputInterface $output)
60
    {
61 6
        $layoutPath = APPPATH . 'views/layout';
62
63 6
        $data = [];
64
65 6
        $data['bootstrapContainer'] = '';
66 6
        $data['scripts'] = [];
67 6
        $data['styleSheets'] = [];
68
69 6
        $css = '//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css';
70
71 6
        if ( ! is_dir('bower_components/font-awesome') && system('bower install font-awesome')) {
72
            $css = '<?php echo base_url(\'bower_components/font-awesome/css/font-awesome.min.css\'); ?>';
73
        }
74
75 6
        $data['styleSheets'][0] = $css;
76
77 6
        if ($input->getOption('bootstrap')) {
78 6
            $data['bootstrapContainer'] = 'container';
79
80 6
            $css = 'https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css';
81 6
            $js = 'https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.js';
82 6
            $jquery = 'https://code.jquery.com/jquery-2.1.1.min.js';
83
84 6
            if ( ! is_dir('bower_components/bootstrap') && system('bower install bootstrap')) {
85
                $css = '<?php echo base_url(\'bower_components/bootstrap/dist/css/bootstrap.min.css\'); ?>';
86
                $js = '<?php echo base_url(\'bower_components/bootstrap/dist/js/bootstrap.min.js\'); ?>';
87
                $jquery = '<?php echo base_url(\'bower_components/jquery/dist/jquery.min.js\'); ?>';
88
            }
89
 
90 6
            array_push($data['styleSheets'], $css);
91 6
            array_push($data['scripts'], $jquery);
92 6
            array_push($data['scripts'], $js);
93 6
        }
94
95 6
        if ( ! @mkdir($layoutPath, 0777, true)) {
96 3
            $message = 'The layout directory already exists!';
97
98 3
            return $output->writeln('<error>' . $message . '</error>');
99
        }
100
101 6
        $header = $this->renderer->render('Views/Layout/header.template', $data);
102 6
        $footer = $this->renderer->render('Views/Layout/footer.template', $data);
103
104 6
        $headerFile = fopen($layoutPath . '/header.php', 'wb');
105 6
        $footerFile = fopen($layoutPath . '/footer.php', 'wb');
106
107 6
        file_put_contents($layoutPath . '/header.php', $header);
108 6
        file_put_contents($layoutPath . '/footer.php', $footer);
109
110 6
        fclose($headerFile);
111 6
        fclose($footerFile);
112
113 6
        $message = 'The layout folder has been created successfully!';
114
115 6
        return $output->writeln('<info>'.$message.'</info>');
116
    }
117
}
118