CreateCommandTasks::getStubPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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;
8
use Cerbero\ConsoleTasker\Tasks\AbstractCreatorTask;
9
use Cerbero\ConsoleTasker\Traits\ConfigAware;
10
use Illuminate\Container\Container;
11
use Illuminate\Support\Str;
12
13
/**
14
 * The task to create the command tasks.
15
 *
16
 */
17
class CreateCommandTasks extends AbstractCreatorTask
18
{
19
    use ConfigAware;
20
21
    /**
22
     * The tasks parser.
23
     *
24
     * @var TasksParser
25
     */
26
    protected $parser;
27
28
    /**
29
     * The parsed task to generate.
30
     *
31
     * @var ParsedTask
32
     */
33
    protected $parsedTask;
34
35
    /**
36
     * The stubs map.
37
     *
38
     * @var array
39
     */
40
    protected $stubsMap = [
41
        Tasks\AbstractTask::class => __DIR__ . '/../../../stubs/task.stub',
42
        Tasks\AbstractCreatorTask::class => __DIR__ . '/../../../stubs/creator.stub',
43
        Tasks\AbstractFilesManipulatorTask::class => __DIR__ . '/../../../stubs/updater.stub',
44
    ];
45
46
    /**
47
     * Instantiate the class.
48
     *
49
     * @param TasksParser $parser
50
     */
51
    public function __construct(TasksParser $parser)
52
    {
53
        $this->parser = $parser;
54
    }
55
56
    /**
57
     * Run the task
58
     *
59
     * @return mixed
60
     */
61
    public function run()
62
    {
63
        $parsedTasks = $this->parser->parse($this->option('tasks'));
64
65
        foreach ($parsedTasks as $parsedTask) {
66
            $this->parsedTask = $parsedTask;
67
            $succeeded = parent::run() && $this->createStub();
68
69
            if (!$succeeded) {
70
                return false;
71
            }
72
        }
73
    }
74
75
    /**
76
     * Create stub if needed
77
     *
78
     * @return bool
79
     */
80
    protected function createStub(): bool
81
    {
82
        if ($this->parsedTask->parentClass != AbstractCreatorTask::class) {
83
            return true;
84
        }
85
86
        $path = dirname($this->getPath()) . '/stubs/' . Str::snake($this->parsedTask->name) . '.stub';
87
        $this->file($path)->needsManualUpdateTo('add stub content');
88
89
        if (!is_dir(dirname($path))) {
90
            @mkdir(dirname($path), 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

90
            /** @scrutinizer ignore-unhandled */ @mkdir(dirname($path), 0777, true);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
91
        }
92
93
        return file_put_contents($path, '') !== false;
94
    }
95
96
    /**
97
     * Determine whether this task should run
98
     *
99
     * @return bool
100
     */
101
    public function shouldRun(): bool
102
    {
103
        return !!$this->option('tasks');
104
    }
105
106
    /**
107
     * Retrieve the reason why this task should not run
108
     *
109
     * @return string|null
110
     */
111
    public function getSkippingReason(): ?string
112
    {
113
        return 'tasks to generate were not specified';
114
    }
115
116
    /**
117
     * Retrieve the path of the stub
118
     *
119
     * @return string
120
     */
121
    protected function getStubPath(): string
122
    {
123
        return $this->stubsMap[$this->parsedTask->parentClass];
124
    }
125
126
    /**
127
     * Retrieve the fully qualified name of the file to create
128
     *
129
     * @return string|null
130
     */
131
    protected function getFullyQualifiedName(): ?string
132
    {
133
        return vsprintf('%s%s\%s\%s', [
134
            Container::getInstance()->make('app')->getNamespace(),
135
            str_replace('/', '\\', $this->config('tasks_directory')),
136
            $this->argument('name'),
137
            $this->parsedTask->name,
138
        ]);
139
    }
140
141
    /**
142
     * Retrieve the reason why the file needs to be updated manually
143
     *
144
     * @return string|null
145
     */
146
    public function needsManualUpdateTo(): ?string
147
    {
148
        return 'implement task logic';
149
    }
150
151
    /**
152
     * Retrieve the replacements to apply on the stub
153
     *
154
     * @return array
155
     */
156
    protected function getReplacements(): array
157
    {
158
        return [
159
            '{{ purpose }}' => Str::snake($this->parsedTask->name, ' '),
160
            '{{ stub }}' => Str::snake($this->parsedTask->name),
161
        ];
162
    }
163
}
164