Completed
Push — min-php71 ( f5a25a )
by James
05:53
created

UpdateCommand::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 15
ccs 0
cts 0
cp 0
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 9
nc 1
nop 2
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace BrowscapPHP\Command;
5
6
use BrowscapPHP\BrowscapUpdater;
7
use BrowscapPHP\Cache\BrowscapCache;
8
use BrowscapPHP\Cache\BrowscapCacheInterface;
9
use BrowscapPHP\Helper\IniLoader;
10
use BrowscapPHP\Helper\LoggerHelper;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use WurflCache\Adapter\File;
16
17
/**
18
 * Command to fetch a browscap ini file from the remote host, convert it into an array and store the content in a local
19
 * file
20
 */
21
class UpdateCommand extends Command
22
{
23
    /**
24
     * @var ?BrowscapCacheInterface
25
     */
26
    private $cache;
27
28
    /**
29
     * @var string
30
     */
31
    private $defaultCacheFolder;
32
33 1
    public function __construct($defaultCacheFolder, ?BrowscapCacheInterface $cache = null)
34
    {
35 1
        $this->defaultCacheFolder = $defaultCacheFolder;
36 1
        $this->cache = $cache;
37
38 1
        parent::__construct();
39 1
    }
40
41 1
    protected function configure() : void
42
    {
43
        $this
44 1
            ->setName('browscap:update')
45 1
            ->setDescription('Fetches an updated INI file for Browscap and overwrites the current PHP file.')
46 1
            ->addOption(
47 1
                'remote-file',
48 1
                'r',
49 1
                InputOption::VALUE_OPTIONAL,
50
                'browscap.ini file to download from remote location (possible values are: ' . IniLoader::PHP_INI_LITE
51 1
                . ', ' . IniLoader::PHP_INI . ', ' . IniLoader::PHP_INI_FULL . ')',
52 1
                IniLoader::PHP_INI
53
            )
54 1
            ->addOption(
55 1
                'no-backup',
56 1
                null,
57 1
                InputOption::VALUE_NONE,
58 1
                'Do not backup the previously existing file'
59
            )
60 1
            ->addOption(
61 1
                'debug',
62 1
                'd',
63 1
                InputOption::VALUE_NONE,
64 1
                'Should the debug mode entered?'
65
            )
66 1
            ->addOption(
67 1
                'cache',
68 1
                'c',
69 1
                InputOption::VALUE_OPTIONAL,
70 1
                'Where the cache files are located',
71 1
                $this->defaultCacheFolder
72
            );
73 1
    }
74
75
    protected function execute(InputInterface $input, OutputInterface $output) : void
76
    {
77
        $loggerHelper = new LoggerHelper();
78
        $logger       = $loggerHelper->create($input->getOption('debug'));
79
80
        $logger->info('started updating cache with remote file');
81
82
        $browscap = new BrowscapUpdater();
83
84
        $browscap->setLogger($logger);
85
        $browscap->setCache($this->getCache($input));
86
        $browscap->update($input->getOption('remote-file'));
87
88
        $logger->info('finished updating cache with remote file');
89
    }
90
91
    private function getCache(InputInterface $input) : BrowscapCacheInterface
92
    {
93
        if (null === $this->cache) {
94
            $cacheAdapter = new File([File::DIR => $input->getOption('cache')]);
95
            $this->cache  = new BrowscapCache($cacheAdapter);
96
        }
97
98
        return $this->cache;
99
    }
100
}
101