ParserCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 14
CRAP Score 1.0002

Importance

Changes 0
Metric Value
dl 19
loc 19
ccs 14
cts 15
cp 0.9333
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1.0002
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
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...
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) {
0 ignored issues
show
Bug introduced by
The class ExceptionalJSON\EncodeErrorException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
86
            $logger->error($e);
87
88
            return 11;
89
        }
90
91
        return 0;
92
    }
93
}
94