Passed
Push — master ( d63c85...cd60ee )
by Carlos C
04:35 queued 02:22
created

BaseCliApplication   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 49
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getArgument() 0 3 1
A getArguments() 0 3 1
A getCommand() 0 3 1
A __invoke() 0 14 3
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CfdiUtils\Development;
6
7
abstract class BaseCliApplication
8
{
9
    /** @var string */
10
    private $command;
11
12
    /** @var string[] */
13
    private $arguments;
14
15
    abstract public function printHelp(): void;
16
17
    abstract public function execute(): int;
18
19
    final public function __construct(string $command, string ...$arguments)
20
    {
21
        $this->command = $command;
22
        $this->arguments = $arguments;
23
    }
24
25
    public function __invoke(): int
26
    {
27
        if ([] !== array_intersect(['-h', '--help'], $this->arguments)) {
28
            $this->printHelp();
29
            return 0;
30
        }
31
32
        try {
33
            return $this->execute();
34
        } catch (\Throwable $exception) {
35
            file_put_contents('php://stderr', $exception->getMessage() . PHP_EOL, FILE_APPEND);
36
            // file_put_contents('php://stderr', get_class($exception) . ': ' . $exception->getMessage() . PHP_EOL, FILE_APPEND);
37
            // file_put_contents('php://stderr', $exception->getTraceAsString() . PHP_EOL, FILE_APPEND);
38
            return min(1, $exception->getCode());
39
        }
40
    }
41
42
    public function getCommand(): string
43
    {
44
        return $this->command;
45
    }
46
47
    /** @return string[] */
48
    public function getArguments(): array
49
    {
50
        return $this->arguments;
51
    }
52
53
    public function getArgument(int $index): string
54
    {
55
        return $this->arguments[$index] ?? '';
56
    }
57
}
58