Completed
Push — master ( 515494...4d37ef )
by Samuel
03:24 queued 01:55
created

Application::parseConfiguration()   C

Complexity

Conditions 8
Paths 15

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 12.4692

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
ccs 10
cts 17
cp 0.5881
rs 6.6037
cc 8
eloc 15
nc 15
nop 1
crap 12.4692
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
31
class Application extends BaseApplication
32
{
33
    const VERSION = '@package_version@';
34
35
    /**
36
     * @var Config
37
     */
38
    protected $config;
39
40
    /**
41
     * @var Logger
42
     */
43
    protected $logger;
44
45
    /**
46
     * @param Config $config
47
     */
48 4
    public function __construct(Config $config)
49
    {
50 4
        parent::__construct('CacheTool', self::VERSION);
51
52 4
        $this->config = $config;
53 4
        $this->logger = new Logger('cachetool');
54 4
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 4
    protected function getDefaultCommands()
60
    {
61 4
        $commands = parent::getDefaultCommands();
62 4
        $commands[] = new CacheToolCommand\SelfUpdateCommand();
63
64 4
        $commands[] = new CacheToolCommand\ApcBinDumpCommand();
65 4
        $commands[] = new CacheToolCommand\ApcBinLoadCommand();
66 4
        $commands[] = new CacheToolCommand\ApcCacheClearCommand();
67 4
        $commands[] = new CacheToolCommand\ApcCacheInfoCommand();
68 4
        $commands[] = new CacheToolCommand\ApcCacheInfoFileCommand();
69 4
        $commands[] = new CacheToolCommand\ApcKeyDeleteCommand();
70 4
        $commands[] = new CacheToolCommand\ApcKeyExistsCommand();
71 4
        $commands[] = new CacheToolCommand\ApcKeyFetchCommand();
72 4
        $commands[] = new CacheToolCommand\ApcKeyStoreCommand();
73 4
        $commands[] = new CacheToolCommand\ApcSmaInfoCommand();
74 4
        $commands[] = new CacheToolCommand\ApcRegexpDeleteCommand();
75
76 4
        $commands[] = new CacheToolCommand\ApcuCacheClearCommand();
77 4
        $commands[] = new CacheToolCommand\ApcuCacheInfoCommand();
78 4
        $commands[] = new CacheToolCommand\ApcuCacheInfoKeysCommand();
79 4
        $commands[] = new CacheToolCommand\ApcuKeyDeleteCommand();
80 4
        $commands[] = new CacheToolCommand\ApcuKeyExistsCommand();
81 4
        $commands[] = new CacheToolCommand\ApcuKeyFetchCommand();
82 4
        $commands[] = new CacheToolCommand\ApcuKeyStoreCommand();
83 4
        $commands[] = new CacheToolCommand\ApcuSmaInfoCommand();
84 4
        $commands[] = new CacheToolCommand\ApcuRegexpDeleteCommand();
85
86 4
        $commands[] = new CacheToolCommand\OpcacheConfigurationCommand();
87 4
        $commands[] = new CacheToolCommand\OpcacheResetCommand();
88 4
        $commands[] = new CacheToolCommand\OpcacheStatusCommand();
89 4
        $commands[] = new CacheToolCommand\OpcacheStatusScriptsCommand();
90 4
        $commands[] = new CacheToolCommand\OpcacheInvalidateScriptsCommand();
91
92 4
        $commands[] = new CacheToolCommand\StatCacheClearCommand();
93 4
        $commands[] = new CacheToolCommand\StatRealpathGetCommand();
94 4
        $commands[] = new CacheToolCommand\StatRealpathSizeCommand();
95
96 4
        return $commands;
97
    }
98
99
    /**
100
     * {@inheritDoc}
101
     */
102 3
    protected function getDefaultInputDefinition()
103
    {
104 3
        $definition = parent::getDefaultInputDefinition();
105 3
        $definition->addOption(new InputOption('--fcgi', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a connection string to FastCGI server.'));
106 3
        $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.'));
107 3
        $definition->addOption(new InputOption('--cli', null, InputOption::VALUE_NONE, 'If specified, forces adapter to cli'));
108 3
        $definition->addOption(new InputOption('--web', null, InputOption::VALUE_NONE, 'If specified, forces adapter to web'));
109 3
        $definition->addOption(new InputOption('--web-path', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a information for web adapter'));
110 3
        $definition->addOption(new InputOption('--web-url', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a information for web adapter'));
111
        $definition->addOption(new InputOption('--tmp-dir', '-t', InputOption::VALUE_REQUIRED, 'Temporary directory to write files to'));
112 3
113
        return $definition;
114
    }
115
116
    /**
117
     * {@inheritDoc}
118 4
     */
119
    public function doRun(InputInterface $input, OutputInterface $output)
120 4
    {
121 4
        $handler = new ConsoleHandler();
122 4
        $handler->setOutput($output);
123
        $this->logger->pushHandler($handler);
124 4
125
        $exitCode = parent::doRun($input, $output);
126 3
127
        $handler->close();
128 3
129
        return $exitCode;
130
    }
131
132
    /**
133
     * {@inheritDoc}
134 4
     */
135
    public function doRunCommand(Command $command, InputInterface $input, OutputInterface $output)
136 4
    {
137 4
        if ($command instanceof ContainerAwareInterface) {
138 3
            $container = $this->buildContainer($input);
139 3
            $command->setContainer($container);
140
        }
141 3
142
        return parent::doRunCommand($command, $input, $output);
143
    }
144
145
    /**
146
     * @param  InputInterface     $input
147
     * @return ContainerInterface
148 4
     */
149
    public function buildContainer(InputInterface $input)
150 4
    {
151 4
        $this->parseConfiguration($input);
152
        $adapter = $this->getAdapter();
153 3
154 3
        $cacheTool = CacheTool::factory($adapter, $this->config['temp_dir'], $this->logger);
155 3
        $container = new Container();
156 3
        $container->set('cachetool', $cacheTool);
157
        $container->set('logger', $this->logger);
158 3
159
        return $container;
160
    }
161
162
    /**
163
     * @param  InputInterface $input
164 4
     */
165
    private function parseConfiguration(InputInterface $input)
166 4
    {
167 1
        if ($input->hasParameterOption('--cli')) {
168 4
            $this->config['adapter'] = 'cli';
169 1
        } elseif ($input->hasParameterOption('--fcgi')) {
170
            $this->config['adapter'] = 'fastcgi';
171 1
            $this->config['fastcgiChroot'] = $input->getParameterOption('--fcgi-chroot');
172 1
173 1
            if (!is_null($input->getParameterOption('--fcgi'))) {
174 3
                $this->config['fastcgi'] = $input->getParameterOption('--fcgi');
175
            }
176
        } elseif ($input->hasParameterOption('--web')) {
177
            $this->config['adapter'] = 'web';
178
            $this->config['webPath'] = $input->getParameterOption('--web-path');
179
            $this->config['webUrl'] = $input->getParameterOption('--web-url');
180
            $this->config['http'] = new FileGetContents($input->getParameterOption('--web-url'));
181 4
        }
182
183
        if ($input->hasParameterOption('--tmp-dir') || $input->hasParameterOption('-t')) {
184 4
            $this->config['temp_dir'] = $input->getParameterOption('--tmp-dir') ?: $input->getParameterOption('-t');
185
        }
186
    }
187
188
189
    /**
190 4
     * @return null|\CacheTool\Adapter\AbstractAdapter
191
     */
192 4
    private function getAdapter()
193 4
    {
194 2
        switch ($this->config['adapter']) {
195 2
            case 'cli':
196 1
                return new Cli();
197 1
            case 'fastcgi':
198
                return new FastCGI($this->config['fastcgi'], $this->config['fastcgiChroot']);
199 1
            case 'web':
200
                return new Web($this->config['webPath'], $this->config['http']);
201 1
        }
202
203
        throw new \RuntimeException("Adapter `{$this->config['adapter']}` is not one of cli, fastcgi or web");
204
    }
205
}
206