PolicyMakeCommand   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 135
rs 10
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getStub() 0 5 2
A buildClass() 0 9 2
A getDefaultNamespace() 0 3 1
A replaceUserNamespace() 0 12 2
A getOptions() 0 4 1
A replaceModel() 0 33 3
1
<?php
2
3
namespace Larafast\Fastapi\Console\Commands;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Support\Str;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class PolicyMakeCommand extends GeneratorCommand
10
{
11
    /**
12
     * The console command name.
13
     *
14
     * @var string
15
     */
16
    protected $name = 'mbt:policy';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Create a new policy class';
24
25
    /**
26
     * The type of class being generated.
27
     *
28
     * @var string
29
     */
30
    protected $type = 'Policy';
31
32
    /**
33
     * Build the class with the given name.
34
     *
35
     * @param string $name
36
     * @return string
37
     */
38
    protected function buildClass($name)
39
    {
40
        $stub = $this->replaceUserNamespace(
41
            parent::buildClass($name)
42
        );
43
44
        $model = $this->option('model');
45
46
        return $model ? $this->replaceModel($stub, $model) : $stub;
47
    }
48
49
    /**
50
     * Replace the User model namespace.
51
     *
52
     * @param string $stub
53
     * @return string
54
     */
55
    protected function replaceUserNamespace($stub)
56
    {
57
        $model = $this->userProviderModel();
58
59
        if (!$model) {
60
            return $stub;
61
        }
62
63
        return str_replace(
64
            $this->rootNamespace() . 'User',
65
            $model,
66
            $stub
67
        );
68
    }
69
70
    /**
71
     * Replace the model for the given stub.
72
     *
73
     * @param string $stub
74
     * @param string $model
75
     * @return string
76
     */
77
    protected function replaceModel($stub, $model)
78
    {
79
        $model = str_replace('/', '\\', $model);
80
81
        $namespaceModel = $this->laravel->getNamespace() . $model;
82
83
        if (Str::startsWith($model, '\\')) {
84
            $stub = str_replace('NamespacedDummyModel', trim($model, '\\'), $stub);
85
        } else {
86
            $stub = str_replace('NamespacedDummyModel', $namespaceModel, $stub);
87
        }
88
89
        $stub = str_replace(
90
            "use {$namespaceModel};\nuse {$namespaceModel};", "use {$namespaceModel};", $stub
91
        );
92
93
        $model = class_basename(trim($model, '\\'));
94
95
        $dummyUser = class_basename($this->userProviderModel());
96
97
        $dummyModel = Str::camel($model) === 'user' ? 'model' : $model;
98
99
        $stub = str_replace('DocDummyModel', Str::snake($dummyModel, ' '), $stub);
100
101
        $stub = str_replace('DummyModelPlural', strtolower(Str::plural(Str::slug(studly_to_words($model), '_'))), $stub);
102
103
        $stub = str_replace('DummyModel', $model, $stub);
104
105
        $stub = str_replace('dummyModel', Str::camel($dummyModel), $stub);
106
107
        $stub = str_replace('DummyUser', $dummyUser, $stub);
108
109
        return str_replace('DocDummyPluralModel', Str::snake(Str::pluralStudly($dummyModel), ' '), $stub);
110
    }
111
112
    /**
113
     * Get the stub file for the generator.
114
     *
115
     * @return string
116
     */
117
    protected function getStub()
118
    {
119
        return $this->option('model')
120
            ? config('fastApi.stubs_dir'). '/policy.stub'
121
            : config('fastApi.stubs_dir'). '/policy.plain.stub';
122
    }
123
124
    /**
125
     * Get the default namespace for the class.
126
     *
127
     * @param string $rootNamespace
128
     * @return string
129
     */
130
    protected function getDefaultNamespace($rootNamespace)
131
    {
132
        return $rootNamespace . '\Policies';
133
    }
134
135
    /**
136
     * Get the console command arguments.
137
     *
138
     * @return array
139
     */
140
    protected function getOptions()
141
    {
142
        return [
143
            ['model', 'm', InputOption::VALUE_OPTIONAL, 'The model that the policy applies to'],
144
        ];
145
    }
146
}
147