MakeModuleCommand   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 11
eloc 43
c 4
b 2
f 0
dl 0
loc 122
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultNamespace() 0 3 1
A getPath() 0 10 1
A handle() 0 26 3
A getStub() 0 3 1
A __construct() 0 5 1
A buildManifest() 0 5 1
A getFQCNComp() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Contains the MakeModule Command class.
7
 *
8
 * @copyright   Copyright (c) 2016 Attila Fulop
9
 * @author      Attila Fulop
10
 * @license     MIT
11
 * @since       2017-01-18
12
 */
13
14
namespace Konekt\Concord\Console\Commands;
15
16
use Illuminate\Console\GeneratorCommand;
17
use Illuminate\Filesystem\Filesystem;
18
use Illuminate\Support\Str;
19
use Konekt\Concord\Contracts\Concord;
20
use Konekt\Concord\Contracts\Convention;
21
use Konekt\Concord\Exceptions\UnknownLaravelVersionException;
22
23
class MakeModuleCommand extends GeneratorCommand
24
{
25
    /** @var string  */
26
    protected $name = 'make:module';
27
28
    /** @var string  */
29
    protected $description = 'Create a new Concord module';
30
31
    /** @var Convention */
32
    protected $convention;
33
34
    /**
35
     * The type of class being generated.
36
     *
37
     * @var string
38
     */
39
    protected $type = 'Module';
40
41
    public function __construct(Filesystem $files, Concord $concord)
42
    {
43
        parent::__construct($files);
44
45
        $this->convention = $concord->getConvention();
46
    }
47
48
    public function handle()
49
    {
50
        if (false === parent::handle()) {
51
            return;
52
        }
53
54
        $providerFileName = $this->getPath($this->qualifyClass($this->getNameInput()));
55
        $this->files->put(//Replace the
56
            $providerFileName,
57
            str_replace(
58
                'DummyProviderFolder',
59
                str_replace('/', '\\', $this->convention->providersFolder()),
60
                $this->files->get($providerFileName)
61
            )
62
        );
63
64
        $name = $this->getNameInput();
65
        $manifestPath = str_replace(
66
            sprintf('%s/ModuleServiceProvider.php', $this->convention->providersFolder()),
67
            $this->convention->manifestFile(),
68
            $this->getPath($this->getFQCNComp($name))
69
        );
70
71
        if (!$this->files->exists($manifestPath)) {
72
            $this->makeDirectory($manifestPath);
73
            $this->files->put($manifestPath, $this->buildManifest($name));
74
        }
75
    }
76
77
    /**
78
     * Get the stub file for the generator.
79
     *
80
     * @return string
81
     */
82
    protected function getStub()
83
    {
84
        return __DIR__ . '/stubs/module_provider.stub';
85
    }
86
87
    /**
88
     * Get the default namespace for the class.
89
     *
90
     * @param  string  $rootNamespace
91
     * @return string
92
     */
93
    protected function getDefaultNamespace($rootNamespace)
94
    {
95
        return $rootNamespace . '\\' . str_replace('/', '\\', $this->convention->modulesFolder());
96
    }
97
98
    /**
99
     * Get the destination class path.
100
     *
101
     * @param  string  $name
102
     * @return string
103
     */
104
    protected function getPath($name)
105
    {
106
        $name = Str::replaceFirst($this->laravel->getNamespace(), '', $name);
107
108
        return sprintf(
109
            '%s/%s/%s/%s',
110
            $this->laravel['path'],
111
            str_replace('\\', '/', $name),
112
            $this->convention->providersFolder(),
113
            'ModuleServiceProvider.php'
114
        );
115
    }
116
117
    protected function buildManifest($name)
118
    {
119
        $stub = $this->files->get(__DIR__ . '/stubs/module_manifest.stub');
120
121
        return str_replace('DummyName', $name, $stub);
122
    }
123
124
    /**
125
     * Returns the fully qualified class name from the base generator command in a Laravel 5.3/5.4
126
     * compatible manner
127
     *
128
     * @param string    $name
129
     *
130
     * @throws UnknownLaravelVersionException
131
     *
132
     * @return string
133
     */
134
    protected function getFQCNComp($name)
135
    {
136
        if (method_exists($this, 'qualifyClass')) {
137
            return $this->qualifyClass($name);
138
        } elseif (method_exists($this, 'parseName')) {
139
            return $this->parseName($name);
0 ignored issues
show
Bug introduced by
The method parseName() does not exist on Konekt\Concord\Console\Commands\MakeModuleCommand. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

139
            return $this->/** @scrutinizer ignore-call */ parseName($name);
Loading history...
140
        }
141
        throw new UnknownLaravelVersionException(
142
            sprintf(
143
                "There's an incompatible parent class `%s` in your installed version of Laravel",
144
                get_parent_class($this)
145
            )
146
        );
147
    }
148
}
149