Completed
Pull Request — master (#218)
by Thomas
05:43
created

ConvertCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 46.51%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 0
loc 94
ccs 20
cts 43
cp 0.4651
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
B configure() 0 25 1
A execute() 0 16 2
A getCache() 0 9 2
A getLogger() 0 4 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace BrowscapPHP\Command;
5
6
use BrowscapPHP\BrowscapUpdater;
7
use BrowscapPHP\Helper\LoggerHelper;
8
use Doctrine\Common\Cache\FilesystemCache;
9
use Psr\Log\LoggerInterface;
10
use Psr\SimpleCache\CacheInterface;
11
use Roave\DoctrineSimpleCache\SimpleCacheAdapter;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
/**
19
 * Command to convert a downloaded Browscap ini file and write it to the cache
20
 */
21
class ConvertCommand extends Command
22
{
23
    /**
24
     * @var ?CacheInterface
25
     */
26
    private $cache;
27
28
    /**
29
     * @var string
30
     */
31
    private $defaultIniFile;
32
33
    /**
34
     * @var string
35
     */
36
    private $defaultCacheFolder;
37
38
    /**
39
     * @var LoggerInterface
40
     */
41
    private $logger;
42
43
    public function __construct(
44
        string $defaultCacheFolder,
45
        string $defaultIniFile,
46
        ?CacheInterface $cache = null,
47
        ?LoggerInterface $logger = null
48
    ) {
49
        $this->defaultCacheFolder = $defaultCacheFolder;
50
        $this->defaultIniFile = $defaultIniFile;
51
        $this->cache = $cache;
52
        $this->logger = $logger;
53
54
        parent::__construct();
55
    }
56
57 1
    protected function configure() : void
58
    {
59
        $this
60 1
            ->setName('browscap:convert')
61 1
            ->setDescription('Converts an existing browscap.ini file to a cache.php file.')
62 1
            ->addArgument(
63 1
                'file',
64 1
                InputArgument::OPTIONAL,
65 1
                'Path to the browscap.ini file',
66 1
                $this->defaultIniFile
67
            )
68 1
            ->addOption(
69 1
                'debug',
70 1
                'd',
71 1
                InputOption::VALUE_NONE,
72 1
                'Should the debug mode entered?'
73
            )
74 1
            ->addOption(
75 1
                'cache',
76 1
                'c',
77 1
                InputOption::VALUE_OPTIONAL,
78 1
                'Where the cache files are located',
79 1
                $this->defaultCacheFolder
80
            );
81 1
    }
82
83
    protected function execute(InputInterface $input, OutputInterface $output) : void
84
    {
85
        $logger = $this->getLogger($input);
86
87
        $logger->info('initializing converting process');
88
89
        $browscap = new BrowscapUpdater($this->getCache($input), $logger);
90
91
        $logger->info('started converting local file');
92
93
        $file = ($input->getArgument('file') ? $input->getArgument('file') : ($this->defaultIniFile));
94
95
        $browscap->convertFile($file);
96
97
        $logger->info('finished converting local file');
98
    }
99
100
    private function getCache(InputInterface $input) : CacheInterface
101
    {
102
        if (null === $this->cache) {
103
            $fileCache = new FilesystemCache($input->getOption('cache'));
104
            $this->cache = new SimpleCacheAdapter($fileCache);
105
        }
106
107
        return $this->cache;
108
    }
109
110
    private function getLogger(InputInterface $input) : LoggerInterface
111
    {
112
        return LoggerHelper::createDefaultLogger($input->getOption('debug'));
113
    }
114
}
115