Completed
Pull Request — master (#186)
by Jay
05:38
created

ParserCommand::getCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 3
cts 3
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Copyright (c) 1998-2015 Browser Capabilities Project
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * copy of this software and associated documentation files (the "Software"),
7
 * to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included
13
 * in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @category   Browscap-PHP
24
 * @copyright  1998-2015 Browser Capabilities Project
25
 * @license    http://www.opensource.org/licenses/MIT MIT License
26
 * @link       https://github.com/browscap/browscap-php/
27
 * @since      added with version 3.0
28
 */
29
30
namespace BrowscapPHP\Command;
31
32
use BrowscapPHP\Browscap;
33
use BrowscapPHP\Cache\BrowscapCache;
34
use BrowscapPHP\Cache\BrowscapCacheInterface;
35
use BrowscapPHP\Helper\LoggerHelper;
36
use Symfony\Component\Console\Command\Command;
37
use Symfony\Component\Console\Input\InputArgument;
38
use Symfony\Component\Console\Input\InputInterface;
39
use Symfony\Component\Console\Input\InputOption;
40
use Symfony\Component\Console\Output\OutputInterface;
41
use WurflCache\Adapter\File;
42
43
/**
44
 * commands to parse a given useragent
45
 *
46
 * @category   Browscap-PHP
47
 * @author     Dave Olsen, http://dmolsen.com
48
 * @author     Thomas Müller <[email protected]>
49
 * @copyright  Copyright (c) 1998-2015 Browser Capabilities Project
50
 * @version    3.0
51
 * @license    http://www.opensource.org/licenses/MIT MIT License
52
 * @link       https://github.com/browscap/browscap-php/
53
 */
54
class ParserCommand extends Command
55
{
56
    /**
57
     * @var BrowscapCacheInterface
58
     */
59
    private $cache = null;
60
61
    /**
62
     * @var string
63
     */
64
    private $defaultCacheFolder;
65
66
    /**
67
     * @param string $defaultCacheFolder
68
     */
69 1
    public function __construct($defaultCacheFolder)
70
    {
71 1
        $this->defaultCacheFolder = $defaultCacheFolder;
72
73 1
        parent::__construct();
74 1
    }
75
76
    /**
77
     * @param \BrowscapPHP\Cache\BrowscapCacheInterface $cache
78
     *
79
     * @return $this
80
     */
81 2
    public function setCache(BrowscapCacheInterface $cache)
82
    {
83 2
        $this->cache = $cache;
84
85 2
        return $this;
86
    }
87
88
    /**
89
     * Configures the current command.
90
     */
91 2 View Code Duplication
    protected function configure()
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...
92
    {
93
        $this
94 2
            ->setName('browscap:parse')
95 2
            ->setDescription('Parses a user agent string and dumps the results.')
96 2
            ->addArgument(
97 2
                'user-agent',
98 2
                InputArgument::REQUIRED,
99 2
                'User agent string to analyze',
100 2
                null
101
            )
102 2
            ->addOption(
103 2
                'debug',
104 2
                'd',
105 2
                InputOption::VALUE_NONE,
106 2
                'Should the debug mode entered?'
107
            )
108 2
            ->addOption(
109 2
                'cache',
110 2
                'c',
111 2
                InputOption::VALUE_OPTIONAL,
112 2
                'Where the cache files are located',
113 2
                $this->defaultCacheFolder
114
            );
115 2
    }
116
117
    /**
118
     * @param InputInterface  $input
119
     * @param OutputInterface $output
120
     *
121
     * @return int|null|void
122
     */
123 1
    protected function execute(InputInterface $input, OutputInterface $output)
124
    {
125 1
        $loggerHelper = new LoggerHelper();
126 1
        $logger       = $loggerHelper->create($input->getOption('debug'));
127
128 1
        $browscap = new Browscap();
129
130
        $browscap
131 1
            ->setLogger($logger)
132 1
            ->setCache($this->getCache($input));
133
134 1
        $result = $browscap->getBrowser($input->getArgument('user-agent'));
135
136 1
        if (!defined('JSON_PRETTY_PRINT')) {
137
            // not defined in PHP 5.3
138
            define('JSON_PRETTY_PRINT', 128);
139
        }
140
141 1
        $output->writeln(json_encode($result, JSON_PRETTY_PRINT));
142 1
    }
143
144
    /**
145
     * @param InputInterface $input
146
     *
147
     * @return BrowscapCacheInterface
148
     */
149 1 View Code Duplication
    private function getCache(InputInterface $input)
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...
150
    {
151 1
        if (null === $this->cache) {
152
            $cacheAdapter = new File([File::DIR => $input->getOption('cache')]);
153
            $this->cache  = new BrowscapCache($cacheAdapter);
154
        }
155
156 1
        return $this->cache;
157
    }
158
}
159