Completed
Push — master ( 9a3147...33bee6 )
by Webysther
02:43
created

Base::init()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 2
dl 0
loc 10
ccs 0
cts 2
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\ShortName;
19
use Webs\Mirror\IProgressBar;
20
use Webs\Mirror\Filesystem;
21
use Webs\Mirror\Http;
22
use Webs\Mirror\Provider;
23
use Webs\Mirror\Package;
24
25
/**
26
 * Base command.
27
 *
28
 * @author Webysther Nunes <[email protected]>
29
 */
30
class Base extends Command
31
{
32
    use ShortName;
33
34
    /**
35
     * @var bool
36
     */
37
    protected $initialized = false;
38
39
    /**
40
     * @var InputInterface
41
     */
42
    protected $input;
43
44
    /**
45
     * @var OutputInterface
46
     */
47
    protected $output;
48
49
    /**
50
     * @var IProgressBar
51
     */
52
    protected $progressBar;
53
54
    /**
55
     * @var Filesystem
56
     */
57
    protected $filesystem;
58
59
    /**
60
     * @var Http
61
     */
62
    protected $http;
63
64
    /**
65
     * @var Provider
66
     */
67
    protected $provider;
68
69
    /**
70
     * @var Package
71
     */
72
    protected $package;
73
74
    /**
75
     * @var int
76
     */
77
    protected $exitCode;
78
79
    /**
80
     * @var bool
81
     */
82
    protected $verboseVerbose = false;
83
84
    /**
85
     * @var int
86
     */
87
    const VV = OutputInterface::VERBOSITY_VERBOSE;
88
89
    /**
90
     * Main files.
91
     */
92
    const MAIN = 'packages.json';
93
    const DOT = '.packages.json';
94
    const INIT = '.init';
95
    const TO = 'p';
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    protected function configure()
101
    {
102
        $this->addOption(
103
            'no-progress',
104
            null,
105
            InputOption::VALUE_NONE,
106
            "Don't show progress bar"
107
        );
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    protected function initialize(InputInterface $input, OutputInterface $output)
114
    {
115
        $this->input = $input;
116
        $this->output = $output;
117
        $this->verboseVerbose = $this->output->getVerbosity() >= self::VV;
118
    }
119
120
    /**
121
     * Inicialize with custom
122
     *
123
     * @param  InputInterface  $input
124
     * @param  OutputInterface $output
125
     * @return void
126
     */
127
    public function init(InputInterface $input, OutputInterface $output)
128
    {
129
        if(isset($this->input) && isset($this->output)){
130
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Webs\Mirror\Command\Base which is incompatible with the documented return type void.
Loading history...
131
        }
132
133
        // Only when direct call by tests
134
        $this->initialize($input, $output);
135
136
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Webs\Mirror\Command\Base which is incompatible with the documented return type void.
Loading history...
137
    }
138
139
    /**
140
     * @return bool
141
     */
142
    public function isVerbose():bool
143
    {
144
        return $this->verboseVerbose;
145
    }
146
147
    /**
148
     * Add a progress bar.
149
     *
150
     * @param IProgressBar $progressBar
151
     *
152
     * @return Base
153
     */
154
    public function setProgressBar(IProgressBar $progressBar):Base
155
    {
156
        $this->progressBar = $progressBar;
157
158
        return $this;
159
    }
160
161
    /**
162
     * Add a fileSystem.
163
     *
164
     * @param Filesystem $fileSystem
165
     *
166
     * @return Base
167
     */
168
    public function setFilesystem(Filesystem $filesystem):Base
169
    {
170
        $this->filesystem = $filesystem;
171
172
        return $this;
173
    }
174
175
    /**
176
     * Add a http.
177
     *
178
     * @param Http $http
179
     *
180
     * @return Base
181
     */
182
    public function setHttp(Http $http):Base
183
    {
184
        $this->http = $http;
185
186
        return $this;
187
    }
188
189
    /**
190
     * Add a provider.
191
     *
192
     * @param Provider $provider
193
     *
194
     * @return Base
195
     */
196
    public function setProvider(Provider $provider):Base
197
    {
198
        $this->provider = $provider;
199
200
        return $this;
201
    }
202
203
    /**
204
     * Add a packages.
205
     *
206
     * @param Package $package
207
     *
208
     * @return Base
209
     */
210
    public function setPackage(Package $package):Base
211
    {
212
        $this->package = $package;
213
214
        return $this;
215
    }
216
217
    /**
218
     * @param int $exit
219
     *
220
     * @return Base
221
     */
222
    protected function setExitCode(int $exit):Base
223
    {
224
        $this->exitCode = $exit;
225
226
        return $this;
227
    }
228
229
    /**
230
     * @return int
231
     */
232
    protected function getExitCode():int
233
    {
234
        return $this->stop() ? $this->exitCode : 0;
235
    }
236
237
    /**
238
     * @return bool
239
     */
240
    protected function stop():bool
241
    {
242
        return isset($this->exitCode);
243
    }
244
}
245