Completed
Push — master ( 5017c0...9386a2 )
by Samuel
11:29
created

Application::parseConfiguration()   B

Complexity

Conditions 11
Paths 90

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 18.2496

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 14
cts 23
cp 0.6087
rs 7.3166
c 0
b 0
f 0
cc 11
nc 90
nop 1
crap 18.2496

How to fix   Complexity   

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
/*
4
 * This file is part of CacheTool.
5
 *
6
 * (c) Samuel Gordalina <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CacheTool\Console;
13
14
use CacheTool\Adapter\FastCGI;
15
use CacheTool\Adapter\Cli;
16
use CacheTool\Adapter\Http\FileGetContents;
17
use CacheTool\Adapter\Web;
18
use CacheTool\CacheTool;
19
use CacheTool\Command as CacheToolCommand;
20
use CacheTool\Monolog\ConsoleHandler;
21
use Monolog\Logger;
22
use Symfony\Component\Console\Application as BaseApplication;
23
use Symfony\Component\Console\Command\Command;
24
use Symfony\Component\Console\Input\InputOption;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Output\OutputInterface;
27
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
28
use Symfony\Component\DependencyInjection\Container;
29
use Symfony\Component\DependencyInjection\ContainerInterface;
30
use Symfony\Component\Yaml\Parser;
31
32
class Application extends BaseApplication
33
{
34
    const VERSION = '@package_version@';
35
36
    /**
37
     * @var Config
38
     */
39
    protected $config;
40
41
    /**
42
     * @var Logger
43
     */
44
    protected $logger;
45
46
    /**
47
     * @param Config $config
48
     */
49 20
    public function __construct(Config $config)
