Completed
Push — master ( 17dfca...2f9de9 )
by Samuel
01:17
created

Application   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 33

Test Coverage

Coverage 94.25%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 33
dl 0
loc 173
ccs 82
cts 87
cp 0.9425
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getDefaultCommands() 0 34 3
A getDefaultInputDefinition() 0 13 1
A doRun() 0 12 1
A doRunCommand() 0 9 2
A buildContainer() 0 12 1
B parseConfiguration() 0 25 9
A getAdapter() 0 13 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 20
    public function __construct(Config $config)
49
    {
50 20
        parent::__construct('CacheTool', self::VERSION);
51
52 20
        $this->config = $config;
53 20
        $this->logger = new Logger('cachetool');
54 20
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 20
    protected function getDefaultCommands()
60
    {
61 20
        $commands = parent::getDefaultCommands();
62 20
        $commands[] = new CacheToolCommand\SelfUpdateCommand();
63
64 20
        if (in_array('apcu', $this->config['extensions'], true)) {
65 19
            $commands[] = new CacheToolCommand\ApcuCacheClearCommand();
66 19
            $commands[] = new CacheToolCommand\ApcuCacheInfoCommand();
67 19
            $commands[] = new CacheToolCommand\ApcuCacheInfoKeysCommand();
68 19
            $commands[] = new CacheToolCommand\ApcuKeyDeleteCommand();
69 19
            $commands[] = new CacheToolCommand\ApcuKeyExistsCommand();
70 19
            $commands[] = new CacheToolCommand\ApcuKeyFetchCommand();
71 19
            $commands[] = new CacheToolCommand\ApcuKeyStoreCommand();
72 19
            $commands[] = new CacheToolCommand\ApcuSmaInfoCommand();
73 19
            $commands[] = new CacheToolCommand\ApcuRegexpDeleteCommand();
74
        }
75
76 20
        if (in_array('opcache', $this->config['extensions'], true)) {
77 19
            $commands[] = new CacheToolCommand\OpcacheConfigurationCommand();
78 19
            $commands[] = new CacheToolCommand\OpcacheResetCommand();
79 19
            $commands[] = new CacheToolCommand\OpcacheResetFileCacheCommand();
80 19
            $commands[] = new CacheToolCommand\OpcacheStatusCommand();
81 19
            $commands[] = new CacheToolCommand\OpcacheStatusScriptsCommand();
82 19
            $commands[] = new CacheToolCommand\OpcacheInvalidateScriptsCommand();
83 19
            $commands[] = new CacheToolCommand\OpcacheCompileScriptsCommand();
84 19
            $commands[] = new CacheToolCommand\OpcacheCompileScriptCommand();
85
        }
86
87 20
        $commands[] = new CacheToolCommand\StatCacheClearCommand();
88 20
        $commands[] = new CacheToolCommand\StatRealpathGetCommand();
89 20
        $commands[] = new CacheToolCommand\StatRealpathSizeCommand();
90
91 20
        return $commands;
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97 20
    protected function getDefaultInputDefinition()
98
    {
99 20
        $definition = parent::getDefaultInputDefinition();
100 20
        $definition->addOption(new InputOption('--fcgi', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a connection string to FastCGI server.'));
101 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.'));
102 20
        $definition->addOption(new InputOption('--cli', null, InputOption::VALUE_NONE, 'If specified, forces adapter to cli'));
103 20
        $definition->addOption(new InputOption('--web', null, InputOption::VALUE_NONE, 'If specified, forces adapter to web'));
104 20
        $definition->addOption(new InputOption('--web-path', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a information for web adapter'));
105 20
        $definition->addOption(new InputOption('--web-url', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a information for web adapter'));
106 20
        $definition->addOption(new InputOption('--tmp-dir', '-t', InputOption::VALUE_REQUIRED, 'Temporary directory to write files to'));
107
108 20
        return $definition;
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     */
114 20
    public function doRun(InputInterface $input, OutputInterface $output)
115
    {
116 20
        $handler = new ConsoleHandler();
117 20
        $handler->setOutput($output);
118 20
        $this->logger->pushHandler($handler);
119
120 20
        $exitCode = parent::doRun($input, $output);
121
122 17
        $handler->close();
123
124 17
        return $exitCode;
125
    }
126
127
    /**
128
     * {@inheritDoc}
129
     */
130 20
    public function doRunCommand(Command $command, InputInterface $input, OutputInterface $output)
131
    {
132 20
        if ($command instanceof ContainerAwareInterface) {
133 18
            $container = $this->buildContainer($input);
134 17
            $command->setContainer($container);
135
        }
136
137 19
        return parent::doRunCommand($command, $input, $output);
138
    }
139
140
    /**
141
     * @param  InputInterface     $input
142
     * @return ContainerInterface
143
     */
144 18
    public function buildContainer(InputInterface $input)
145
    {
146 18
        $this->parseConfiguration($input);
147 18
        $adapter = $this->getAdapter();
148
149 17
        $cacheTool = CacheTool::factory($adapter, $this->config['temp_dir'], $this->logger);
150 17
        $container = new Container();
151 17
        $container->set('cachetool', $cacheTool);
152 17
        $container->set('logger', $this->logger);
153
154 17
        return $container;
155
    }
156
157
    /**
158
     * @param  InputInterface $input
159
     */
160 18
    private function parseConfiguration(InputInterface $input)
161
    {
162 18
        if ($input->hasParameterOption('--cli')) {
163 1
            $this->config['adapter'] = 'cli';
164 17
        } elseif ($input->hasParameterOption('--fcgi')) {
165 1
            $this->config['adapter'] = 'fastcgi';
166 1
            $this->config['fastcgiChroot'] = $input->getParameterOption('--fcgi-chroot');
167
168 1
            if (!is_null($input->getParameterOption('--fcgi'))) {
169 1
                $this->config['fastcgi'] = $input->getParameterOption('--fcgi');
170
            }
171 16
        } elseif ($input->hasParameterOption('--web')) {
172
            $this->config['adapter'] = 'web';
173
            $this->config['webPath'] = $input->getParameterOption('--web-path');
174
            $this->config['webUrl'] = $input->getParameterOption('--web-url');
175
        }
176
177 18
        if ($this->config['adapter'] === 'web') {
178
            $this->config['http'] = new FileGetContents($this->config['webUrl']);
179
        }
180
181 18
        if ($input->hasParameterOption('--tmp-dir') || $input->hasParameterOption('-t')) {
182 1
            $this->config['temp_dir'] = $input->getParameterOption('--tmp-dir') ?: $input->getParameterOption('-t');
183
        }
184 18
    }
185
186
187
    /**
188
     * @return null|\CacheTool\Adapter\AbstractAdapter
189
     */
190 18
    private function getAdapter()
191
    {
192 18
        switch ($this->config['adapter']) {
193 18
            case 'cli':
194 14
                return new Cli();
195 4
            case 'fastcgi':
196 3
                return new FastCGI($this->config['fastcgi'], $this->config['fastcgiChroot']);
197 1
            case 'web':
198
                return new Web($this->config['webPath'], $this->config['http']);
199
        }
200
201 1
        throw new \RuntimeException("Adapter `{$this->config['adapter']}` is not one of cli, fastcgi or web");
202
    }
203
}
204