Passed
Push — master ( ea2f03...45a811 )
by Andrea Marco
02:53
created

CreateCommand::getReplacements()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 3
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Cerbero\ConsoleTasker\Console\Tasks;
4
5
use Cerbero\ConsoleTasker\Console\ParsedTask;
6
use Cerbero\ConsoleTasker\Console\TasksParser;
7
use Cerbero\ConsoleTasker\Tasks\AbstractCreatorTask;
8
use Cerbero\ConsoleTasker\Traits\ConfigAware;
9
use Illuminate\Container\Container;
10
use Illuminate\Support\Str;
11
12
/**
13
 * The task to create the Artisan command.
14
 *
15
 */
16
class CreateCommand extends AbstractCreatorTask
17
{
18
    use ConfigAware;
19
20
    /**
21
     * The tasks parser.
22
     *
23
     * @var TasksParser
24
     */
25
    protected $parser;
26
27
    /**
28
     * Instantiate the class.
29
     *
30
     * @param TasksParser $parser
31
     */
32
    public function __construct(TasksParser $parser)
33
    {
34
        $this->parser = $parser;
35
    }
36
37
    /**
38
     * Retrieve the path of the stub
39
     *
40
     * @return string
41
     */
42
    protected function getStubPath(): string
43
    {
44
        return __DIR__ . '/../../../stubs/command.stub';
45
    }
46
47
    /**
48
     * Retrieve the fully qualified name of the file to create
49
     *
50
     * @return string|null
51
     */
52
    protected function getFullyQualifiedName(): ?string
53
    {
54
        $name = str_replace('/', '\\', $this->argument('name'));
55
        $namespace = Container::getInstance()->make('app')->getNamespace();
56
57
        if (Str::startsWith($name, $namespace)) {
58
            return $name;
59
        }
60
61
        return "{$namespace}Console\Commands\\{$name}";
62
    }
63
64
    /**
65
     * Retrieve the reason why the file needs to be updated manually
66
     *
67
     * @return string|null
68
     */
69
    public function needsManualUpdateTo(): ?string
70
    {
71
        return 'write command description';
72
    }
73
74
    /**
75
     * Retrieve the replacements to apply on the stub
76
     *
77
     * @return array
78
     */
79
    protected function getReplacements(): array
80
    {
81
        $parsedTasks = $this->option('tasks') ? $this->parser->parse($this->option('tasks')) : [];
82
83
        return [
84
            '{{ command }}' => $this->option('command'),
85
            '{{ tasks }}' => $this->getPaddedTasks($parsedTasks),
86
            '{{ namespaces }}' => $this->getNamespaces($parsedTasks),
87
        ];
88
    }
89
90
    /**
91
     * Retrieve the command tasks with padding
92
     *
93
     * @param array $parsedTasks
94
     * @return string
95
     */
96
    protected function getPaddedTasks(array $parsedTasks): string
97
    {
98
        if (empty($parsedTasks)) {
99
            return str_repeat(' ', 12) . '//';
100
        }
101
102
        $paddedTasks = array_map(function (ParsedTask $task) {
103
            return str_repeat(' ', 12) . $task->name . '::class,';
104
        }, $parsedTasks);
105
106
        return implode(PHP_EOL, $paddedTasks);
107
    }
108
109
    /**
110
     * Retrieve the task namespaces
111
     *
112
     * @param array $parsedTasks
113
     * @return string|null
114
     */
115
    protected function getNamespaces(array $parsedTasks): ?string
116
    {
117
        if (empty($parsedTasks)) {
118
            return null;
119
        }
120
121
        $namespace = vsprintf('%s%s\%s', [
122
            Container::getInstance()->make('app')->getNamespace(),
123
            str_replace('/', '\\', $this->config('tasks_directory')),
124
            $this->argument('name'),
125
        ]);
126
127
        return array_reduce($parsedTasks, function (string $carry, ParsedTask $task) use ($namespace) {
128
            return $carry .= "use {$namespace}\\{$task->name};" . PHP_EOL;
129
        }, '');
130
    }
131
}
132