50
    {
51 20
        parent::__construct('CacheTool', self::VERSION);
52
53 20
        $this->config = $config;
54 20
        $this->logger = new Logger('cachetool');
55 20
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 20
    protected function getDefaultCommands()
61
    {
62 20
        $commands = parent::getDefaultCommands();
63 20
        $commands[] = new CacheToolCommand\SelfUpdateCommand();
64
65 20
        if (in_array('apcu', $this->config['extensions'], true)) {
66 19
            $commands[] = new CacheToolCommand\ApcuCacheClearCommand();
67 19
            $commands[] = new CacheToolCommand\ApcuCacheInfoCommand();
68 19
            $commands[] = new CacheToolCommand\ApcuCacheInfoKeysCommand();
69 19
            $commands[] = new CacheToolCommand\ApcuKeyDeleteCommand();
70 19
            $commands[] = new CacheToolCommand\ApcuKeyExistsCommand();
71 19
            $commands[] = new CacheToolCommand\ApcuKeyFetchCommand();
72 19
            $commands[] = new CacheToolCommand\ApcuKeyStoreCommand();
73 19
            $commands[] = new CacheToolCommand\ApcuSmaInfoCommand();
74 19
            $commands[] = new CacheToolCommand\ApcuRegexpDeleteCommand();
75
        }
76
77 20
        if (in_array('opcache', $this->config['extensions'], true)) {
78 19
            $commands[] = new CacheToolCommand\OpcacheConfigurationCommand();
79 19
            $commands[] = new CacheToolCommand\OpcacheResetCommand();
80 19
            $commands[] = new CacheToolCommand\OpcacheResetFileCacheCommand();
81 19
            $commands[] = new CacheToolCommand\OpcacheStatusCommand();
82 19
            $commands[] = new CacheToolCommand\OpcacheStatusScriptsCommand();
83 19
            $commands[] = new CacheToolCommand\OpcacheInvalidateScriptsCommand();
84 19
            $commands[] = new CacheToolCommand\OpcacheCompileScriptsCommand();
85 19
            $commands[] = new CacheToolCommand\OpcacheCompileScriptCommand();
86
        }
87
88 20
        $commands[] = new CacheToolCommand\StatCacheClearCommand();
89 20
        $commands[] = new CacheToolCommand\StatRealpathGetCommand();
90 20
        $commands[] = new CacheToolCommand\StatRealpathSizeCommand();
91
92 20
        return $commands;
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98 20
    protected function getDefaultInputDefinition()
99
    {
100 20
        $definition = parent::getDefaultInputDefinition();
101 20
        $definition->addOption(new InputOption('--fcgi', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a connection string to FastCGI server.'));
102 20
        $definition->addOption(new InputOption('--fcgi-chroot', null, InputOption::VALUE_OPTIONAL, 'If specified, used for mapping script path to chrooted FastCGI server. --tmp-dir need to be chrooted too.'));
103 20
        $definition->addOption(new InputOption('--cli', null, InputOption::VALUE_NONE, 'If specified, forces adapter to cli'));
104 20
        $definition->addOption(new InputOption('--web', null, InputOption::VALUE_NONE, 'If specified, forces adapter to web'));
105 20
        $definition->addOption(new InputOption('--web-path', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a information for web adapter'));
106 20
        $definition->addOption(new InputOption('--web-url', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a information for web adapter'));
107 20
        $definition->addOption(new InputOption('--tmp-dir', '-t', InputOption::VALUE_REQUIRED, 'Temporary directory to write files to'));
108 20
        $definition->addOption(new InputOption('--config', '-c', InputOption::VALUE_OPTIONAL, 'If specified use this yaml config'));
109
110 20
        return $definition;
111
    }
112
113
    /**
114
     * {@inheritDoc}
115
     */
116 20
    public function doRun(InputInterface $input, OutputInterface $output)
117
    {
118 20
        $handler = new ConsoleHandler();
119 20
        $handler->setOutput($output);
120 20
        $this->logger->pushHandler($handler);
121
122 20
        $exitCode = parent::doRun($input, $output);
123
124 17
        $handler->close();
125
126 17
        return $exitCode;
127
    }
128
129
    /**
130
     * {@inheritDoc}
131
     */
132 20
    public function doRunCommand(Command $command, InputInterface $input, OutputInterface $output)
133
    {
134 20
        if ($command instanceof ContainerAwareInterface) {
135 18
            $container = $this->buildContainer($input);
136 17
            $command->setContainer($container);
137
        }
138
139 19
        return parent::doRunCommand($command, $input, $output);
140
    }
141
142
    /**
143
     * @param  InputInterface     $input
144
     * @return ContainerInterface
145
     */
146 18
    public function buildContainer(InputInterface $input)
147
    {
148 18
        $this->parseConfiguration($input);
149 18
        $adapter = $this->getAdapter();
150
151 17
        $cacheTool = CacheTool::factory($adapter, $this->config['temp_dir'], $this->logger);
152 17
        $container = new Container();
153 17
        $container->set('cachetool', $cacheTool);
154 17
        $container->set('logger', $this->logger);
155
156 17
        return $container;
157
    }
158
159
    /**
160
     * @param  InputInterface $input
161
     */
162 18
    private function parseConfiguration(InputInterface $input)
163
    {
164 18
        if ($input->hasParameterOption('--config')) {
165
            $path = $input->getParameterOption('--config');
166
            if (is_file($path)) {
167
                $yaml = new Parser();
168
                $config = $yaml->parse(file_get_contents($path));
169
                $this->config = new Config($config);
170
            }
171
        }
172
173 18
        if ($input->hasParameterOption('--cli')) {
174 1
            $this->config['adapter'] = 'cli';
175 17
        } elseif ($input->hasParameterOption('--fcgi')) {
176 1
            $this->config['adapter'] = 'fastcgi';
177 1
            $this->config['fastcgiChroot'] = $input->getParameterOption('--fcgi-chroot');
178
179 1
            if (!is_null($input->getParameterOption('--fcgi'))) {
180 1
                $this->config['fastcgi'] = $input->getParameterOption('--fcgi');
181
            }
182 16
        } elseif ($input->hasParameterOption('--web')) {
183
            $this->config['adapter'] = 'web';
184
            $this->config['webPath'] = $input->getParameterOption('--web-path');
185
            $this->config['webUrl'] = $input->getParameterOption('--web-url');
186
        }
187
188 18
        if ($this->config['adapter'] === 'web') {
189
            $this->config['http'] = new FileGetContents($this->config['webUrl']);
190
        }
191
192 18
        if ($input->hasParameterOption('--tmp-dir') || $input->hasParameterOption('-t')) {
193 1
            $this->config['temp_dir'] = $input->getParameterOption('--tmp-dir') ?: $input->getParameterOption('-t');
194
        }
195 18
    }
196
197
198
    /**
199
     * @return null|\CacheTool\Adapter\AbstractAdapter
200
     */
201 18
    private function getAdapter()
202
    {
203 18
        switch ($this->config['adapter']) {
204 18
            case 'cli':
205 14
                return new Cli();
206 4
            case 'fastcgi':
207 3
                return new FastCGI($this->config['fastcgi'], $this->config['fastcgiChroot']);
208 1
            case 'web':
209
                return new Web($this->config['webPath'], $this->config['http']);
210
        }
211
212 1
        throw new \RuntimeException("Adapter `{$this->config['adapter']}` is not one of cli, fastcgi or web");
213
    }
214
}
215