Passed
Push — master ( b3365b...65b7f9 )
by Chris
16:01
created

HookCommand::handle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 20
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 32
ccs 0
cts 20
cp 0
crap 12
rs 9.6
1
<?php
2
3
namespace Leonidas\Console\Command\Make;
4
5
use Leonidas\Console\Command\HopliteCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class HookCommand extends HopliteCommand
10
{
11
    protected const STUB_NAMESPACE = 'Leonidas\\Console\\Stubs\\Hook';
12
13
    protected static $defaultName = 'make:hook';
14
15
    protected static $defaultDescription = 'Creates a hook helper class';
16
17
    protected function configure(): void
18
    {
19
        $this
20
            ->addArgument('tag', InputArgument::REQUIRED)
21
            ->addArgument('type', InputArgument::OPTIONAL, 'The type of hook to create', 'action')
22
            ->addOption('path', 'p', InputOption::VALUE_OPTIONAL);
23
    }
24
25
    protected function handle(): int
26
    {
27
        $tag = $this->input->getArgument('tag');
28
        $type = $this->input->getArgument('type');
29
        $converted = $this->convert($tag)->toPascal();
30
31
        $parts = explode('/', $this->config('make.hook.path'));
0 ignored issues
show
Bug introduced by
It seems like $this->config('make.hook.path') can also be of type null; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

31
        $parts = explode('/', /** @scrutinizer ignore-type */ $this->config('make.hook.path'));
Loading history...
32
        $root = array_shift($parts);
33
        $namespace = implode('\\', $parts);
34
        $dir = $root . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, array_slice($parts, 1));
35
        $filename = 'Targets' . $converted . 'Hook.php';
36
        $file = getcwd() . '/' . $dir . '/' . $filename;
37
38
        $template = $this->getTemplate($type);
39
40
        $replacements = [
41
            static::STUB_NAMESPACE => $namespace,
42
            'Dummy' . ucfirst($type) => $converted,
43
            'dummy_' . strtolower($type) => $tag,
44
        ];
45
46
        foreach ($replacements as $key => $value) {
47
            $template = str_replace($key, $value, $template);
48
        }
49
50
        if (!file_exists($file)) {
51
            touch($file);
52
        }
53
54
        file_put_contents($file, $template);
55
56
        return self::SUCCESS;
57
    }
58
59
    protected function getTemplate(string $type): string
60
    {
61
        return file_get_contents($this->getTemplateFile($type));
62
    }
63
64
    protected function getTemplateFile(string $type): string
65
    {
66
        return $this->internal('/Stubs/Hook/TargetsDummy' . ucfirst($type) . 'Hook.php');
67
    }
68
}
69