Completed
Push — master ( a6622b...2986ac )
by James
23s
created

FetchCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 97
Duplicated Lines 71.13 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 56.41%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 69
loc 97
ccs 22
cts 39
cp 0.5641
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A configure() 27 27 1
B execute() 42 42 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
        $logger->info('started fetching remote file');
93
94
        $browscap = new BrowscapUpdater($cache, $logger);
95
96
        /** @var string $remoteFileOption */
97
        $remoteFileOption = $input->getOption('remote-file');
98
99
        try {
100
            $browscap->fetch($file, $remoteFileOption);
101
        } catch (ErrorCachedVersionException $e) {
102
            $logger->debug($e);
103
104
            return 3;
105
        } catch (FetcherException $e) {
106
            $logger->debug($e);
107
108
            return 9;
109
        } catch (Exception $e) {
110
            $logger->debug($e);
111
112
            return 10;
113
        }
114
115
        $logger->info('finished fetching remote file');
116
117
        return 0;
118
    }
119
}
120