RepositoryMakeCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 30
c 1
b 0
f 0
dl 0
loc 73
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDestinationFilePath() 0 7 1
A getTemplateContents() 0 13 1
A getDefaultNamespace() 0 6 2
A getModel() 0 13 3
A getRepositoryName() 0 9 2
1
<?php
2
3
namespace Rawilk\LaravelModules\Commands\Generators;
4
5
use Illuminate\Support\Str;
6
use Rawilk\LaravelModules\Support\Config\GenerateConfigReader;
7
use Rawilk\LaravelModules\Support\Stub;
8
use Rawilk\LaravelModules\Traits\ModuleCommands;
9
use Illuminate\Database\Eloquent\Model;
10
11
class RepositoryMakeCommand extends GeneratorCommand
12
{
13
    use ModuleCommands;
14
15
    /** @var string */
16
    protected $argumentName = 'repository';
17
18
    /** @var string */
19
    protected $signature = 'module:make-repository
20
                            {repository : The name of the repository}
21
                            {module? : The name of the module to create the repository for}
22
                            {--base_class= : Override the default base repository class (from config)}
23
                            {--model= : The model the repository is for}';
24
25
    /** @var string */
26
    protected $description = 'Generate a new repository for the specified module.';
27
28
    protected function getDefaultNamespace(): string
29
    {
30
        /** @var \Rawilk\LaravelModules\Contracts\Repository $module */
31
        $module = $this->laravel['modules'];
32
33
        return $module->config('paths.generator.repository.namespace') ?: $module->config('paths.generator.repository.path', 'Repositories');
34
    }
35
36
    protected function getDestinationFilePath(): string
37
    {
38
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
39
40
        $repositoryPath = GenerateConfigReader::read('repository');
41
42
        return $path . $repositoryPath->getPath() . '/' . $this->getRepositoryName() . '.php';
43
    }
44
45
    protected function getTemplateContents(): string
46
    {
47
        /** @var \Rawilk\LaravelModules\Module $module */
48
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
49
50
        return (new Stub('/repository.stub', [
51
            'NAMESPACE'        => $this->getClassNamespace($module),
52
            'CLASS'            => class_basename($this->getRepositoryName()),
53
            'BASE_CLASS'       => $this->getBaseClass('repository'),
54
            'BASE_CLASS_SHORT' => $this->getBaseClass('repository', true),
55
            'MODEL'            => $this->getModel(),
56
            'MODEL_NAMESPACE'  => $this->getModel(false),
57
        ]))->render();
58
    }
59
60
    private function getModel(bool $returnBasename = true): string
61
    {
62
        $model = Str::studly($this->option('model'));
63
64
        if (! $model) {
65
            $model = Model::class;
66
        }
67
68
        $model = str_replace('/', '\\', $model);
69
70
        return $returnBasename
71
            ? class_basename($model) . '::class'
72
            : $model;
73
    }
74
75
    private function getRepositoryName(): string
76
    {
77
        $repository = Str::studly($this->argument('repository'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('repository') can also be of type string[]; however, parameter $value of Illuminate\Support\Str::studly() 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

77
        $repository = Str::studly(/** @scrutinizer ignore-type */ $this->argument('repository'));
Loading history...
78
79
        if (! Str::contains(strtolower($repository), 'repository')) {
80
            $repository .= 'Repository';
81
        }
82
83
        return $repository;
84
    }
85
}
86