Passed
Push — master ( 1690b4...a30145 )
by Webysther
02:31
created

Base::progressBarUpdate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
ccs 0
cts 10
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Packagist Mirror.
7
 *
8
 * For the full license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webs\Mirror\Command;
13
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Webs\Mirror\Util;
19
use Dariuszp\CliProgressBar;
20
21
/**
22
 * Base command.
23
 *
24
 * @author Webysther Nunes <[email protected]>
25
 */
26
abstract class Base extends Command
27
{
28
    /**
29
     * Console params configuration.
30
     */
31
    protected function configure():void
32
    {
33
        $this->addOption(
34
            'info',
35
            null,
36
            InputOption::VALUE_NONE,
37
            'Show information about disk usage, execution time and memory usage'
38
        )
39
             ->addOption(
40
                 'no-progress',
41
                 null,
42
                 InputOption::VALUE_NONE,
43
                 'Don\'t show progress bar'
44
             );
45
    }
46
47
    /**
48
     * Execution.
49
     *
50
     * @param InputInterface  $input  Input console
51
     * @param OutputInterface $output Output console
52
     *
53
     * @return int 0 if pass, any another is error
54
     */
55
    public function execute(InputInterface $input, OutputInterface $output):int
56
    {
57
        $this->input = $input;
0 ignored issues
show
Bug Best Practice introduced by
The property input does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
58
        $this->output = $output;
0 ignored issues
show
Bug Best Practice introduced by
The property output does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
59
60
        $info = false;
61
        if ($input->getOption('info')) {
62
            $info = true;
63
            $util = new Util();
64
        }
65
66
        $this->determineMode();
67
68
        if ($this->childExecute($input, $output)) {
69
            return 1;
70
        }
71
72
        if ($info) {
73
            $util->showResults($input, $output);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $util does not seem to be defined for all execution paths leading up to this point.
Loading history...
74
        }
75
76
        return 0;
77
    }
78
79
    /**
80
     * Execution.
81
     *
82
     * @param InputInterface  $input  Input console
83
     * @param OutputInterface $output Output console
84
     *
85
     * @return int 0 if pass, any another is error
86
     */
87
    abstract protected function childExecute(InputInterface $input, OutputInterface $output):int;
88
89
    /**
90
     * Find hash and replace by *.
91
     *
92
     * @param string $name Name of provider or package
93
     *
94
     * @return string Shortname
95
     */
96
    protected function shortname(string $name):string
97
    {
98
        return preg_replace('/\$(\w*)/', '*', $name);
99
    }
100
101
    /**
102
     * Determine mode operation.
103
     */
104
    protected function determineMode():void
105
    {
106
        $this->hasQuiet = $this->output->isQuiet()
0 ignored issues
show
Bug Best Practice introduced by
The property hasQuiet does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
107
                            || $this->input->getOption('no-progress')
108
                            || $this->input->getOption('no-ansi');
109
    }
110
111
    /**
112
     * Start progress bar.
113
     *
114
     * @param int $total Total
115
     */
116
    protected function progressBarStart(int $total):void
117
    {
118
        if ($this->hasQuiet) {
119
            return;
120
        }
121
122
        $this->bar = new CliProgressBar($total, 0);
0 ignored issues
show
Bug Best Practice introduced by
The property bar does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
123
    }
124
125
    /**
126
     * Update progress bar.
127
     *
128
     * @param int $current Current value
129
     */
130
    protected function progressBarUpdate(int $current = 0):void
131
    {
132
        if ($this->hasQuiet) {
133
            return;
134
        }
135
136
        if ($current) {
137
            $this->bar->progress($current);
138
139
            return;
140
        }
141
142
        $this->bar->progress();
143
    }
144
145
    /**
146
     * Finish progress bar.
147
     */
148
    protected function progressBarFinish():void
149
    {
150
        if ($this->hasQuiet) {
151
            return;
152
        }
153
154
        $this->bar->end();
155
    }
156
157
    /**
158
     * Check if is gzip, if not compress.
159
     *
160
     * @param string $gzip
161
     *
162
     * @return string
163
     */
164
    protected function parseGzip(string $gzip):string
165
    {
166
        if (mb_strpos($gzip, "\x1f"."\x8b"."\x08") !== 0) {
167
            return gzencode($gzip);
168
        }
169
170
        return $gzip;
171
    }
172
173
    /**
174
     * Check if is gzip, if yes uncompress.
175
     *
176
     * @param string $gzip
177
     *
178
     * @return string
179
     */
180
    protected function unparseGzip(string $gzip):string
181
    {
182
        if (mb_strpos($gzip, "\x1f"."\x8b"."\x08") !== 0) {
183
            return $gzip;
184
        }
185
186
        return gzdecode($gzip);
187
    }
188
}
189