CreateCommand   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 35
c 3
b 0
f 0
dl 0
loc 138
ccs 0
cts 33
cp 0
rs 10
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getStubPath() 0 3 1
A getFullyQualifiedName() 0 10 2
A getSkippingReason() 0 3 1
A getNamespaces() 0 15 2
A needsManualUpdateTo() 0 7 2
A getReplacements() 0 8 2
A getPaddedTasks() 0 11 2
A shouldRun() 0 3 1
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
     * Determine whether this task should run
66
     *
67
     * @return bool
68
     */
69
    public function shouldRun(): bool
70
    {
71
        return !file_exists($this->getPath());
72
    }
73
74
    /**
75
     * Retrieve the reason why this task should not run
76
     *
77
     * @return string|null
78
     */
79
    public function getSkippingReason(): ?string
80
    {
81
        return 'the command already exists';
82
    }
83
84
    /**
85
     * Retrieve the reason why the file needs to be updated manually
86
     *
87
     * @return string|null
88
     */
89
    public function needsManualUpdateTo(): ?string
90
    {
91
        if ($this->option('command') == 'command:name') {
92
            return 'write command name and description';
93
        }
94
95
        return 'write command description';
96
    }
97
98
    /**
99
     * Retrieve the replacements to apply on the stub
100
     *
101
     * @return array
102
     */
103
    protected function getReplacements(): array
104
    {
105
        $parsedTasks = $this->option('tasks') ? $this->parser->parse($this->option('tasks')) : [];
106
107
        return [
108
            '{{ command }}' => $this->option('command'),
109
            '{{ tasks }}' => $this->getPaddedTasks($parsedTasks),
110
            '{{ namespaces }}' => $this->getNamespaces($parsedTasks),
111
        ];
112
    }
113
114
    /**
115
     * Retrieve the command tasks with padding
116
     *
117
     * @param array $parsedTasks
118
     * @return string
119
     */
120
    protected function getPaddedTasks(array $parsedTasks): string
121
    {
122
        if (empty($parsedTasks)) {
123
            return str_repeat(' ', 12) . '//';
124
        }
125
126
        $paddedTasks = array_map(function (ParsedTask $task) {
127
            return str_repeat(' ', 12) . $task->name . '::class,';
128
        }, $parsedTasks);
129
130
        return implode(PHP_EOL, $paddedTasks);
131
    }
132
133
    /**
134
     * Retrieve the task namespaces
135
     *
136
     * @param array $parsedTasks
137
     * @return string|null
138
     */
139
    protected function getNamespaces(array $parsedTasks): ?string
140
    {
141
        if (empty($parsedTasks)) {
142
            return null;
143
        }
144
145
        $namespace = vsprintf('%s%s\%s', [
146
            Container::getInstance()->make('app')->getNamespace(),
147
            str_replace('/', '\\', $this->config('tasks_directory')),
148
            $this->argument('name'),
149
        ]);
150
151
        return array_reduce($parsedTasks, function (string $carry, ParsedTask $task) use ($namespace) {
152
            return $carry .= "use {$namespace}\\{$task->name};" . PHP_EOL;
153
        }, '');
154
    }
155
}
156