Completed
Pull Request — master (#157)
by Thomas
13:25
created

ConvertCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
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\LoggerHelper;
37
use Symfony\Component\Console\Command\Command;
38
use Symfony\Component\Console\Input\InputArgument;
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 convert a downloaded Browscap ini file and write it to the cache
46
 *
47
 * @category   Browscap-PHP
48
 * @package    Command
49
 * @author     Dave Olsen, http://dmolsen.com
50
 * @author     Thomas Müller <[email protected]>
51
 * @copyright  Copyright (c) 1998-2015 Browser Capabilities Project
52
 * @version    3.0
53
 * @license    http://www.opensource.org/licenses/MIT MIT License
54
 * @link       https://github.com/browscap/browscap-php/
55
 */
56
class ConvertCommand extends Command
57
{
58
    /**
59
     * @var BrowscapCacheInterface
60
     */
61
    private $cache = null;
62
63
    /**
64
     * @var string
65
     */
66
    private $defaultIniFile;
67
68
    /**
69
     * @var string
70
     */
71
    private $defaultCacheFolder;
72
73
    /**
74
     * @param string $defaultCacheFolder
75
     * @param string $defaultIniFile
76
     */
77 2
    public function __construct($defaultCacheFolder, $defaultIniFile)
78
    {
79 2
        $this->defaultCacheFolder = $defaultCacheFolder;
80 2
        $this->defaultIniFile     = $defaultIniFile;
81
82 2
        parent::__construct();
83 2
    }
84
85
    /**
86
     * @param \BrowscapPHP\Cache\BrowscapCacheInterface $cache
87
     *
88
     * @return $this
89
     */
90 2
    public function setCache(BrowscapCacheInterface $cache)
91
    {
92 2
        $this->cache = $cache;
93
94 2
        return $this;
95
    }
96
97
    /**
98
     * Configures the current command.
99
     */
100 2
    protected function configure()
101
    {
102 2
        $this
103 2
            ->setName('browscap:convert')
104 2
            ->setDescription('Converts an existing browscap.ini file to a cache.php file.')
105 2
            ->addArgument(
106 2
                'file',
107 2
                InputArgument::OPTIONAL,
108 2
                'Path to the browscap.ini file',
109 2
                $this->defaultIniFile
110 2
            )
111 2
            ->addOption(
112 2
                'debug',
113 2
                'd',
114 2
                InputOption::VALUE_NONE,
115
                'Should the debug mode entered?'
116 2
            )
117 2
            ->addOption(
118 2
                'cache',
119 2
                'c',
120 2
                InputOption::VALUE_OPTIONAL,
121 2
                'Where the cache files are located',
122 2
                $this->defaultCacheFolder
123 2
            )
124
        ;
125 2
    }
126
127
    /**
128
     * @param InputInterface  $input
129
     * @param OutputInterface $output
130
     *
131
     * @return int|null|void
132
     */
133 1
    protected function execute(InputInterface $input, OutputInterface $output)
134
    {
135 1
        $loggerHelper = new LoggerHelper();
136 1
        $logger       = $loggerHelper->create($input->getOption('debug'));
137
138 1
        $logger->info('initializing converting process');
139
140 1
        $browscap = new BrowscapUpdater();
141
142
        $browscap
143 1
            ->setLogger($logger)
144 1
            ->setCache($this->getCache($input))
145
        ;
146
147 1
        $logger->info('started converting local file');
148
149 1
        $file = ($input->getArgument('file') ? $input->getArgument('file') : ($this->defaultIniFile));
150
151 1
        $browscap->convertFile($file);
152
153 1
        $logger->info('finished converting local file');
154 1
    }
155
156
    /**
157
     * @param InputInterface $input
158
     *
159
     * @return BrowscapCacheInterface
160
     */
161 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...
162
    {
163 1
        if (null === $this->cache) {
164
            $cacheAdapter = new File(array(File::DIR => $input->getOption('cache')));
165
            $this->cache  = new BrowscapCache($cacheAdapter);
166
        }
167
168 1
        return $this->cache;
169
    }
170
}
171