Passed
Push — master ( 351308...878789 )
by Alexander
02:24 queued 10s
created

Application::loadConfig()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Vasoft\VersionIncrement;
6
7
use Vasoft\VersionIncrement\Exceptions\ApplicationException;
8
use Vasoft\VersionIncrement\Exceptions\InvalidConfigFileException;
9
10
class Application
11
{
12
    private bool $debug = false;
13
    private bool $showList = false;
14
    private bool $modeHelp = false;
15
    private string $changeType = '';
16
17 9
    public function run(array $argv): int
18
    {
19 9
        $exitCode = 0;
20
21
        try {
22 9
            $this->checkParams($argv);
23 9
            if ($this->modeHelp) {
24 1
                $this->displayHelp();
25
26 1
                return 0;
27
            }
28
29 8
            $composerJsonPath = getenv('COMPOSER') ?: getcwd();
30 7
            $configFile = $composerJsonPath . '/.vs-version-increment.php';
31 7
            $config = $this->loadConfig($configFile);
32 6
            if ($this->showList) {
33 3
                fwrite(STDOUT, implode(PHP_EOL, $config->getSectionDescriptions()) . PHP_EOL);
34
35 3
                return 0;
36
            }
37 3
            $versionUpdater = new SemanticVersionUpdater($composerJsonPath, $config, $this->changeType);
38 3
            $versionUpdater
39 3
                ->setDebug($this->debug)
40 3
                ->updateVersion();
41 2
        } catch (ApplicationException $e) {
42 1
            fwrite(STDERR, 'Error: ' . $e->getMessage() . PHP_EOL);
43 1
            $exitCode = $e->getCode();
44 1
        } catch (\Throwable $e) {
45 1
            fwrite(STDERR, 'Error: ' . $e->getMessage() . PHP_EOL);
46 1
            $exitCode = ApplicationException::CODE;
47
        }
48
49 5
        return $exitCode;
50
    }
51
52 9
    private function checkParams(array $argv): void
53
    {
54 9
        unset($argv[0]);
55
56 9
        $this->showList = false;
57 9
        $this->debug = false;
58 9
        $this->changeType = '';
59
60 9
        foreach ($argv as $arg) {
61
            switch ($arg) {
62 9
                case '--help':
63 1
                    $this->modeHelp = true;
64
65 1
                    return;
66
67 8
                case '--list':
68 5
                    $this->showList = true;
69
70 5
                    return;
71
72 3
                case '--debug':
73 1
                    $this->debug = true;
74 1
                    break;
75
76
                default:
77 2
                    if (empty($this->changeType)) {
78 2
                        $this->changeType = $arg;
79
                    }
80 2
                    break;
81
            }
82
        }
83
    }
84
85
    /**
86
     * @throws InvalidConfigFileException
87
     */
88 7
    private function loadConfig(string $configFile): Config
89
    {
90 7
        if (file_exists($configFile)) {
91 5
            $config = include $configFile;
92 5
            if (!$config instanceof Config) {
93 5
                throw  new InvalidConfigFileException();
94
            }
95
        } else {
96 2
            $config = new Config();
97
        }
98
99 6
        return $config;
100
    }
101
102
    private function displayHelp(): void
103
    {
104
        echo <<<'HELP'
105
            Vasoft Semantic Version Increment
106
            run vs-version-increment [--debug] [--list] [--help] [major|minor|patch]
107
            Usage:
108
                --debug   Enable debug mode
109
                --help    Display this help message
110
                --list    Show list of sections
111
                major|minor|patch    Increment type
112
113
            HELP;
114
    }
115
}
116