Passed
Push — master ( b7b35d...140698 )
by Alexander
02:25
created

Application   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
eloc 76
c 1
b 0
f 0
dl 0
loc 119
ccs 62
cts 62
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 33 6
A checkParams() 0 29 6
A displayList() 0 12 2
A loadConfig() 0 12 3
A displayHelp() 0 3 1
A formatList() 0 8 2
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 10
    public function run(array $argv): int
18
    {
19 10
        $exitCode = 0;
20
21
        try {
22 10
            $this->checkParams($argv);
23 10
            if ($this->modeHelp) {
24 1
                $this->displayHelp();
25
26 1
                return 0;
27
            }
28
29 9
            $composerJsonPath = getenv('COMPOSER') ?: getcwd();
30 8
            $configFile = $composerJsonPath . '/.vs-version-increment.php';
31 8
            $config = $this->loadConfig($configFile);
32 7
            if ($this->showList) {
33 4
                $this->displayList($config);
34
35 4
                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 4
    private function displayList(Config $config): void
53
    {
54 4
        $output = 'Available sections:' . PHP_EOL;
55 4
        $titles = $config->getSections()->getTitles();
56 4
        $output .= $this->formatList($titles);
57 4
        $scopes = $config->getScopes();
58 4
        if (!empty($scopes)) {
59 1
            $output .= PHP_EOL . 'Available scopes:' . PHP_EOL;
60 1
            $output .= $this->formatList($scopes);
61
        }
62
63 4
        fwrite(STDOUT, $output);
64
    }
65
66 4
    private function formatList(array $values): string
67
    {
68 4
        $output = '';
69 4
        foreach ($values as $key => $title) {
70 4
            $output .= '    ' . $key . ' - ' . $title . PHP_EOL;
71
        }
72
73 4
        return $output;
74
    }
75
76 10
    private function checkParams(array $argv): void
77
    {
78 10
        unset($argv[0]);
79
80 10
        $this->showList = false;
81 10
        $this->debug = false;
82 10
        $this->changeType = '';
83
84 10
        foreach ($argv as $arg) {
85
            switch ($arg) {
86 10
                case '--help':
87 1
                    $this->modeHelp = true;
88
89 1
                    return;
90
91 9
                case '--list':
92 6
                    $this->showList = true;
93
94 6
                    return;
95
96 3
                case '--debug':
97 1
                    $this->debug = true;
98 1
                    break;
99
100
                default:
101 2
                    if (empty($this->changeType)) {
102 2
                        $this->changeType = $arg;
103
                    }
104 2
                    break;
105
            }
106
        }
107
    }
108
109
    /**
110
     * @throws InvalidConfigFileException
111
     */
112 8
    private function loadConfig(string $configFile): Config
113
    {
114 8
        if (file_exists($configFile)) {
115 6
            $config = include $configFile;
116 6
            if (!$config instanceof Config) {
117 6
                throw  new InvalidConfigFileException();
118
            }
119
        } else {
120 2
            $config = new Config();
121
        }
122
123 7
        return $config;
124
    }
125
126
    private function displayHelp(): void
127
    {
128
        echo <<<'HELP'
129
            Vasoft Semantic Version Increment
130
            run vs-version-increment [--debug] [--list] [--help] [major|minor|patch]
131
            Usage:
132
                --debug   Enable debug mode
133
                --help    Display this help message
134
                --list    Show list of sections
135
                major|minor|patch    Increment type
136
137
            HELP;
138
    }
139
}
140