Completed
Pull Request — master (#11)
by Andrea Marco
02:56
created

MakeTransformerCommand::buildClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 13
rs 9.4285
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\Console\Command;
8
use Illuminate\Filesystem\Filesystem;
9
10
class MakeTransformerCommand extends GeneratorCommand
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'make:transformer {model : The namespaced model to transform. e.g. "App\User"}';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Create a new transformer class';
25
26
    /**
27
     * The type of class being generated.
28
     *
29
     * @var string
30
     */
31
    protected $type = 'Transformer';
32
33
    /**
34
     * The view factory.
35
     *
36
     * @var \Illuminate\Contracts\View\Factory
37
     */
38
    protected $factory;
39
40
    /**
41
     * The includes listing.
42
     *
43
     * @var \Rexlabs\Laravel\Smokescreen\Console\IncludesListing
44
     */
45
    protected $includesListing;
46
47
    /**
48
     * The properties listing.
49
     *
50
     * @var \Rexlabs\Laravel\Smokescreen\Console\PropertiesListing
51
     */
52
    protected $propertiesListing;
53
54
    /**
55
     * Set the dependencies.
56
     *
57
     * @param  \Illuminate\Filesystem\Filesystem $files
58
     * @param  \Illuminate\Contracts\View\Factory $factory
59
     * @param  \Rexlabs\Laravel\Smokescreen\Console\IncludesListing $includesListing
60
     * @param  \Rexlabs\Laravel\Smokescreen\Console\PropertiesListing $propertiesListing
61
     */
62
    public function __construct(
63
        Filesystem $files,
64
        Factory $factory,
65
        IncludesListing $includesListing,
66
        PropertiesListing $propertiesListing
67
    ) {
68
        parent::__construct($files);
69
70
        $this->factory = $factory;
71
        $this->includesListing = $includesListing;
72
        $this->propertiesListing = $propertiesListing;
73
    }
74
75
    /**
76
     * Get the stub file for the generator.
77
     *
78
     * @return string
79
     */
80
    protected function getStub()
81
    {
82
        return 'smokescreen::transformer';
83
    }
84
85
    /**
86
     * Execute the console command.
87
     *
88
     * @return bool|null
89
     */
90
    public function handle()
91
    {
92
        $this->alertIfModelIsMissing();
93
94
        parent::handle();
95
    }
96
97
    /**
98
     * Display an error if the specified model does not exist.
99
     *
100
     */
101
    protected function alertIfModelIsMissing()
102
    {
103
        if (!class_exists($name = $this->argument('model'))) {
0 ignored issues
show
Bug introduced by
It seems like $name = $this->argument('model') can also be of type array; however, parameter $class_name of class_exists() 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

103
        if (!class_exists(/** @scrutinizer ignore-type */ $name = $this->argument('model'))) {
Loading history...
104
            exit($this->error("The model [$name] does not exist, please create it first."));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->error('The model ...ease create it first.') targeting Illuminate\Console\Command::error() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
105
        }
106
    }
107
108
    /**
109
     * Get the default namespace for the class.
110
     *
111
     * @param  string  $rootNamespace
112
     * @return string
113
     */
114
    protected function getDefaultNamespace($rootNamespace)
115
    {
116
        return config('smokescreen.transformer_namespace');
117
    }
118
119
    /**
120
     * Build the class with the given name.
121
     *
122
     * @param  string  $name
123
     * @return string
124
     */
125
    protected function buildClass($name)
126
    {
127
        $model = $this->argument('model');
128
129
        $data = [
130
            'namespace' => $this->getNamespace($name),
131
            'modelName' => $this->getModelName(),
132
            'transformerName' => $this->getNameInput(),
133
            'includes' => $this->includesListing->listForEloquent($model),
0 ignored issues
show
Bug introduced by
It seems like $model can also be of type array; however, parameter $class of Rexlabs\Laravel\Smokescr...ting::listForEloquent() 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

133
            'includes' => $this->includesListing->listForEloquent(/** @scrutinizer ignore-type */ $model),
Loading history...
134
            'properties' => $this->propertiesListing->listForEloquent($model),
0 ignored issues
show
Bug introduced by
It seems like $model can also be of type array; however, parameter $class of Rexlabs\Laravel\Smokescr...ting::listForEloquent() 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

134
            'properties' => $this->propertiesListing->listForEloquent(/** @scrutinizer ignore-type */ $model),
Loading history...
135
        ];
136
137
        return $this->factory->make($this->getStub(), $data)->render();
138
    }
139
140
    /**
141
     * Get the desired class name from the input.
142
     *
143
     * @return string
144
     */
145
    protected function getNameInput()
146
    {
147
        return $this->getModelName() . 'Transformer';
148
    }
149
150
    /**
151
     * Retrieve the model name.
152
     *
153
     * @return string
154
     */
155
    protected function getModelName() : string
156
    {
157
        return class_basename($this->argument('model'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('model') can also be of type array; however, parameter $class of class_basename() does only seem to accept object|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

157
        return class_basename(/** @scrutinizer ignore-type */ $this->argument('model'));
Loading history...
158
    }
159
}
160