Completed
Push — master ( 113db7...3cc6f9 )
by Joao
02:23
created

SetupCommand::compileConfigFiles()   D

Complexity

Conditions 9
Paths 16

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 41
rs 4.909
cc 9
eloc 23
nc 16
nop 0
1
<?php namespace jlourenco\support\Commands;
2
3
use Illuminate\Console\Command;
4
use Symfony\Component\Console\Input\InputOption;
5
use Symfony\Component\Console\Input\InputArgument;
6
use File;
7
8
class SetupCommand extends Command {
9
10
    /**
11
     * The console command name.
12
     *
13
     * @var string
14
     */
15
    protected $name = 'jlourenco:setup';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Command to setup all the requirements for the jlourenco packages';
23
24
    /**
25
     * Create a new command instance.
26
     *
27
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
28
     */
29
    public function __construct()
30
    {
31
        parent::__construct();
32
    }
33
34
    /**
35
     * Execute the console command.
36
     *
37
     * @return mixed
38
     */
39
    public function fire()
40
    {
41
        if (!$this->confirm('Running this command will reset all jlourenco config files. Are you sure? '))
42
        {
43
            $this->info('Command was aborted by the user.');
44
            return;
45
        }
46
47
        $this->compileConfigFiles();
48
49
        $this->info('Command ran successfully');
50
    }
51
52
    private function compileConfigFiles()
53
    {
54
        $path = base_path('/config');
55
        $mainFile = $path . '/jlourenco.php';
56
        $fileExists = file_exists($mainFile);
57
58
        $files = array_filter(scandir($path), function ($var) {
59
            return (!(stripos($var, 'jlourenco.') === false) && $var != 'jlourenco.php');
60
        });
61
62
        if ($fileExists)
63
            unlink($mainFile);
64
65
        touch($mainFile);
66
67
        $content = "<?php\n";
68
        $content .= "return [\n\n";
69
70
        foreach ($files as $file)
71
        {
72
            $in = fopen($path . '/' . $file, "r");
73
74
            while ($line = fgets($in))
75
            {
76
                if ((stripos($line, '<?php') === false) && (stripos($line, '];') === false) && (stripos($line, 'return [') === false))
77
                    $content .= $line;
78
            }
79
80
            fclose($in);
81
82
            unlink($path . '/' . $file);
83
        }
84
85
        $content .= "];\n";
86
87
        $bytesWritten = File::append($mainFile, $content);
88
        if ($bytesWritten === false)
89
            die("Couldn't write to config file.");
0 ignored issues
show
Coding Style Compatibility introduced by
The method compileConfigFiles() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
90
91
        $this->info('Config files compiled');
92
    }
93
94
}
95