Passed
Push — feature/lg ( 3c06af...b2e7fb )
by Richard
03:07
created

PublishUserCommand   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
eloc 86
c 1
b 0
f 0
dl 0
loc 215
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A updateMenu() 0 11 1
A getViews() 0 10 1
A updateRoutes() 0 12 1
A getOptions() 0 3 1
A copyViews() 0 13 2
A publishCreateUserRequest() 0 19 3
A fillTemplate() 0 10 1
A createDirectories() 0 3 1
A publishUpdateUserRequest() 0 16 3
A publishUserRepository() 0 19 3
A publishUserController() 0 21 4
A getArguments() 0 3 1
A handle() 0 11 2
1
<?php
2
3
namespace PWWEB\Artomator\Commands\Publish;
4
5
use InfyOm\Generator\Utils\FileUtil;
6
7
class PublishUserCommand extends PublishBaseCommand
8
{
9
    /**
10
     * The console command name.
11
     *
12
     * @var string
13
     */
14
    protected $name = 'artomator.publish:user';
15
16
    /**
17
     * The console command description.
18
     *
19
     * @var string
20
     */
21
    protected $description = 'Publishes Users CRUD file';
22
23
    /**
24
     * Execute the command.
25
     *
26
     * @return void
27
     */
28
    public function handle()
29
    {
30
        $this->copyViews();
31
        $this->updateRoutes();
32
        $this->updateMenu();
33
        $this->publishUserController();
34
        if (config('infyom.laravel_generator.options.repository_pattern')) {
35
            $this->publishUserRepository();
36
        }
37
        $this->publishCreateUserRequest();
38
        $this->publishUpdateUserRequest();
39
    }
40
41
    private function copyViews()
42
    {
43
        $viewsPath = config('infyom.laravel_generator.path.views', resource_path('views/'));
44
        $templateType = config('infyom.laravel_generator.templates', 'adminlte-templates');
45
46
        $this->createDirectories($viewsPath.'users');
47
48
        $files = $this->getViews();
49
50
        foreach ($files as $stub => $blade) {
51
            $sourceFile = get_template_file_path('scaffold/'.$stub, $templateType);
52
            $destinationFile = $viewsPath.$blade;
53
            $this->publishFile($sourceFile, $destinationFile, $blade);
54
        }
55
    }
56
57
    private function createDirectories($dir)
58
    {
59
        FileUtil::createDirectoryIfNotExist($dir);
60
    }
61
62
    private function getViews()
63
    {
64
        return [
65
            'users/create'      => 'users/create.blade.php',
66
            'users/edit'        => 'users/edit.blade.php',
67
            'users/fields'      => 'users/fields.blade.php',
68
            'users/index'       => 'users/index.blade.php',
69
            'users/show'        => 'users/show.blade.php',
70
            'users/show_fields' => 'users/show_fields.blade.php',
71
            'users/table'       => 'users/table.blade.php',
72
        ];
73
    }
74
75
    private function updateRoutes()
76
    {
77
        $path = config('infyom.laravel_generator.path.routes', base_path('routes/web.php'));
78
79
        $routeContents = file_get_contents($path);
80
81
        $routesTemplate = get_template('routes.user', 'laravel-generator');
82
83
        $routeContents .= "\n\n".$routesTemplate;
84
85
        file_put_contents($path, $routeContents);
86
        $this->comment("\nUser route added");
87
    }
88
89
    private function updateMenu()
90
    {
91
        $viewsPath = config('infyom.laravel_generator.path.views', resource_path('views/'));
92
        $templateType = config('infyom.laravel_generator.templates', 'adminlte-templates');
93
        $path = $viewsPath.'layouts/menu.blade.php';
94
        $menuContents = file_get_contents($path);
95
        $sourceFile = file_get_contents(get_template_file_path('scaffold/users/menu', $templateType));
96
        $menuContents .= "\n".$sourceFile;
97
98
        file_put_contents($path, $menuContents);
99
        $this->comment("\nUser Menu added");
100
    }
101
102
    private function publishUserController()
103
    {
104
        $templateData = get_template('user/user_controller', 'laravel-generator');
105
        if (! config('infyom.laravel_generator.options.repository_pattern')) {
106
            $templateData = get_template('user/user_controller_without_repository', 'laravel-generator');
107
            $templateData = $this->fillTemplate($templateData);
108
        }
109
110
        $templateData = $this->fillTemplate($templateData);
111
112
        $controllerPath = config('infyom.laravel_generator.path.controller', app_path('Http/Controllers/'));
113
114
        $fileName = 'UserController.php';
115
116
        if (file_exists($controllerPath.$fileName) && ! $this->confirmOverwrite($fileName)) {
117
            return;
118
        }
119
120
        FileUtil::createFile($controllerPath, $fileName, $templateData);
121
122
        $this->info('UserController created');
123
    }
124
125
    private function publishUserRepository()
126
    {
127
        $templateData = get_template('user/user_repository', 'laravel-generator');
128
129
        $templateData = $this->fillTemplate($templateData);
130
131
        $repositoryPath = config('infyom.laravel_generator.path.repository', app_path('Repositories/'));
132
133
        $fileName = 'UserRepository.php';
134
135
        FileUtil::createDirectoryIfNotExist($repositoryPath);
136
137
        if (file_exists($repositoryPath.$fileName) && ! $this->confirmOverwrite($fileName)) {
138
            return;
139
        }
140
141
        FileUtil::createFile($repositoryPath, $fileName, $templateData);
142
143
        $this->info('UserRepository created');
144
    }
145
146
    private function publishCreateUserRequest()
147
    {
148
        $templateData = get_template('user/create_user_request', 'laravel-generator');
149
150
        $templateData = $this->fillTemplate($templateData);
151
152
        $requestPath = config('infyom.laravel_generator.path.request', app_path('Http/Requests/'));
153
154
        $fileName = 'CreateUserRequest.php';
155
156
        FileUtil::createDirectoryIfNotExist($requestPath);
157
158
        if (file_exists($requestPath.$fileName) && ! $this->confirmOverwrite($fileName)) {
159
            return;
160
        }
161
162
        FileUtil::createFile($requestPath, $fileName, $templateData);
163
164
        $this->info('CreateUserRequest created');
165
    }
166
167
    private function publishUpdateUserRequest()
168
    {
169
        $templateData = get_template('user/update_user_request', 'laravel-generator');
170
171
        $templateData = $this->fillTemplate($templateData);
172
173
        $requestPath = config('infyom.laravel_generator.path.request', app_path('Http/Requests/'));
174
175
        $fileName = 'UpdateUserRequest.php';
176
        if (file_exists($requestPath.$fileName) && ! $this->confirmOverwrite($fileName)) {
177
            return;
178
        }
179
180
        FileUtil::createFile($requestPath, $fileName, $templateData);
181
182
        $this->info('UpdateUserRequest created');
183
    }
184
185
    /**
186
     * Replaces dynamic variables of template.
187
     *
188
     * @param string $templateData
189
     *
190
     * @return string
191
     */
192
    private function fillTemplate($templateData)
193
    {
194
        $templateData = str_replace('$NAMESPACE_CONTROLLER$', config('infyom.laravel_generator.namespace.controller'), $templateData);
195
196
        $templateData = str_replace('$NAMESPACE_REQUEST$', config('infyom.laravel_generator.namespace.request'), $templateData);
197
198
        $templateData = str_replace('$NAMESPACE_REPOSITORY$', config('infyom.laravel_generator.namespace.repository'), $templateData);
199
        $templateData = str_replace('$NAMESPACE_USER$', config('auth.providers.users.model'), $templateData);
200
201
        return $templateData;
202
    }
203
204
    /**
205
     * Get the console command options.
206
     *
207
     * @return array
208
     */
209
    public function getOptions()
210
    {
211
        return [];
212
    }
213
214
    /**
215
     * Get the console command arguments.
216
     *
217
     * @return array
218
     */
219
    protected function getArguments()
220
    {
221
        return [];
222
    }
223
}
224