FetchCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 27
Ratio 100 %

Code Coverage

Tests 21
CRAP Score 1

Importance

Changes 0
Metric Value
dl 27
loc 27
ccs 21
cts 22
cp 0.9545
rs 9.488
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
/**
21
 * Command to fetch a browscap ini file from the remote host and store the content in a local file
22
 */
23
class FetchCommand extends Command
24
{
25
    /**
26
     * @var string
27
     */
28
    private $defaultIniFile;
29
30
    /**
31
     * @var string
32
     */
33
    private $defaultCacheFolder;
34
35
    public function __construct(string $defaultCacheFolder, string $defaultIniFile)
36
    {
37
        $this->defaultCacheFolder = $defaultCacheFolder;
38
        $this->defaultIniFile = $defaultIniFile;
39
40 1
        parent::__construct();
41
    }
42
43 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...
44 1
    {
45 1
        $this
46 1
            ->setName('browscap:fetch')
47 1
            ->setDescription('Fetches an updated INI file for Browscap.')
48 1
            ->addArgument(
49 1
                'file',
50
                InputArgument::OPTIONAL,
51 1
                'browscap.ini file',
52 1
                $this->defaultIniFile
53 1
            )
54 1
            ->addOption(
55 1
                'remote-file',
56 1
                'r',
57 1
                InputOption::VALUE_OPTIONAL,
58
                'browscap.ini file to download from remote location (possible values are: ' . IniLoaderInterface::PHP_INI_LITE
59 1
                . ', ' . IniLoaderInterface::PHP_INI . ', ' . IniLoaderInterface::PHP_INI_FULL . ')',
60 1
                IniLoaderInterface::PHP_INI
61 1
            )
62 1
            ->addOption(
63 1
                'cache',
64 1
                'c',
65
                InputOption::VALUE_OPTIONAL,
66 1
                'Where the cache files are located',
67
                $this->defaultCacheFolder
68
            );
69
    }
70
71
    /**
72
     * @param \Symfony\Component\Console\Input\InputInterface   $input
73
     * @param \Symfony\Component\Console\Output\OutputInterface $output
74
     *
75
     * @return int
76
     */
77 View Code Duplication
    protected function execute(InputInterface $input, OutputInterface $output) : int
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...
78
    {
79
        $logger = LoggerHelper::createDefaultLogger($output);
80
81
        /** @var string $cacheOption */
82
        $cacheOption = $input->getOption('cache');
83
        $fileCache = new FilesystemCache($cacheOption);
84
        $cache = new SimpleCacheAdapter($fileCache);
85
86
        /** @var string $file */
87
        $file = $input->getArgument('file');
88
        if (! $file) {
89
            $file = $this->defaultIniFile;
90
        }
91
92
        $output->writeln(sprintf('write fetched file to %s', $file));
93
94
        $logger->info('started fetching remote file');
95
96
        $browscap = new BrowscapUpdater($cache, $logger);
97
98
        /** @var string $remoteFileOption */
99
        $remoteFileOption = $input->getOption('remote-file');
100
101
        try {
102
            $browscap->fetch($file, $remoteFileOption);
103
        } catch (ErrorCachedVersionException $e) {
104
            $logger->debug($e);
105
106
            return 3;
107
        } catch (FetcherException $e) {
108
            $logger->debug($e);
109
110
            return 9;
111
        } catch (Exception $e) {
112
            $logger->debug($e);
113
114
            return 10;
115
        }
116
117
        $logger->info('finished fetching remote file');
118
119
        return 0;
120
    }
121
}
122