Passed
Push — master ( 32dee0...5d2de3 )
by Andrea Marco
13:08 queued 11s
created

CreateCommand::getNamespaces()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 15
ccs 0
cts 6
cp 0
crap 6
rs 9.9666
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 replacements to apply on the stub
66
     *
67
     * @return array
68
     */
69
    protected function getReplacements(): array
70
    {
71
        $parsedTasks = $this->option('tasks') ? $this->parser->parse($this->option('tasks')) : [];
72
73
        return [
74
            '{{ command }}' => $this->option('command'),
75
            '{{ tasks }}' => $this->getPaddedTasks($parsedTasks),
76
            '{{ namespaces }}' => $this->getNamespaces($parsedTasks),
77
        ];
78
    }
79
80
    /**
81
     * Retrieve the command tasks with padding
82
     *
83
     * @param array $parsedTasks
84
     * @return string
85
     */
86
    protected function getPaddedTasks(array $parsedTasks): string
87
    {
88
        if (empty($parsedTasks)) {
89
            return str_repeat(' ', 12) . '//';
90
        }
91
92
        $paddedTasks = array_map(function (ParsedTask $task) {
93
            return str_repeat(' ', 12) . $task->name . '::class,';
94
        }, $parsedTasks);
95
96
        return implode(PHP_EOL, $paddedTasks);
97
    }
98
99
    /**
100
     * Retrieve the task namespaces
101
     *
102
     * @param array $parsedTasks
103
     * @return string|null
104
     */
105
    protected function getNamespaces(array $parsedTasks): ?string
106
    {
107
        if (empty($parsedTasks)) {
108
            return null;
109
        }
110
111
        $namespace = vsprintf('%s%s\%s', [
112
            Container::getInstance()->make('app')->getNamespace(),
113
            str_replace('/', '\\', $this->config('tasks_directory')),
114
            $this->argument('name'),
115
        ]);
116
117
        return array_reduce($parsedTasks, function (string $carry, ParsedTask $task) use ($namespace) {
118
            return $carry .= "use {$namespace}\\{$task->name};" . PHP_EOL;
119
        }, '') . PHP_EOL;
120
    }
121
}
122