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

CheckUpdateCommand::setCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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\LoggerHelper;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use WurflCache\Adapter\File;
15
16
/**
17
 * Command to fetch a browscap ini file from the remote host, convert it into an array and store the content in a local
18
 * file
19
 */
20
class CheckUpdateCommand extends Command
21
{
22
    /**
23
     * @var ?BrowscapCacheInterface
24
     */
25
    private $cache;
26
27
    /**
28
     * @var string
29
     */
30
    private $defaultCacheFolder;
31
32 1
    public function __construct(string $defaultCacheFolder, ?BrowscapCacheInterface $cache = null)
33
    {
34 1
        $this->defaultCacheFolder = $defaultCacheFolder;
35 1
        $this->cache = $cache;
36
37 1
        parent::__construct();
38 1
    }
39
40 1
    protected function configure() : void
41
    {
42
        $this
43 1
            ->setName('browscap:check-update')
44 1
            ->setDescription('Checks if an updated INI file is available.')
45 1
            ->addOption(
46 1
                'debug',
47 1
                'd',
48 1
                InputOption::VALUE_NONE,
49 1
                'Should the debug mode entered?'
50
            )
51 1
            ->addOption(
52 1
                'cache',
53 1
                'c',
54 1
                InputOption::VALUE_OPTIONAL,
55 1
                'Where the cache files are located',
56 1
                $this->defaultCacheFolder
57
            );
58 1
    }
59
60
    protected function execute(InputInterface $input, OutputInterface $output) : void
61
    {
62
        $loggerHelper = new LoggerHelper();
63
        $logger       = $loggerHelper->create($input->getOption('debug'));
64
65
        $logger->debug('started checking for new version of remote file');
66
67
        $browscap = new BrowscapUpdater();
68
69
        $browscap->setLogger($logger);
70
        $browscap->setCache($this->getCache($input));
71
        $browscap->checkUpdate();
72
73
        $logger->debug('finished checking for new version of remote file');
74
    }
75
76
    private function getCache(InputInterface $input) : BrowscapCacheInterface
77
    {
78
        if (null === $this->cache) {
79
            $cacheAdapter = new File([File::DIR => $input->getOption('cache')]);
80
            $this->cache  = new BrowscapCache($cacheAdapter);
81
        }
82
83
        return $this->cache;
84
    }
85
}
86