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

UpdateCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 67.35%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 7
lcom 2
cbo 7
dl 0
loc 94
ccs 33
cts 49
cp 0.6735
rs 10
c 3
b 1
f 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B configure() 0 33 1
A execute() 0 11 1
A getCache() 0 9 2
A getLogger() 0 8 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace BrowscapPHP\Command;
5
6
use BrowscapPHP\BrowscapUpdater;
7
use BrowscapPHP\Helper\IniLoaderInterface;
8
use BrowscapPHP\Helper\LoggerHelper;
9
use Doctrine\Common\Cache\FilesystemCache;
10
use Psr\Log\LoggerInterface;
11
use Psr\SimpleCache\CacheInterface;
12
use Roave\DoctrineSimpleCache\SimpleCacheAdapter;
13
use Symfony\Component\Console\Command\Command;
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 fetch a browscap ini file from the remote host, convert it into an array and store the content in a local
20
 * file
21
 */
22
class UpdateCommand extends Command
23
{
24
    /**
25
     * @var ?CacheInterface
26
     */
27
    private $cache;
28
29
    /**
30
     * @var string
31
     */
32
    private $defaultCacheFolder;
33
34
    /**
35
     * @var LoggerInterface
36
     */
37
    private $logger;
38
39 1
    public function __construct(
40
        string $defaultCacheFolder,
41
        ?CacheInterface $cache = null,
42
        ?LoggerInterface $logger = null
43
    ) {
44 1
        $this->defaultCacheFolder = $defaultCacheFolder;
45 1
        $this->cache = $cache;
46 1
        $this->logger = $logger;
47
48 1
        parent::__construct();
49 1
    }
50
51 1
    protected function configure() : void
52
    {
53
        $this
54 1
            ->setName('browscap:update')
55 1
            ->setDescription('Fetches an updated INI file for Browscap and overwrites the current PHP file.')
56 1
            ->addOption(
57 1
                'remote-file',
58 1
                'r',
59 1
                InputOption::VALUE_OPTIONAL,
60 1
                'browscap.ini file to download from remote location (possible values are: ' . IniLoaderInterface::PHP_INI_LITE
61 1
                . ', ' . IniLoaderInterface::PHP_INI . ', ' . IniLoaderInterface::PHP_INI_FULL . ')',
62 1
                IniLoaderInterface::PHP_INI
63
            )
64 1
            ->addOption(
65 1
                'no-backup',
66 1
                null,
67 1
                InputOption::VALUE_NONE,
68 1
                'Do not backup the previously existing file'
69
            )
70 1
            ->addOption(
71 1
                'debug',
72 1
                'd',
73 1
                InputOption::VALUE_NONE,
74 1
                'Should the debug mode entered?'
75
            )
76 1
            ->addOption(
77 1
                'cache',
78 1
                'c',
79 1
                InputOption::VALUE_OPTIONAL,
80 1
                'Where the cache files are located',
81 1
                $this->defaultCacheFolder
82
            );
83 1
    }
84
85
    protected function execute(InputInterface $input, OutputInterface $output) : void
86
    {
87
        $logger = $this->getLogger($input);
88
89
        $logger->info('started updating cache with remote file');
90
91
        $browscap = new BrowscapUpdater($this->getCache($input), $logger);
92
        $browscap->update($input->getOption('remote-file'));
93
94
        $logger->info('finished updating cache with remote file');
95
    }
96
97
    private function getCache(InputInterface $input) : CacheInterface
98
    {
99
        if (null === $this->cache) {
100
            $fileCache = new FilesystemCache($input->getOption('cache'));
101
            $this->cache = new SimpleCacheAdapter($fileCache);
102
        }
103
104
        return $this->cache;
105
    }
106
107
    private function getLogger(InputInterface $input) : LoggerInterface
108
    {
109
        if (null === $this->logger) {
110
            $this->logger = LoggerHelper::createDefaultLogger($input->getOption('debug'));
111
        }
112
113
        return $this->logger;
114
    }
115
}
116