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

FetchCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 73
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B configure() 0 26 1
A execute() 0 19 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\BrowscapUpdater;
33
use BrowscapPHP\Helper\IniLoader;
34
use BrowscapPHP\Helper\LoggerHelper;
35
use Symfony\Component\Console\Command\Command;
36
use Symfony\Component\Console\Input\InputArgument;
37
use Symfony\Component\Console\Input\InputInterface;
38
use Symfony\Component\Console\Input\InputOption;
39
use Symfony\Component\Console\Output\OutputInterface;
40
41
/**
42
 * command to fetch a browscap ini file from the remote host and store the content in a local file
43
 *
44
 * @category   Browscap-PHP
45
 * @author     Dave Olsen, http://dmolsen.com
46
 * @author     Thomas Müller <[email protected]>
47
 * @copyright  Copyright (c) 1998-2015 Browser Capabilities Project
48
 * @version    3.0
49
 * @license    http://www.opensource.org/licenses/MIT MIT License
50
 * @link       https://github.com/browscap/browscap-php/
51
 */
52
class FetchCommand extends Command
53
{
54
    /**
55
     * @var string
56
     */
57
    private $defaultIniFile;
58
59
    /**
60
     * @param string $defaultIniFile
61
     */
62 1
    public function __construct($defaultIniFile)
63
    {
64 1
        $this->defaultIniFile = $defaultIniFile;
65
66 1
        parent::__construct();
67 1
    }
68
69
    /**
70
     * Configures the current command.
71
     */
72 1
    protected function configure()
73
    {
74
        $this
75 1
            ->setName('browscap:fetch')
76 1
            ->setDescription('Fetches an updated INI file for Browscap.')
77 1
            ->addArgument(
78 1
                'file',
79 1
                InputArgument::OPTIONAL,
80 1
                'browscap.ini file',
81 1
                $this->defaultIniFile
82
            )
83 1
            ->addOption(
84 1
                'remote-file',
85 1
                'r',
86 1
                InputOption::VALUE_OPTIONAL,
87 1
                'browscap.ini file to download from remote location (possible values are: ' . IniLoader::PHP_INI_LITE
88 1
                . ', ' . IniLoader::PHP_INI . ', ' . IniLoader::PHP_INI_FULL . ')',
89 1
                IniLoader::PHP_INI
90
            )
91 1
            ->addOption(
92 1
                'debug',
93 1
                'd',
94 1
                InputOption::VALUE_NONE,
95 1
                'Should the debug mode entered?'
96
            );
97 1
    }
98
99
    /**
100
     * @param InputInterface  $input
101
     * @param OutputInterface $output
102
     *
103
     * @return int|null|void
104
     */
105
    protected function execute(InputInterface $input, OutputInterface $output)
106
    {
107
        $loggerHelper = new LoggerHelper();
108
        $logger       = $loggerHelper->create($input->getOption('debug'));
109
110
        $file = $input->getArgument('file');
111
        if (!$file) {
112
            $file = $this->defaultIniFile;
113
        }
114
115
        $logger->info('started fetching remote file');
116
117
        $browscap = new BrowscapUpdater();
118
119
        $browscap->setLogger($logger);
120
        $browscap->fetch($file, $input->getOption('remote-file'));
121
122
        $logger->info('finished fetching remote file');
123
    }
124
}
125