Completed
Push — master ( dedab2...f146ce )
by Samuel
03:39 queued 02:35
created

Application::getAdapter()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 1
cts 1
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 0
crap 4
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('--cli', null, InputOption::VALUE_NONE, 'If specified, forces adapter to cli'));
107 3
        $definition->addOption(new InputOption('--web', null, InputOption::VALUE_NONE, 'If specified, forces adapter to web'));
108 3
        $definition->addOption(new InputOption('--web-path', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a information for web adapter'));
109 3
        $definition->addOption(new InputOption('--web-url', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a information for web adapter'));
110 3
        $definition->addOption(new InputOption('--tmp-dir', '-t', InputOption::VALUE_REQUIRED, 'Temporary directory to write files to'));
111
112 3
        return $definition;
113
    }
114
115
    /**
116
     * {@inheritDoc}
117
     */
118 4
    public function doRun(InputInterface $input, OutputInterface $output)
119
    {
120 4
        $handler = new ConsoleHandler();
121 4
        $handler->setOutput($output);
122 4
        $this->logger->pushHandler($handler);
123
124 4
        $exitCode = parent::doRun($input, $output);
125
126 3
        $handler->close();
127
128 3
        return $exitCode;
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     */
134 4
    public function doRunCommand(Command $command, InputInterface $input, OutputInterface $output)
135
    {
136 4
        if ($command instanceof ContainerAwareInterface) {
137 4
            $container = $this->buildContainer($input);
138 3
            $command->setContainer($container);
139 3
        }
140
141 3
        return parent::doRunCommand($command, $input, $output);
142
    }
143
144
    /**
145
     * @param  InputInterface     $input
146
     * @return ContainerInterface
147
     */
148 4
    public function buildContainer(InputInterface $input)
149
    {
150 4
        $this->parseConfiguration($input);
151 1
        $adapter = $this->getAdapter();
152 4
153 1
        $cacheTool = CacheTool::factory($adapter, $this->config['temp_dir'], $this->logger);
154
        $container = new Container();
155 1
        $container->set('cachetool', $cacheTool);
156 1
        $container->set('logger', $this->logger);
157 1
158 3
        return $container;
159
    }
160
161
    /**
162
     * @param  InputInterface $input
163
     */
164
    private function parseConfiguration(InputInterface $input)
165 4
    {
166
        if ($input->hasParameterOption('--cli')) {
167
            $this->config['adapter'] = 'cli';
168
        } elseif ($input->hasParameterOption('--fcgi')) {
169 4
            $this->config['adapter'] = 'fastcgi';
170 4
171 2
            if (!is_null($input->getParameterOption('--fcgi'))) {
172 2
                $this->config['fastcgi'] = $input->getParameterOption('--fcgi');
173
            }
174 2
        } elseif ($input->hasParameterOption('--web')) {
175 1
            $this->config['adapter'] = 'web';
176 1
            $this->config['webPath'] = $input->getParameterOption('--web-path');
177
            $this->config['webUrl'] = $input->getParameterOption('--web-url');
178 1
            $this->config['http'] = new FileGetContents($input->getParameterOption('--web-url'));
179
        }
180
181
        if ($input->hasParameterOption('--tmp-dir') || $input->hasParameterOption('-t')) {
182 1
            $this->config['temp_dir'] = $input->getParameterOption('--tmp-dir') ?: $input->getParameterOption('-t');
183 1
        }
184 4
    }
185
186 3
187 3
    /**
188 3
     * @return null|\CacheTool\Adapter\AbstractAdapter
189 3
     */
190
    private function getAdapter()
191 3
    {
192
        switch ($this->config['adapter']) {
193
            case 'cli':
194
                return new Cli();
195
            case 'fastcgi':
196
                return new FastCGI($this->config['fastcgi']);
197
            case 'web':
198
                return new Web($this->config['webPath'], $this->config['http']);
199
        }
200
201
        throw new \RuntimeException("Adapter `{$this->config['adapter']}` is not one of cli, fastcgi or web");
202
    }
203
}
204