Completed
Push — master ( c915d2...2ad4b2 )
by Alexander
04:32
created

MakeTransformer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 171
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 4 1
A generateTransformer() 0 14 2
A makeDirectory() 0 6 2
A makeClass() 0 10 1
A replaceNamespace() 0 8 1
A replaceClass() 0 6 1
A replaceModel() 0 11 1
A getModelNamespace() 0 8 2
A getClassFromNamespace() 0 4 1
1
<?php
2
3
namespace Flugg\Responder\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
8
/**
9
 * An Artisan command generating a new transformer class into app/Transformers.
10
 *
11
 * @package Laravel Responder
12
 * @author  Alexander Tømmerås <[email protected]>
13
 * @license The MIT License
14
 */
15
class MakeTransformer extends Command
16
{
17
    /**
18
     * The name and signature of the console command.
19
     *
20
     * @var string
21
     */
22
    protected $signature = 'make:transformer 
23
                            {name : The name of the transformer class}
24
                            {--model= : The namespace to the model being transformed}';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Create a new transformer class';
32
33
    /**
34
     * The file system instance.
35
     *
36
     * @var Filesystem
37
     */
38
    protected $files;
39
40
    /**
41
     * Create a new command instance.
42
     *
43
     * @param  Filesystem $files
44
     */
45
    public function __construct( Filesystem $files )
46
    {
47
        parent::__construct();
48
49
        $this->files = $files;
50
    }
51
52
    /**
53
     * Execute the console command.
54
     *
55
     * @return mixed
56
     */
57
    public function handle()
58
    {
59
        $this->generateTransformer();
60
    }
61
62
    /**
63
     * Generate the transformer class.
64
     *
65
     * @return void
66
     */
67
    protected function generateTransformer()
68
    {
69
        $name = (string) $this->argument( 'name' );
70
        $path = app_path() . '/Transformers/' . $name . '.php';
71
72
        if ( $this->files->exists( $path ) ) {
73
            return $this->error( $name . ' already exists!' );
74
        }
75
76
        $this->makeDirectory( $path );
77
        $this->files->put( $path, $this->makeClass( $name ) );
78
79
        $this->info( 'Transformer created successfully.' );
80
    }
81
82
    /**
83
     * Build a transformers directory if one doesn't exist.
84
     *
85
     * @param  string $path
86
     * @return void
87
     */
88
    protected function makeDirectory( string $path )
89
    {
90
        if ( ! $this->files->isDirectory( dirname( $path ) ) ) {
91
            $this->files->makeDirectory( dirname( $path ), 0777, true, true );
92
        }
93
    }
94
95
    /**
96
     * Build the class with the given name.
97
     *
98
     * @param  string $name
99
     * @return string
100
     */
101
    protected function makeClass( string $name ):string
102
    {
103
        $stub = $this->files->get( __DIR__ . '/../../resources/stubs/transformer.stub' );
104
105
        $stub = $this->replaceNamespace( $stub );
106
        $stub = $this->replaceClass( $stub, $name );
107
        $stub = $this->replaceModel( $stub, $name );
108
109
        return $stub;
110
    }
111
112
    /**
113
     * Replace the namespace for the given stub.
114
     *
115
     * @param  string $stub
116
     * @return string
117
     */
118
    protected function replaceNamespace( string $stub ):string
119
    {
120
        $namespace = $this->laravel->getNamespace() . 'Transformers';
121
122
        $stub = str_replace( 'DummyNamespace', $namespace, $stub );
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $stub. This often makes code more readable.
Loading history...
123
124
        return $stub;
125
    }
126
127
    /**
128
     * Replace the class name for the given stub.
129
     *
130
     * @param  string $stub
131
     * @param  string $name
132
     * @return string
133
     */
134
    protected function replaceClass( string $stub, string $name ):string
135
    {
136
        $stub = str_replace( 'DummyClass', $name, $stub );
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $stub. This often makes code more readable.
Loading history...
137
138
        return $stub;
139
    }
140
141
    /**
142
     * Replace the class name for the given stub.
143
     *
144
     * @param  string $stub
145
     * @param  string $name
146
     * @return string
147
     */
148
    protected function replaceModel( string $stub, string $name ):string
149
    {
150
        $model = $this->getModelNamespace( $name );
151
        $class = $this->getClassFromNamespace( $model );
152
153
        $stub = str_replace( 'DummyModelNamespace', $model, $stub );
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $stub. This often makes code more readable.
Loading history...
154
        $stub = str_replace( 'DummyModelClass', $class, $stub );
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $stub. This often makes code more readable.
Loading history...
155
        $stub = str_replace( 'DummyModelVariable', camel_case( $class ), $stub );
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $stub. This often makes code more readable.
Loading history...
156
157
        return $stub;
158
    }
159
160
    /**
161
     * Get the class name for the transformer.
162
     *
163
     * @param  string $name
164
     * @return string
165
     */
166
    protected function getModelNamespace( string $name ):string
167
    {
168
        if ( $this->option( 'model' ) ) {
169
            return $this->option( 'model' );
170
        }
171
172
        return 'App\\' . str_replace( 'Transformer', '', $name );
173
    }
174
175
    /**
176
     * Get the class name for the transformer.
177
     *
178
     * @param  string $namespace
179
     * @return string
180
     */
181
    protected function getClassFromNamespace( string $namespace ):string
182
    {
183
        return last( explode( '\\', $namespace ) );
184
    }
185
}