UpdateCommand::execute()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 0
cts 7
cp 0
rs 9.344
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 20
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\Helper\Exception;
10
use BrowscapPHP\Helper\IniLoaderInterface;
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 UpdateCommand extends Command
24
{
25
    /**
26
     * @var string
27
     */
28
    private $defaultCacheFolder;
29
30
    public function __construct(string $defaultCacheFolder)
31
    {
32
        $this->defaultCacheFolder = $defaultCacheFolder;
33
34 1
        parent::__construct();
35
    }
36
37 1 View Code Duplication
    protected function configure() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38 1
    {
39 1
        $this
40 1
            ->setName('browscap:update')
41 1
            ->setDescription('Fetches an updated INI file for Browscap and overwrites the current PHP file.')
42 1
            ->addOption(
43 1
                'remote-file',
44 1
                'r',
45 1
                InputOption::VALUE_OPTIONAL,
46
                'browscap.ini file to download from remote location (possible values are: ' . IniLoaderInterface::PHP_INI_LITE
47 1
                . ', ' . IniLoaderInterface::PHP_INI . ', ' . IniLoaderInterface::PHP_INI_FULL . ')',
48 1
                IniLoaderInterface::PHP_INI
49 1
            )
50 1
            ->addOption(
51 1
                'no-backup',
52
                null,
53 1
                InputOption::VALUE_NONE,
54 1
                'Do not backup the previously existing file'
55 1
            )
56 1
            ->addOption(
57 1
                'cache',
58 1
                'c',
59
                InputOption::VALUE_OPTIONAL,
60 1
                'Where the cache files are located',
61
                $this->defaultCacheFolder
62
            );
63
    }
64
65
    protected function execute(InputInterface $input, OutputInterface $output) : int
66
    {
67
        $logger = LoggerHelper::createDefaultLogger($output);
68
69
        /** @var string $cacheOption */
70
        $cacheOption = $input->getOption('cache');
71
        $fileCache = new FilesystemCache($cacheOption);
72
        $cache = new SimpleCacheAdapter($fileCache);
73
74
        $logger->info('started updating cache with remote file');
75
76
        $browscap = new BrowscapUpdater($cache, $logger);
77
78
        /** @var string $remoteFileOption */
79
        $remoteFileOption = $input->getOption('remote-file');
80
81
        try {
82
            $browscap->update($remoteFileOption);
83
        } catch (ErrorCachedVersionException $e) {
84
            $logger->debug($e);
85
86
            return 3;
87
        } catch (FetcherException $e) {
88
            $logger->debug($e);
89
90
            return 9;
91
        } catch (Exception $e) {
92
            $logger->debug($e);
93
94
            return 10;
95
        }
96
97
        $logger->info('finished updating cache with remote file');
98
99
        return 0;
100
    }
101
}
102