Completed
Push — v2 ( 975246...e7bb8d )
by Alexander
05:01
created

MakeTransformer::parseModel()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 14
ccs 0
cts 7
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Flugg\Responder\Console;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Support\Str;
7
use Symfony\Component\Console\Input\InputOption;
8
9
/**
10
 * An Artisan command class responsible for making transformer classes.
11
 *
12
 * @package flugger/laravel-responder
13
 * @author  Alexander Tømmerås <[email protected]>
14
 * @license The MIT License
15
 */
16
class MakeTransformer extends GeneratorCommand
17
{
18
    /**
19
     * The console command name.
20
     *
21
     * @var string
22
     */
23
    protected $name = 'make:transformer';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Create a new transformer class';
31
32
    /**
33
     * The type of class being generated.
34
     *
35
     * @var string
36
     */
37
    protected $type = 'Transformer';
38
39
    /**
40
     * Get the stub file for the generator.
41
     *
42
     * @return string
43
     */
44
    protected function getStub()
45
    {
46
        if ($this->option('model')) {
47
            return __DIR__ . '/../../resources/stubs/transformer.model.stub';
48
        }
49
50
        return __DIR__ . '/../../resources/stubs/transformer.plain.stub';
51
    }
52
53
    /**
54
     * Get the default namespace for the class.
55
     *
56
     * @param  string $rootNamespace
57
     * @return string
58
     */
59
    protected function getDefaultNamespace($rootNamespace)
60
    {
61
        return $rootNamespace . '\Transformers';
62
    }
63
64
    /**
65
     * Build the class with the given name.
66
     *
67
     * @param  string $name
68
     * @return string
69
     */
70
    protected function buildClass($name)
71
    {
72
        $replace = [];
73
74
        if (! $this->option('model') && ! $this->option('plain')) {
75
            $this->input->setOption('model', $this->resolveModelFromClassName());
76
        }
77
78
        if ($this->option('model')) {
79
            $replace = $this->buildModelReplacements($replace);
80
        }
81
82
        return str_replace(array_keys($replace), array_values($replace), parent::buildClass($name));
83
    }
84
85
    /**
86
     * Resolve a model from the given class name.
87
     *
88
     * @return string
89
     */
90
    protected function resolveModelFromClassName()
91
    {
92
        return 'App\\' . str_replace('Transformer', '', array_last(explode('/', $this->getNameInput())));
93
    }
94
95
    /**
96
     * Build the model replacement values.
97
     *
98
     * @param  array $replace
99
     * @return array
100
     */
101
    protected function buildModelReplacements(array $replace)
102
    {
103
        if (! class_exists($modelClass = $this->parseModel($this->option('model')))) {
0 ignored issues
show
Bug introduced by
It seems like $this->option('model') targeting Illuminate\Console\Command::option() can also be of type array; however, Flugg\Responder\Console\...ansformer::parseModel() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
104
            if ($this->confirm("A {$modelClass} model does not exist. Do you want to generate it?", true)) {
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $modelClass instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
105
                $this->call('make:model', ['name' => $modelClass]);
106
            }
107
        }
108
109
        return array_merge($replace, [
110
            'DummyFullModelClass' => $modelClass,
111
            'DummyModelClass' => class_basename($modelClass),
112
            'DummyModelVariable' => lcfirst(class_basename($modelClass)),
113
        ]);
114
    }
115
116
    /**
117
     * Get the fully-qualified model class name.
118
     *
119
     * @param  string $model
120
     * @return string
121
     */
122
    protected function parseModel($model)
123
    {
124
        if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) {
125
            throw new InvalidArgumentException('Model name contains invalid characters.');
126
        }
127
128
        $model = trim(str_replace('/', '\\', $model), '\\');
129
130
        if (! Str::startsWith($model, $rootNamespace = $this->laravel->getNamespace())) {
131
            $model = $rootNamespace . $model;
132
        }
133
134
        return $model;
135
    }
136
137
    /**
138
     * Get the console command options.
139
     *
140
     * @return array
141
     */
142
    protected function getOptions()
143
    {
144
        return [
145
            ['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a model transformer.'],
146
            ['plain', 'p', InputOption::VALUE_NONE, 'Generate a plain transformer.'],
147
        ];
148
    }
149
}