CheckUpdateCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.2963

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 1
cts 3
cp 0.3333
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1.2963
1
<?php
2
declare(strict_types = 1);
3
4
namespace BrowscapPHP\Command;
5
6
use BrowscapPHP\BrowscapUpdater;
7
use BrowscapPHP\Exception\ErrorCachedVersionException;
8
use BrowscapPHP\Exception\FetcherException;
9
use BrowscapPHP\Exception\NoCachedVersionException;
10
use BrowscapPHP\Exception\NoNewVersionException;
11
use BrowscapPHP\Helper\LoggerHelper;
12
use Doctrine\Common\Cache\FilesystemCache;
13
use Roave\DoctrineSimpleCache\SimpleCacheAdapter;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * Command to fetch a browscap ini file from the remote host, convert it into an array and store the content in a local
21
 * file
22
 */
23
class CheckUpdateCommand extends Command
24
{
25
    /**
26
     * @var string
27
     */
28
    private $defaultCacheFolder;
29
30
    public function __construct(string $defaultCacheFolder)
31
    {
32
        $this->defaultCacheFolder = $defaultCacheFolder;
33 1
34
        parent::__construct();
35
    }
36 1
37 1
    protected function configure() : void
38 1
    {
39 1
        $this
40 1
            ->setName('browscap:check-update')
41 1
            ->setDescription('Checks if an updated INI file is available.')
42 1
            ->addOption(
43 1
                'cache',
44
                'c',
45 1
                InputOption::VALUE_OPTIONAL,
46
                'Where the cache files are located',
47
                $this->defaultCacheFolder
48
            );
49
    }
50
51
    /**
52
     * @param \Symfony\Component\Console\Input\InputInterface   $input
53
     * @param \Symfony\Component\Console\Output\OutputInterface $output
54
     *
55
     * @return int
56
     */
57
    protected function execute(InputInterface $input, OutputInterface $output) : int
58
    {
59
        $logger = LoggerHelper::createDefaultLogger($output);
60
61
        /** @var string $cacheOption */
62
        $cacheOption = $input->getOption('cache');
63
        $fileCache = new FilesystemCache($cacheOption);
64
        $cache = new SimpleCacheAdapter($fileCache);
65
66
        $logger->debug('started checking for new version of remote file');
67
68
        $browscap = new BrowscapUpdater($cache, $logger);
69
70
        try {
71
            $browscap->checkUpdate();
72
        } catch (NoCachedVersionException $e) {
73
            return 1;
74
        } catch (NoNewVersionException $e) {
75
            // no newer version available
76
            $logger->info('there is no newer version available');
77
78
            return 2;
79
        } catch (ErrorCachedVersionException $e) {
80
            $logger->info($e);
81
82
            return 3;
83
        } catch (FetcherException $e) {
84
            $logger->info($e);
85
86
            return 4;
87
        } catch (\Throwable $e) {
88
            $logger->info($e);
89
90
            return 5;
91
        }
92
93
        $logger->debug('finished checking for new version of remote file');
94
95
        return 0;
96
    }
97
}
98