Test Failed
Push — master ( 577caa...8d4ce1 )
by butschster
09:23
created

CommandDeclaration::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 9
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 6
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Scaffolder\Declaration;
6
7
use Spiral\Console\Attribute\Argument;
8
use Spiral\Console\Attribute\AsCommand;
9
use Spiral\Console\Attribute\Option;
10
use Spiral\Console\Attribute\Question;
11
use Spiral\Console\Command;
12
use Spiral\Scaffolder\Config\ScaffolderConfig;
13 4
14
class CommandDeclaration extends AbstractDeclaration
15 4
{
16
    public const TYPE = 'command';
17
18 4
    public function __construct(
19
        ScaffolderConfig $config,
20 4
        string $name,
21
        ?string $comment = null,
22
        ?string $namespace = null,
23
        private readonly ?string $alias = null,
24
        private readonly ?string $description = null,
25
    ) {
26 4
        parent::__construct($config, $name, $comment, $namespace);
27
    }
28 4
29
    public function addArgument(string $name): void
30 4
    {
31
        $this->class
32 4
            ->addProperty($name)
33 4
            ->setPrivate()
34 4
            ->setType('string')
35 4
            ->addAttribute(Argument::class, [
36
                'description' => 'Argument description',
37 4
            ])
38 4
            ->addAttribute(Question::class, [
39 4
                'question' => \sprintf('What would you like to name the %s argument?', $name),
40 4
            ]);
41 4
    }
42
43
    public function addOption(string $name): void
44
    {
45
        $this->class
46
            ->addProperty($name)
47
            ->setPrivate()
48
            ->setType('bool')
49
            ->addAttribute(Option::class, [
50
                'description' => 'Argument description',
51
            ]);
52
    }
53
54
    /**
55
     * Declare default command body.
56
     */
57
    public function declare(): void
58
    {
59
        $this->namespace->addUse(Command::class);
60
        $this->namespace->addUse(AsCommand::class);
61
        $this->namespace->addUse(Argument::class);
62
        $this->namespace->addUse(Option::class);
63
        $this->namespace->addUse(Question::class);
64
65
        $this->class->setExtends(Command::class);
66
        $this->class->setFinal();
67
68
        $commandDefinition = [
69
            'name' => $this->alias,
70
        ];
71
72
        if ($this->description) {
73
            $commandDefinition['description'] = $this->description;
74
        }
75
76
        $this->class->addAttribute(AsCommand::class, $commandDefinition);
77
78
        $this->class
79
            ->addMethod('__invoke')
80
            ->setReturnType('int')
81
            ->setBody(
82
                <<<'PHP'
83
                // Put your command logic here
84
                $this->info('Command logic is not implemented yet');
85
86
                return self::SUCCESS;
87
                PHP,
88
            );
89
    }
90
}
91