Test Failed
Push — master ( e00988...ebb08c )
by Php Easy Api
04:02
created

Client::create()   C

Complexity

Conditions 9
Paths 192

Size

Total Lines 137
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 66
nc 192
nop 0
dl 0
loc 137
rs 6.5729
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Resta\Console\Source\Client;
4
5
use Resta\Support\Utils;
6
use Resta\Console\ConsoleOutputter;
7
use Resta\Console\ConsoleListAccessor;
8
use Resta\Foundation\PathManager\StaticPathModel;
9
use Resta\Support\Generator\Generator;
10
11
class Client extends ConsoleOutputter
12
{
13
    use ConsoleListAccessor;
14
15
    /**
16
     * @var string
17
     */
18
    public $type = 'client';
19
20
    /**
21
     * @var array
22
     */
23
    protected $runnableMethods = [
24
        'create'=>'Creates request object'
25
    ];
26
27
    /**
28
     * @var bool
29
     */
30
    protected $projectStatus = true;
31
32
    /**
33
     * @var array
34
     */
35
    public $commandRule=['name','client'];
36
37
    /**
38
     * @method create
39
     * @return void|mixed
40
     */
41
    public function create()
42
    {
43
        $name = $this->argument['name'];
44
        $client = $this->argument['client'];
45
46
47
        $this->directory['clientNameCreate']        = path()->request();
48
        $this->argument['clientNameNamespace']      = Utils::getNamespace($this->directory['clientNameCreate']);
49
        $this->directory['clientNameDir']           = $this->directory['clientNameCreate'].'/'.$name;
50
        $this->argument['clientNameDirNamespace']   = Utils::getNamespace($this->directory['clientNameCreate'].'/'.$name);
51
        $this->directory['clientSource']            = $this->directory['clientNameDir'].'/'.$client;
52
        $this->argument['clientSourceNamespace']    = Utils::getNamespace($this->directory['clientNameDir'].'/'.$client.'');
53
        
54
        //set project directory
55
        $this->file->makeDirectory($this);
56
57
        $this->argument['managerTraitNamespace'] = Utils::getNamespace($this->directory['clientNameDir'].'/'.$name.'Trait.php');
58
59
        if(!file_exists($manager = $this->directory['clientNameDir'].'/'.$name.'Manager.php')){
60
            $this->touch['client/manager'] = $manager;
61
            $this->touch['client/managertrait'] = $this->directory['clientNameDir'].'/'.$name.'Trait.php';
62
            
63
        }
64
65
        if(isset($this->argument['trait'])){
66
            $this->argument['name'] = $this->argument['trait'];
67
            
68
            if(!file_exists($managerCustomTrait = $this->directory['clientNameDir'].'/'.$this->argument['trait'].'Trait.php')){
69
                $this->touch['client/managercustomtrait'] = $managerCustomTrait;
70
            }
71
72
            $this->argument['managerTraitNamespace'] = Utils::getNamespace($managerCustomTrait);
73
        }
74
75
        if(!file_exists($this->directory['clientNameCreate'].'/Client.php')){
76
            $this->touch['client/client'] = $this->directory['clientNameCreate'].'/Client.php';
77
            $this->touch['client/clientGenerator'] = $this->directory['clientNameCreate'].'/ClientGenerator.php';
78
        }
79
80
        $clientSourceNamespace = Utils::getNamespace($this->directory['clientSource'].'/'.$client.'.php');
81
82
        if(!file_exists($clientSourceName = $this->directory['clientSource'].'/'.$client.'.php')){
83
            $this->touch['client/source'] = $clientSourceName.'';
84
        }
85
        
86
87
        if(!file_exists($this->directory['clientNameCreate'].'/ClientProvider.php')){
88
            $this->touch['client/clientProvider'] = $this->directory['clientNameCreate'].'/ClientProvider.php';
89
        }
90
91
        $this->touch['client/clientGeneratorFile'] = $this->directory['clientSource'].'/'.$client.'Generator.php';
92
93
94
        //set project touch
95
        $this->file->touch($this);
96
97
        $nameManager = $name.'Manager';
98
99
        $nameGeneratorNamespace = Utils::getNamespace($managerPath = $this->directory['clientNameDir'].''.DIRECTORY_SEPARATOR.''.$nameManager.'.php');
100
101
        $generator = new Generator(path()->version(),'ClientManager');
102
103
        $clientManager = app()->namespace()->version().'\\ClientManager';
104
105
        $clientManagerResolve = new $clientManager;
106
107
        if(!method_exists($clientManagerResolve,strtolower($name))){
108
109
            $generator->createMethod([
110
                strtolower($name)
111
            ]);
112
113
            $generator->createClassUse([
114
                $nameGeneratorNamespace
115
            ]);
116
117
            $generator->createMethodBody([
118
                strtolower($name) => 'return new '.$nameManager.'();'
119
            ]);
120
121
            $generator->createMethodDocument([
122
                strtolower($name) => [
123
                    '@return '.$nameManager.''
124
                ]
125
            ]);
126
127
            $generator->createMethodAccessibleProperty([
128
                strtolower($name) => 'protected'
129
            ]);
130
131
            Utils::changeClass(path()->version().''.DIRECTORY_SEPARATOR.'ClientManager.php',
132
                [
133
                    'Class ClientManager'=>'Class ClientManager'.PHP_EOL.' * @property '.$nameManager.' '.strtolower($name),
134
                ]);
135
136
        }
137
138
        $nameGenerator = new Generator($this->directory['clientNameDir'],$name.'Manager');
139
140
        $nameGeneratorNamespaceResolve = new $nameGeneratorNamespace;
141
142
        if(!method_exists($nameGeneratorNamespaceResolve,strtolower($client))){
143
144
            $nameGenerator->createMethod([
145
                strtolower($client)
146
            ]);
147
148
            $nameGenerator->createMethodAccessibleProperty([
149
                strtolower($client) => 'protected'
150
            ]);
151
152
            $nameGenerator->createMethodBody([
153
                strtolower($client) => 'return new '.$client.'();'
154
            ]);
155
156
            $nameGenerator->createMethodDocument([
157
                strtolower($client) => [
158
                    '@return '.$client.'',
159
                    '',
160
                    '@throws ReflectionException'
161
                ]
162
            ]);
163
164
            $nameGenerator->createClassUse([
165
                $clientSourceNamespace
166
            ]);
167
168
169
            Utils::changeClass($managerPath,
170
                [
171
                    'Class '.$name.'Manager'=>'Class '.$name.'Manager'.PHP_EOL.' * @property '.$client.' '.strtolower($client),
172
                ]);
173
        }
174
175
176
177
        echo $this->classical(' > Client called as "'.$client.'" has been successfully created in the '.app()->namespace()->request().'');
178
    }
179
}