Passed
Push — Generators ( 1df58a...4d090d )
by Roy
04:17 queued 11s
created

BaseGeneratorCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
eloc 20
dl 0
loc 66
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 12 2
A __construct() 0 5 1
A getPathBeforeClassName() 0 3 1
A getClassName() 0 5 2
A convertPathToNamespace() 0 3 1
A getClassNamespace() 0 5 2
1
<?php
2
3
namespace LaravelModulize\Console\Commands;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Console\GeneratorCommand;
8
use LaravelModulize\Contracts\ModulizerRepositoryInterface;
9
10
abstract class BaseGeneratorCommand extends GeneratorCommand
11
{
12
    /**
13
     * Instance of the repository
14
     *
15
     * @var \LaravelModulize\Contracts\ModulizerRepositoryInterface
16
     */
17
    protected $repository;
18
19
    protected $module;
20
    protected $modulePath;
21
22
    /**
23
     * Create a new command instance.
24
     *
25
     * @param \LaravelModulize\Contracts\ModulizerRepositoryInterface $repository
26
     * @return void
27
     */
28
    public function __construct(Filesystem $files, ModulizerRepositoryInterface $repository)
29
    {
30
        parent::__construct($files);
31
32
        $this->repository = $repository;
33
    }
34
35
    /**
36
     * Execute the console command.
37
     *
38
     * @return mixed
39
     */
40
    public function handle()
41
    {
42
        $this->module = $this->argument('module');
43
        $this->modulePath = $this->repository->getModulePath($this->module);
0 ignored issues
show
Bug introduced by
It seems like $this->module can also be of type null and string[]; however, parameter $module of LaravelModulize\Contract...erface::getModulePath() 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

43
        $this->modulePath = $this->repository->getModulePath(/** @scrutinizer ignore-type */ $this->module);
Loading history...
44
45
        if (!$this->repository->filesExist($this->modulePath)) {
46
            $this->call('modulize:make:module', [
47
                'module' => $this->module,
48
            ]);
49
        }
50
51
        parent::handle();
52
    }
53
54
    protected function getClassName($name)
55
    {
56
        return Str::contains($name, '/')
57
            ? array_reverse(explode('/', $name, 2))[0]
58
            : $name;
59
    }
60
61
    protected function getClassNamespace($name)
62
    {
63
        return Str::contains($name, '/')
64
            ? $this->convertPathToNamespace($name)
65
            : '';
66
    }
67
68
    protected function convertPathToNamespace($name)
69
    {
70
        return Str::start(str_replace('/', '', $this->getPathBeforeClassName($name)), '\\');
71
    }
72
73
    protected function getPathBeforeClassName($name)
74
    {
75
        return Str::before($name, $this->getClassName($name));
76
    }
77
}
78