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

ConvertCommand::execute()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 41

Duplication

Lines 41
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 41
loc 41
ccs 0
cts 9
cp 0
rs 8.9528
c 0
b 0
f 0
cc 5
nc 8
nop 2
crap 30
1
<?php
2
declare(strict_types = 1);
3
4
namespace BrowscapPHP\Command;
5
6
use BrowscapPHP\BrowscapUpdater;
7
use BrowscapPHP\Exception;
8
use BrowscapPHP\Helper\LoggerHelper;
9
use Doctrine\Common\Cache\FilesystemCache;
10
use Roave\DoctrineSimpleCache\SimpleCacheAdapter;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * Command to convert a downloaded Browscap ini file and write it to the cache
19
 */
20
class ConvertCommand extends Command
21
{
22
    /**
23
     * @var string
24
     */
25
    private $defaultIniFile;
26
27
    /**
28
     * @var string
29
     */
30
    private $defaultCacheFolder;
31
32
    public function __construct(string $defaultCacheFolder, string $defaultIniFile)
33
    {
34
        $this->defaultCacheFolder = $defaultCacheFolder;
35
        $this->defaultIniFile = $defaultIniFile;
36
37
        parent::__construct();
38
    }
39 1
40 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...
41
    {
42 1
        $this
43 1
            ->setName('browscap:convert')
44 1
            ->setDescription('Converts an existing browscap.ini file to a cache.php file.')
45 1
            ->addArgument(
46 1
                'file',
47 1
                InputArgument::OPTIONAL,
48 1
                'Path to the browscap.ini file',
49
                $this->defaultIniFile
50 1
            )
51 1
            ->addOption(
52 1
                'cache',
53 1
                'c',
54 1
                InputOption::VALUE_OPTIONAL,
55 1
                'Where the cache files are located',
56
                $this->defaultCacheFolder
57 1
            );
58
    }
59
60
    /**
61
     * @param \Symfony\Component\Console\Input\InputInterface   $input
62
     * @param \Symfony\Component\Console\Output\OutputInterface $output
63
     *
64
     * @return int
65
     */
66 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...
67
    {
68
        $logger = LoggerHelper::createDefaultLogger($output);
69
70
        /** @var string $cacheOption */
71
        $cacheOption = $input->getOption('cache');
72
        $fileCache = new FilesystemCache($cacheOption);
73
        $cache = new SimpleCacheAdapter($fileCache);
74
75
        $logger->info('initializing converting process');
76
77
        $browscap = new BrowscapUpdater($cache, $logger);
78
79
        $logger->info('started converting local file');
80
81
        /** @var string $file */
82
        $file = $input->getArgument('file');
83
        if (! $file) {
84
            $file = $this->defaultIniFile;
85
        }
86
87
        try {
88
            $browscap->convertFile($file);
89
        } catch (Exception\FileNameMissingException $e) {
90
            $logger->debug($e);
91
92
            return 6;
93
        } catch (Exception\FileNotFoundException $e) {
94
            $logger->debug($e);
95
96
            return 7;
97
        } catch (Exception\ErrorReadingFileException $e) {
98
            $logger->debug($e);
99
100
            return 8;
101
        }
102
103
        $logger->info('finished converting local file');
104
105
        return 0;
106
    }
107
}
108