Update::execute()   F
last analyzed

Complexity

Conditions 13
Paths 576

Size

Total Lines 67
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 67
rs 3.1641
cc 13
eloc 44
nc 576
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace Crossjoin\Browscap\Command;
5
6
use Crossjoin\Browscap\Browscap;
7
use Crossjoin\Browscap\Exception\ParserConfigurationException;
8
use Crossjoin\Browscap\Exception\ParserRuntimeException;
9
use Crossjoin\Browscap\Exception\SourceConditionNotSatisfiedException;
10
use Crossjoin\Browscap\Exception\SourceUnavailableException;
11
use Crossjoin\Browscap\Exception\UnexpectedValueException;
12
use Crossjoin\Browscap\Parser\Sqlite\Parser;
13
use Crossjoin\Browscap\PropertyFilter\Allowed;
14
use Crossjoin\Browscap\PropertyFilter\Disallowed;
15
use Crossjoin\Browscap\Source\Ini\BrowscapOrg;
16
use Crossjoin\Browscap\Source\Ini\File;
17
use Crossjoin\Browscap\Source\Ini\PhpSetting;
18
use Crossjoin\Browscap\Type;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\OutputInterface;
24
25
/**
26
 * Class Update
27
 *
28
 * @package Crossjoin\Browscap\Command
29
 * @author Christoph Ziegenberg <[email protected]>
30
 * @link https://github.com/crossjoin/browscap
31
 * @codeCoverageIgnore
32
 */
33
class Update extends Command
34
{
35
    protected function configure()
36
    {
37
        $this
38
            ->setName('update')
39
            ->setDescription('Update the parser data')
40
            ->addOption(
41
                'dir',
42
                null,
43
                InputArgument::OPTIONAL,
44
                'Directory to save parser data in (sub directory will be generated automatically).'
45
            )
46
            ->addOption(
47
                'force',
48
                null,
49
                InputOption::VALUE_NONE,
50
                'Flag to force an update although it seems to be unnecessary.'
51
            )
52
            ->addOption(
53
                'ini-php',
54
                null,
55
                InputOption::VALUE_NONE,
56
                'Will use the settings of the browscap directive in the php.ini file for the update.'
57
            )
58
            ->addOption(
59
                'ini-load',
60
                null,
61
                InputOption::VALUE_OPTIONAL,
62
                'Will download the parser data (of the given type - "lite", "full" or "standard") for the update. ' .
63
                'If no type set, the "standard" type is used.'
64
            )
65
            ->addOption(
66
                'ini-file',
67
                null,
68
                InputOption::VALUE_REQUIRED,
69
                'Will use the defined browscap ini-file for the update.'
70
            )
71
            ->addOption(
72
                'filter-allowed',
73
                null,
74
                InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
75
                'Sets a filter to define allowed properties when generating the data. Other properties will be ignored.'
76
            )
77
            ->addOption(
78
                'filter-disallowed',
79
                null,
80
                InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
81
                'Sets a filter to define disallowed properties when generating the data. Other properties are allowed.'
82
            )
83
        ;
84
    }
85
86
    /**
87
     * @param InputInterface $input
88
     * @param OutputInterface $output
89
     *
90
     * @throws UnexpectedValueException
91
     */
92
    protected function interact(InputInterface $input, OutputInterface $output)
93
    {
94
        parent::interact($input, $output);
95
96
        if (((int)$input->getOption('ini-php') + (int)$input->getOption('ini-load') + (int)$input->getOption('ini-file')) > 1) {
97
            throw new UnexpectedValueException('Please use only one --ini-* option to set the data source.');
98
        }
99
        if (((count($input->getOption('filter-allowed')) > 0) + (count($input->getOption('filter-disallowed')) > 0)) > 1) {
100
            throw new UnexpectedValueException('Please use only one --filter-* option for the data source.');
101
        }
102
    }
103
104
    /**
105
     * @param InputInterface $input
106
     * @param OutputInterface $output
107
     *
108
     * @return int|null|void
109
     * @throws ParserConfigurationException
110
     * @throws ParserRuntimeException
111
     * @throws SourceConditionNotSatisfiedException
112
     * @throws SourceUnavailableException
113
     * @throws UnexpectedValueException
114
     */
115
    protected function execute(InputInterface $input, OutputInterface $output)
116
    {
117
        $browscap = new Browscap();
118
119
        // Set source
120
        $source = null;
121
        if ($input->getOption('ini-php')) {
122
            $source = new PhpSetting();
123
        } elseif ($input->getOption('ini-load')) {
124
            $type = Type::STANDARD;
125
            switch ($input->getOption('ini-load')) {
126
                case 'lite':
127
                    $type = Type::LITE;
128
                    break;
129
                case 'full':
130
                    $type = Type::FULL;
131
                    break;
132
            }
133
            $source = new BrowscapOrg($type);
134
        } elseif ($input->getOption('ini-file')) {
135
            $file = (string)$input->getOption('ini-file');
136
            $source = new File($file);
137
        }
138
        if ($source !== null) {
139
            $browscap->getParser()->setSource($source);
140
        }
141
        
142
        // Set filter
143
        $filter = null;
144
        if ($input->getOption('filter-allowed')) {
145
            $properties = (array)$input->getOption('filter-allowed');
146
            $filter = new Allowed($properties);
147
        } elseif ($input->getOption('filter-disallowed')) {
148
            $properties = (array)$input->getOption('filter-disallowed');
149
            $filter = new Disallowed($properties);
150
        }
151
        if ($filter !== null) {
152
            $browscap->getParser()->setPropertyFilter($filter);
153
        }
154
155
        // Set data directory
156
        if ($input->getOption('dir')) {
157
            /** @var Parser $parser */
158
            $parser = $browscap->getParser();
159
            $parser->setDataDirectory((string)$input->getOption('dir'));
160
        }
161
162
        // Force an update?
163
        $force = false;
164
        if ($input->getOption('force')) {
165
            $force = true;
166
        }
167
168
        // Process update
169
        $output->writeln('Processing update...');
170
        $updated = $browscap->update($force);
171
172
        // Output result
173
        $version = $browscap->getParser()->getReader()->getVersion();
174
        $type    = $browscap->getParser()->getReader()->getType();
175
        $name    = Type::getName($type);
176
        if ($updated === true) {
177
            $output->writeln(sprintf('Browscap data successfully updated. New version: %s (%s)', $version, $name));
178
        } else {
179
            $output->writeln(sprintf('Nothing to update. Current version: %s (%s)', $version, $name));
180
        }
181
    }
182
}
183