Completed
Push — master ( 17f397...dedab2 )
by Samuel
13:14 queued 11:43
created

Application::buildContainer()   C

Complexity

Conditions 11
Paths 60

Size

Total Lines 45
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 13.0563

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 45
ccs 26
cts 35
cp 0.7429
rs 5.2653
cc 11
eloc 31
nc 60
nop 1
crap 13.0563

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
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
        if ($input->hasParameterOption('--cli')) {
151 1
            $this->config['adapter'] = 'cli';
152 4
        } elseif ($input->hasParameterOption('--fcgi')) {
153 1
            $this->config['adapter'] = 'fastcgi';
154
          
155 1
            if (!is_null($input->getParameterOption('--fcgi'))) {
156 1
                $this->config['fastcgi'] = $input->getParameterOption('--fcgi');
157 1
            }
158 3
        } elseif ($input->hasParameterOption('--web')) {
159
            $this->config['adapter'] = 'web';
160
            $this->config['webPath'] = $input->getParameterOption('--web-path');
161
            $this->config['webUrl'] = $input->getParameterOption('--web-url');
162
            $this->config['http'] = new FileGetContents($input->getParameterOption('--web-url'));
163
        }
164
165 4
        if ($input->hasParameterOption('--tmp-dir') || $input->hasParameterOption('-t')) {
166
            $this->config['temp_dir'] = $input->getParameterOption('--tmp-dir') ?: $input->getParameterOption('-t');
167
        }
168
169 4
        switch ($this->config['adapter']) {
170 4
            case 'cli':
171 2
                $adapter = new Cli();
172 2
                break;
173
174 2
            case 'fastcgi':
175 1
                $adapter = new FastCGI($this->config['fastcgi']);
176 1
                break;
177
178 1
            case 'web':
179
                $adapter = new Web($this->config['webPath'], $this->config['http']);
180
                break;
181
182 1
            default:
183 1
                throw new \RuntimeException("Adapter `{$this->config['adapter']}` is not one of cli, fastcgi or web");
184 4
        }
185
186 3
        $cacheTool = CacheTool::factory($adapter, $this->config['temp_dir'], $this->logger);
187 3
        $container = new Container();
188 3
        $container->set('cachetool', $cacheTool);
189 3
        $container->set('logger', $this->logger);
190
191 3
        return $container;
192
    }
193
}
194