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

ConvertCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 112
Duplicated Lines 30.36 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 34
loc 112
ccs 43
cts 43
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setCache() 0 6 1
A execute() 0 20 2
B configure() 25 25 1
A getCache() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\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
 * command to convert a downloaded Browscap ini file and write it to the cache
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 ConvertCommand extends Command
55
{
56
    /**
57
     * @var BrowscapCacheInterface
58
     */
59
    private $cache = null;
60
61
    /**
62
     * @var string
63
     */
64
    private $defaultIniFile;
65
66
    /**
67
     * @var string
68
     */
69
    private $defaultCacheFolder;
70
71
    /**
72
     * @param string $defaultCacheFolder
73
     * @param string $defaultIniFile
74
     */
75 2
    public function __construct($defaultCacheFolder, $defaultIniFile)
76
    {
77 2
        $this->defaultCacheFolder = $defaultCacheFolder;
78 2
        $this->defaultIniFile     = $defaultIniFile;
79
80 2
        parent::__construct();
81 2
    }
82
83
    /**
84
     * @param \BrowscapPHP\Cache\BrowscapCacheInterface $cache
85
     *
86
     * @return $this
87
     */
88 2
    public function setCache(BrowscapCacheInterface $cache)
89
    {
90 2
        $this->cache = $cache;
91
92 2
        return $this;
93
    }
94
95
    /**
96
     * Configures the current command.
97
     */
98 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...
99
    {
100
        $this
101 2
            ->setName('browscap:convert')
102 2
            ->setDescription('Converts an existing browscap.ini file to a cache.php file.')
103 2
            ->addArgument(
104 2
                'file',
105 2
                InputArgument::OPTIONAL,
106 2
                'Path to the browscap.ini file',
107 2
                $this->defaultIniFile
108
            )
109 2
            ->addOption(
110 2
                'debug',
111 2
                'd',
112 2
                InputOption::VALUE_NONE,
113 2
                'Should the debug mode entered?'
114
            )
115 2
            ->addOption(
116 2
                'cache',
117 2
                'c',
118 2
                InputOption::VALUE_OPTIONAL,
119 2
                'Where the cache files are located',
120 2
                $this->defaultCacheFolder
121
            );
122 2
    }
123
124
    /**
125
     * @param InputInterface  $input
126
     * @param OutputInterface $output
127
     *
128
     * @return int|null|void
129
     */
130 1
    protected function execute(InputInterface $input, OutputInterface $output)
131
    {
132 1
        $loggerHelper = new LoggerHelper();
133 1
        $logger       = $loggerHelper->create($input->getOption('debug'));
134
135 1
        $logger->info('initializing converting process');
136
137 1
        $browscap = new BrowscapUpdater();
138
139 1
        $browscap->setLogger($logger);
140 1
        $browscap->setCache($this->getCache($input));
141
142 1
        $logger->info('started converting local file');
143
144 1
        $file = ($input->getArgument('file') ? $input->getArgument('file') : ($this->defaultIniFile));
145
146 1
        $browscap->convertFile($file);
147
148 1
        $logger->info('finished converting local file');
149 1
    }
150
151
    /**
152
     * @param InputInterface $input
153
     *
154
     * @return BrowscapCacheInterface
155
     */
156 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...
157
    {
158 1
        if (null === $this->cache) {
159
            $cacheAdapter = new File([File::DIR => $input->getOption('cache')]);
160
            $this->cache  = new BrowscapCache($cacheAdapter);
161
        }
162
163 1
        return $this->cache;
164
    }
165
}
166