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 |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
$output->writeln(sprintf('converting file %s', $file)); |
88
|
|
|
|
89
|
|
|
try { |
90
|
|
|
$browscap->convertFile($file); |
91
|
|
|
} catch (Exception\FileNameMissingException $e) { |
92
|
|
|
$logger->debug($e); |
93
|
|
|
|
94
|
|
|
return 6; |
95
|
|
|
} catch (Exception\FileNotFoundException $e) { |
96
|
|
|
$logger->debug($e); |
97
|
|
|
|
98
|
|
|
return 7; |
99
|
|
|
} catch (Exception\ErrorReadingFileException $e) { |
100
|
|
|
$logger->debug($e); |
101
|
|
|
|
102
|
|
|
return 8; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$logger->info('finished converting local file'); |
106
|
|
|
|
107
|
|
|
return 0; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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.