OptimizeCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 72
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
A execute() 0 14 2
A cacheRouteTable() 0 11 1
A cacheConfig() 0 33 5
1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @link http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
namespace Infuse\Console;
12
13
use Infuse\HasApp;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class OptimizeCommand extends Command
19
{
20
    use HasApp;
21
22
    protected function configure()
23
    {
24
        $this
25
            ->setName('optimize')
26
            ->setDescription('Optimizes the app');
27
    }
28
29
    protected function execute(InputInterface $input, OutputInterface $output)
30
    {
31
        $output->writeln('Optimizing app');
32
33
        if ($this->app['config']->get('router.cacheFile')) {
34
            $this->cacheRouteTable($output);
35
        } else {
36
            $output->writeln('The route table could be cached with the router.cacheFile setting');
37
        }
38
39
        $this->cacheConfig($output);
40
41
        return 0;
42
    }
43
44
    private function cacheRouteTable(OutputInterface $output)
45
    {
46
        $output->writeln('-- Caching route table');
47
48
        $cacheFile = $this->app['config']->get('router.cacheFile');
49
        @unlink($cacheFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
50
51
        $this->app['router']->getDispatcher();
52
53
        $output->writeln('Success!');
54
    }
55
56
    private function cacheConfig(OutputInterface $output)
57
    {
58
        $output->writeln('-- Caching configuration');
59
60
        $configFile = INFUSE_BASE_DIR.'/config.php';
61
        $originalConfigFile = INFUSE_BASE_DIR.'/config.original.php';
62
63
        // we are going to cache the already loaded config,
64
        // unless the loaded config comes from a cached version
65
        $settings = $this->app['config']->all();
66
        if (file_exists($originalConfigFile)) {
67
            $settings = include $originalConfigFile;
68
        }
69
70
        // build a hard-coded version of the application configuration
71
        // that does not include any configuration building logic
72
        $configStr = "<?php\n// THIS FILE IS AUTO-GENERATED\n";
73
        $configStr .= 'return ';
74
        $configStr .= var_export($settings, true);
75
        $configStr .= ';';
76
77
        // move the original config file to a backup
78
        if (file_exists($configFile) && !file_exists($originalConfigFile)) {
79
            rename($configFile, $originalConfigFile);
80
        }
81
82
        // flush out 
83
        if (file_put_contents($configFile, $configStr)) {
84
            $output->writeln('Success!');
85
        } else {
86
            $output->writeln('Could not write config.php');
87
        }
88
    }
89
}
90