Completed
Pull Request — master (#155)
by Thomas
12:04
created

UpdateCommand::getBrowscap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
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
 * @package    Command
25
 * @copyright  1998-2015 Browser Capabilities Project
26
 * @license    http://www.opensource.org/licenses/MIT MIT License
27
 * @link       https://github.com/browscap/browscap-php/
28
 * @since      added with version 3.0
29
 */
30
31
namespace BrowscapPHP\Command;
32
33
use BrowscapPHP\BrowscapUpdater;
34
use BrowscapPHP\Cache\BrowscapCache;
35
use BrowscapPHP\Cache\BrowscapCacheInterface;
36
use BrowscapPHP\Helper\IniLoader;
37
use BrowscapPHP\Helper\LoggerHelper;
38
use Symfony\Component\Console\Command\Command;
39
use Symfony\Component\Console\Input\InputInterface;
40
use Symfony\Component\Console\Input\InputOption;
41
use Symfony\Component\Console\Output\OutputInterface;
42
use WurflCache\Adapter\File;
43
44
/**
45
 * command to fetch a browscap ini file from the remote host, convert it into an array and store the content in a local
46
 * file
47
 *
48
 * @category   Browscap-PHP
49
 * @package    Command
50
 * @author     Dave Olsen, http://dmolsen.com
51
 * @author     Thomas Müller <[email protected]>
52
 * @copyright  Copyright (c) 1998-2015 Browser Capabilities Project
53
 * @version    3.0
54
 * @license    http://www.opensource.org/licenses/MIT MIT License
55
 * @link       https://github.com/browscap/browscap-php/
56
 */
57
class UpdateCommand extends Command
58
{
59
    /**
60
     * @var BrowscapCacheInterface
61
     */
62
    private $cache = null;
63
64
    /**
65
     * @var string
66
     */
67
    private $defaultCacheFolder;
68
69
    /**
70
     * @param string $defaultCacheFolder
71
     */
72 1
    public function __construct($defaultCacheFolder)
73
    {
74 1
        $this->defaultCacheFolder = $defaultCacheFolder;
75
76 1
        parent::__construct();
77 1
    }
78
79
    /**
80
     * @param \BrowscapPHP\Cache\BrowscapCacheInterface $cache
81
     *
82
     * @return $this
83
     */
84 1
    public function setCache(BrowscapCacheInterface $cache)
85
    {
86 1
        $this->cache = $cache;
87
88 1
        return $this;
89
    }
90
91
    /**
92
     * Configures the current command.
93
     */
94 1
    protected function configure()
95
    {
96 1
        $this
97 1
            ->setName('browscap:update')
98 1
            ->setDescription('Fetches an updated INI file for Browscap and overwrites the current PHP file.')
99 1
            ->addOption(
100 1
                'remote-file',
101 1
                'r',
102 1
                InputOption::VALUE_OPTIONAL,
103
                'browscap.ini file to download from remote location (possible values are: ' . IniLoader::PHP_INI_LITE
104 1
                . ', ' . IniLoader::PHP_INI . ', ' . IniLoader::PHP_INI_FULL . ')',
105
                IniLoader::PHP_INI
106 1
            )
107 1
            ->addOption(
108 1
                'no-backup',
109 1
                null,
110 1
                InputOption::VALUE_NONE,
111
                'Do not backup the previously existing file'
112 1
            )
113 1
            ->addOption(
114 1
                'debug',
115 1
                'd',
116 1
                InputOption::VALUE_NONE,
117
                'Should the debug mode entered?'
118 1
            )
119 1
            ->addOption(
120 1
                'cache',
121 1
                'c',
122 1
                InputOption::VALUE_OPTIONAL,
123 1
                'Where the cache files are located',
124 1
                $this->defaultCacheFolder
125 1
            )
126
        ;
127 1
    }
128
129
    /**
130
     * @param InputInterface  $input
131
     * @param OutputInterface $output
132
     *
133
     * @return int|null|void
134
     */
135 View Code Duplication
    protected function execute(InputInterface $input, OutputInterface $output)
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...
136
    {
137
        $loggerHelper = new LoggerHelper();
138
        $logger       = $loggerHelper->create($input->getOption('debug'));
139
140
        $logger->info('started updating cache with remote file');
141
142
        $browscap = new BrowscapUpdater();
143
144
        $browscap
145
            ->setLogger($logger)
146
            ->setCache($this->getCache($input))
147
            ->update($input->getOption('remote-file'))
148
        ;
149
150
        $logger->info('finished updating cache with remote file');
151
    }
152
153
    /**
154
     * @param InputInterface $input
155
     *
156
     * @return BrowscapCacheInterface
157
     */
158 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...
159
    {
160
        if (null === $this->cache) {
161
            $cacheAdapter = new File(array(File::DIR => $input->getOption('cache')));
162
            $this->cache  = new BrowscapCache($cacheAdapter);
163
        }
164
165
        return $this->cache;
166
    }
167
}
168