1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace BrowscapPHP\Command; |
5
|
|
|
|
6
|
|
|
use BrowscapPHP\Browscap; |
7
|
|
|
use BrowscapPHP\Helper\LoggerHelper; |
8
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
9
|
|
|
use Roave\DoctrineSimpleCache\SimpleCacheAdapter; |
10
|
|
|
use Symfony\Component\Console\Command\Command; |
11
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* commands to parse a given useragent |
18
|
|
|
*/ |
19
|
|
|
class ParserCommand extends Command |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $defaultCacheFolder; |
25
|
|
|
|
26
|
|
|
public function __construct(string $defaultCacheFolder) |
27
|
|
|
{ |
28
|
|
|
$this->defaultCacheFolder = $defaultCacheFolder; |
29
|
|
|
|
30
|
|
|
parent::__construct(); |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
View Code Duplication |
protected function configure() : void |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$this |
36
|
1 |
|
->setName('browscap:parse') |
37
|
1 |
|
->setDescription('Parses a user agent string and dumps the results.') |
38
|
1 |
|
->addArgument( |
39
|
1 |
|
'user-agent', |
40
|
1 |
|
InputArgument::REQUIRED, |
41
|
1 |
|
'User agent string to analyze', |
42
|
1 |
|
null |
43
|
|
|
) |
44
|
1 |
|
->addOption( |
45
|
1 |
|
'cache', |
46
|
1 |
|
'c', |
47
|
1 |
|
InputOption::VALUE_OPTIONAL, |
48
|
1 |
|
'Where the cache files are located', |
49
|
1 |
|
$this->defaultCacheFolder |
50
|
|
|
); |
51
|
1 |
|
} |
52
|
|
|
|
53
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) : void |
54
|
|
|
{ |
55
|
|
|
$logger = LoggerHelper::createDefaultLogger($output); |
56
|
|
|
|
57
|
|
|
$fileCache = new FilesystemCache($input->getOption('cache')); |
58
|
|
|
$cache = new SimpleCacheAdapter($fileCache); |
59
|
|
|
|
60
|
|
|
$browscap = new Browscap($cache, $logger); |
61
|
|
|
|
62
|
|
|
$result = $browscap->getBrowser($input->getArgument('user-agent')); |
63
|
|
|
|
64
|
|
|
$output->writeln(json_encode($result, JSON_PRETTY_PRINT)); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
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.