Passed
Push — master ( 5867a7...07e920 )
by Alexander
02:02
created

Application::displayList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Vasoft\VersionIncrement;
6
7
use Vasoft\VersionIncrement\Core\Help;
8
use Vasoft\VersionIncrement\Core\Legend;
9
use Vasoft\VersionIncrement\Core\UpdateRunner;
10
use Vasoft\VersionIncrement\Exceptions\ApplicationException;
11
use Vasoft\VersionIncrement\Exceptions\InvalidConfigFileException;
12
13
class Application
14
{
15 10
    public function run(array $argv): int
16
    {
17 10
        array_shift($argv);
18
19
        try {
20 10
            $composerJsonPath = getenv('COMPOSER') ?: getcwd();
21 9
            $configFile = $composerJsonPath . '/.vs-version-increment.php';
22 9
            $config = $this->loadConfig($configFile);
23 8
            $helper = new Help();
24 8
            $handlers = [
25 8
                $helper,
26 8
                new Legend($config),
27 8
                new UpdateRunner($composerJsonPath, $config),
28 8
            ];
29 8
            $helper->registerHandlersFromArray($handlers);
30 8
            foreach ($handlers as $handler) {
31 8
                if (($exitCode = $handler->handle($argv)) !== null) {
32 5
                    return $exitCode;
33
                }
34
            }
35
36 3
            return 0;
37 2
        } catch (ApplicationException $e) {
38 1
            fwrite(STDERR, 'Error: ' . $e->getMessage() . PHP_EOL);
39
40 1
            return $e->getCode();
41 1
        } catch (\Throwable $e) {
42 1
            fwrite(STDERR, 'Error: ' . $e->getMessage() . PHP_EOL);
43
44 1
            return ApplicationException::CODE;
45
        }
46
    }
47
48
    /**
49
     * @throws InvalidConfigFileException
50
     */
51 9
    private function loadConfig(string $configFile): Config
52
    {
53 9
        if (file_exists($configFile)) {
54 7
            $config = include $configFile;
55 7
            if (!$config instanceof Config) {
56 7
                throw  new InvalidConfigFileException();
57
            }
58
        } else {
59 2
            $config = new Config();
60
        }
61
62 8
        return $config;
63
    }
64
}
65