UpdateRunner::checkParams()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 22
ccs 11
cts 11
cp 1
rs 9.4888
cc 5
nc 5
nop 1
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Vasoft\VersionIncrement\Core;
6
7
use Vasoft\VersionIncrement\Config;
8
use Vasoft\VersionIncrement\SemanticVersionUpdater;
9
use Vasoft\VersionIncrement\Contract\ApplicationHandlerInterface;
10
use Vasoft\VersionIncrement\Exceptions\BranchException;
11
use Vasoft\VersionIncrement\Exceptions\ChangelogException;
12
use Vasoft\VersionIncrement\Exceptions\ComposerException;
13
use Vasoft\VersionIncrement\Exceptions\GitCommandException;
14
use Vasoft\VersionIncrement\Exceptions\IncorrectChangeTypeException;
15
use Vasoft\VersionIncrement\Exceptions\UncommittedException;
16
17
class UpdateRunner implements ApplicationHandlerInterface
18
{
19
    public const KEY_DEBUG = '--debug';
20
    public const KEY_NO_COMMIT = '--no-commit';
21
    private bool $debug = false;
22
    private bool $doCommit = true;
23
24 9
    public function __construct(
25
        private readonly string $composerJsonPath,
26
        private readonly Config $config,
27 9
    ) {}
28
29
    /**
30
     * @throws BranchException
31
     * @throws ChangelogException
32
     * @throws ComposerException
33
     * @throws GitCommandException
34
     * @throws IncorrectChangeTypeException
35
     * @throws UncommittedException
36
     */
37 4
    public function handle(array $argv): ?int
38
    {
39 4
        $changeType = $this->checkParams($argv);
40 4
        (new SemanticVersionUpdater($this->composerJsonPath, $this->config, $changeType, $this->doCommit))
41 4
            ->setDebug($this->debug)
42 4
            ->updateVersion();
43
44 4
        return null;
45
    }
46
47 4
    private function checkParams(array $argv): string
48
    {
49 4
        $result = '';
50
51 4
        foreach ($argv as $arg) {
52
            switch ($arg) {
53
                case self::KEY_DEBUG:
54 1
                    $this->debug = true;
55 1
                    break;
56
                case self::KEY_NO_COMMIT:
57 1
                    $this->doCommit = false;
58 1
                    break;
59
60
                default:
61 2
                    if (empty($this->changeType)) {
62 2
                        $result = $arg;
63
                    }
64 2
                    break;
65
            }
66
        }
67
68 4
        return $result;
69
    }
70
71 1
    public function getHelp(): array
72
    {
73 1
        return [
74 1
            new HelpRow(Help::SECTION_KEYS, self::KEY_DEBUG, 'Enable debug mode'),
75 1
            new HelpRow(
76 1
                Help::SECTION_KEYS,
77 1
                self::KEY_NO_COMMIT,
78 1
                'Execute all file updates (e.g., CHANGELOG.md, composer.json) but skip creating the final Git commit and version tag.',
79 1
            ),
80 1
            new HelpRow(
81 1
                Help::SECTION_TYPES,
82 1
                implode('|', SemanticVersionUpdater::$availableTypes),
83 1
                'Updates version according to the passed type',
84 1
            ),
85 1
        ];
86
    }
87
}
88