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

BaseCliApplication::getArgument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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