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