Completed
Pull Request — master (#11)
by Jodie
02:31
created

MakeTransformerCommand::getTransformerNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Rexlabs\Laravel\Smokescreen\Console;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Contracts\View\Factory;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Filesystem\Filesystem;
9
10
class MakeTransformerCommand extends GeneratorCommand
11
{
12
    /**
13
     * The name and signature of the console command.
14
     * smokescreen:transformer --for='App\Models\Post'.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'make:transformer
19
        {model : The model to transform. e.g. "App\User"} 
20
        {--force : Overwrite an existing transformer}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Create a new smokescreen transformer class';
28
29
    /**
30
     * The type of class being generated.
31
     *
32
     * @var string
33
     */
34
    protected $type = 'Transformer';
35
36
    /**
37
     * The view factory.
38
     *
39
     * @var \Illuminate\Contracts\View\Factory
40
     */
41
    protected $viewFactory;
42
43
    /**
44
     * @var string
45
     */
46
    protected $modelClass;
47
48
    /**
49
     * Inject the dependencies.
50
     *
51
     * @param \Illuminate\Filesystem\Filesystem  $files
52
     * @param \Illuminate\Contracts\View\Factory $viewFactory
53
     */
54
    public function __construct(
55
        Filesystem $files,
56
        Factory $viewFactory
57
    ) {
58
        parent::__construct($files);
59
60
        $this->viewFactory = $viewFactory;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function handle()
67
    {
68
        $this->modelClass = $this->resolveModelClass($this->argument('model'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('model') can also be of type array; however, parameter $name of Rexlabs\Laravel\Smokescr...nd::resolveModelClass() 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

68
        $this->modelClass = $this->resolveModelClass(/** @scrutinizer ignore-type */ $this->argument('model'));
Loading history...
69
70
        parent::handle();
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    protected function getStub()
77
    {
78
        return 'smokescreen::transformer';
79
    }
80
81
    /**
82
     * Given a model name (or namespace) try to resolve the fully qualified model class
83
     * while checking common model namespaces.
84
     *
85
     * @param string $name
86
     *
87
     * @return mixed|null
88
     */
89
    protected function resolveModelClass(string $name)
90
    {
91
        $modelClass = null;
92
93
        // If not name-spaced, get a list of classes to search in common model namespaces.
94
        $search = strpos($name, '\\') !== false ? [$name] : array_map(function ($directory) use ($name) {
95
            return $directory.'\\'.$name;
96
        }, ['App\\Models', 'App\\Model', 'App']);
97
98
        // Check for a valid class.
99
        foreach ($search as $class) {
100
            if (class_exists($class)) {
101
                $modelClass = $class;
102
                break;
103
            }
104
        }
105
106
        // If we didn't find one, exit out.
107
        if ($modelClass === null) {
108
            throw new \InvalidArgumentException("The model [$name] does not exist, please create it first.");
109
        }
110
111
        return $modelClass;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    protected function getDefaultNamespace($rootNamespace)
118
    {
119
        return $this->getTransformerNamespace();
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    protected function buildClass($name)
126
    {
127
        return $this->viewFactory->make($this->getStub(), $this->getTemplateData())
128
            ->render();
129
    }
130
131
    /**
132
     * @throws \ReflectionException
133
     *
134
     * @return array
135
     */
136
    protected function getTemplateData()
137
    {
138
        $modelInspector = new ModelMapper($this->getModel());
139
140
        return [
141
            'model'                => $this->getModel(),
142
            'modelClass'           => $this->getModelClass(),
143
            'modelNamespace'       => $this->getModelNamespace(),
144
            'modelName'            => $this->getModelName(),
145
            'transformerClass'     => $this->getTransformerClass(),
146
            'transformerNamespace' => $this->getTransformerNamespace(),
147
            'transformerName'      => $this->getTransformerName(),
148
            'includes'             => $modelInspector->getIncludes(),
149
            'properties'           => $modelInspector->getDeclaredProperties(),
150
            'defaultProperties'    => $modelInspector->getDefaultProperties(),
151
        ];
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    protected function getNameInput()
158
    {
159
        return $this->getTransformerName();
160
    }
161
162
    /**
163
     * Get the transformer class name.
164
     *
165
     * @return string
166
     */
167
    protected function getTransformerName()
168
    {
169
        return preg_replace('/{ModelName}/i', $this->getModelName(),
170
            config('smokescreen.transformer_name', '{ModelName}Transformer'));
171
    }
172
173
    /**
174
     * Retrieve the transformer namespace.
175
     *
176
     * @return \Illuminate\Config\Repository|mixed
177
     */
178
    protected function getTransformerNamespace()
179
    {
180
        return config('smokescreen.transformer_namespace', 'App\Transformers');
181
    }
182
183
    /**
184
     * Retrieve the transformer class including namespace.
185
     *
186
     * @return string
187
     */
188
    protected function getTransformerClass()
189
    {
190
        return $this->getTransformerNamespace().'\\'.$this->getTransformerName();
0 ignored issues
show
Bug introduced by
Are you sure $this->getTransformerNamespace() of type Illuminate\Config\Repository|mixed can be used in concatenation? ( Ignorable by Annotation )

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

190
        return /** @scrutinizer ignore-type */ $this->getTransformerNamespace().'\\'.$this->getTransformerName();
Loading history...
191
    }
192
193
    /**
194
     * Retrieve the model class including namespace.
195
     *
196
     * @return string
197
     */
198
    protected function getModelClass(): string
199
    {
200
        return $this->modelClass;
201
    }
202
203
    /**
204
     * Get the eloquent model instance.
205
     *
206
     * @return Model
207
     */
208
    protected function getModel(): Model
209
    {
210
        $class = $this->getModelClass();
211
212
        return new $class();
213
    }
214
215
    /**
216
     * Retrieve the model class name.
217
     *
218
     * @return string
219
     */
220
    protected function getModelName(): string
221
    {
222
        return class_basename($this->getModelClass());
223
    }
224
225
    /**
226
     * Get the namespace of the model class.
227
     *
228
     * @return string
229
     */
230
    protected function getModelNamespace()
231
    {
232
        return $this->getNamespace($this->getModelClass());
233
    }
234
}
235