Completed
Pull Request — master (#96)
by
unknown
03:45
created

ProviderInstaller::migrateUserModule()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 9.4285
1
<?php
2
3
namespace Modules\Core\Console\Installers\Scripts\UserProviders;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Contracts\Foundation\Application;
7
use Illuminate\Filesystem\Filesystem;
8
use Modules\Core\Console\Installers\SetupScript;
9
use Modules\Core\Providers\CoreServiceProvider;
10
use Modules\Core\Services\Composer;
11
use Modules\User\Contracts\Authentication;
12
use Modules\User\Repositories\RoleRepository;
13
use Modules\User\Repositories\UserRepository;
14
15
abstract class ProviderInstaller implements SetupScript
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $driver;
21
22
    /**
23
     * @var Command
24
     */
25
    protected $command;
26
27
    /**
28
     * @var Filesystem
29
     */
30
    protected $finder;
31
32
    /**
33
     * @var Composer
34
     */
35
    protected $composer;
36
37
    /**
38
     * @var Application
39
     */
40
    protected $application;
41
42
    /**
43
     * @param Filesystem     $finder
44
     * @param Composer       $composer
45
     * @param Application    $application
46
     */
47
    public function __construct(Filesystem $finder, Composer $composer, Application $application)
48
    {
49
        $this->finder = $finder;
50
        $this->composer = $composer;
51
        $this->application = $application;
52
        $this->application['env'] = 'local';
53
    }
54
55
    /**
56
     * Fire the install script
57
     * @param  Command $command
58
     * @return mixed
59
     */
60
    public function fire(Command $command)
61
    {
62
        $this->command = $command;
63
64
        // Publish asgard configs
65
        if ($this->command->option('verbose')) {
66
            $this->command->call('vendor:publish', ['--provider' => CoreServiceProvider::class]);
67
        } else {
68
            $this->command->callSilent('vendor:publish', ['--provider' => CoreServiceProvider::class]);
69
        }
70
71
        if (! $this->checkIsInstalled()) {
72
            return $this->command->error('No user driver was installed. Please check the presence of a Service Provider');
73
        }
74
75
        $this->publish();
76
        $this->configure();
77
        $this->migrate();
78
        $this->seed();
79
        $this->migrateUserModule($command);
80
81
        $this->createFirstUser();
82
83
        if ($this->command->option('verbose')) {
84
            $command->info($this->driver . ' succesfully configured');
85
        }
86
    }
87
88
    /**
89
     * @return mixed
90
     */
91
    abstract public function composer();
92
93
    /**
94
     * Check if the user driver is correctly registered.
95
     * @return bool
96
     */
97
    abstract public function checkIsInstalled();
98
99
    /**
100
     * @return mixed
101
     */
102
    abstract public function publish();
103
104
    /**
105
     * @return mixed
106
     */
107
    abstract public function migrate();
108
109
    /**
110
     * @return mixed
111
     */
112
    abstract public function seed();
113
114
    /**
115
     * @return mixed
116
     */
117
    abstract public function configure();
118
119
    /**
120
     * @param $password
121
     * @return mixed
122
     */
123
    abstract public function getHashedPassword($password);
124
125
    /**
126
     * @param $command
127
     * @return mixed
128
     */
129
    private function migrateUserModule($command)
130
    {
131
        if ($command->option('verbose')) {
132
            return $command->call('module:migrate', ['module' => 'User']);
133
        }
134
135
        return $command->callSilent('module:migrate', ['module' => 'User']);
136
    }
137
138
    /**
139
     * @param $search
140
     * @param $Driver
141
     */
142
    protected function replaceCartalystUserModelConfiguration($search, $Driver)
143
    {
144
        $driver = strtolower($Driver);
145
146
        $path = base_path("config/cartalyst.{$driver}.php");
147
148
        $config = $this->finder->get($path);
149
150
        $config = str_replace($search, "Modules\\User\\Entities\\{$Driver}\\User", $config);
151
152
        $this->finder->put($path, $config);
153
    }
154
155
    /**
156
     * Set the correct repository binding on the fly for the current request
157
     *
158
     * @param $driver
159
     */
160
    protected function bindUserRepositoryOnTheFly($driver)
161
    {
162
        $this->application->bind(
163
            UserRepository::class,
164
            "Modules\\User\\Repositories\\$driver\\{$driver}UserRepository"
165
        );
166
        $this->application->bind(
167
            RoleRepository::class,
168
            "Modules\\User\\Repositories\\$driver\\{$driver}RoleRepository"
169
        );
170
        $this->application->bind(
171
            Authentication::class,
172
            "Modules\\User\\Repositories\\$driver\\{$driver}Authentication"
173
        );
174
    }
175
176
    /**
177
     * Create a first admin user
178
     */
179
    protected function createFirstUser()
180
    {
181
        $info = [
182
            'first_name' => $this->askForFirstName(),
183
            'last_name'  => $this->askForLastName(),
184
            'email'      => $this->askForEmail(),
185
            'password'   => $this->getHashedPassword(
186
                $this->askForPassword()
187
            ),
188
        ];
189
190
        $user = $this->application->make(UserRepository::class)->createWithRolesFromCli($info, [1], true);
191
        $this->application->make(\Modules\User\Repositories\UserTokenRepository::class)->generateFor($user->id);
192
193
        $this->command->info('Admin account created!');
194
    }
195
196
    /**
197
     * @return string
198
     */
199
    private function askForFirstName()
200
    {
201
        do {
202
            $firstname = $this->command->ask('Enter your first name');
203
            if ($firstname == '') {
204
                $this->command->error('First name is required');
205
            }
206
        } while (! $firstname);
207
208
        return $firstname;
209
    }
210
211
    /**
212
     * @return string
213
     */
214
    private function askForLastName()
215
    {
216
        do {
217
            $lastname = $this->command->ask('Enter your last name');
218
            if ($lastname == '') {
219
                $this->command->error('Last name is required');
220
            }
221
        } while (! $lastname);
222
223
        return $lastname;
224
    }
225
226
    /**
227
     * @return string
228
     */
229
    private function askForEmail()
230
    {
231
        do {
232
            $email = $this->command->ask('Enter your email address');
233
            if ($email == '') {
234
                $this->command->error('Email is required');
235
            }
236
        } while (! $email);
237
238
        return $email;
239
    }
240
241
    /**
242
     * @return string
243
     */
244
    private function askForPassword()
245
    {
246
        do {
247
            $password = $this->askForFirstPassword();
248
            $passwordConfirmation = $this->askForPasswordConfirmation();
249
            if ($password != $passwordConfirmation) {
250
                $this->command->error('Password confirmation doesn\'t match. Please try again.');
251
            }
252
        } while ($password != $passwordConfirmation);
253
254
        return $password;
255
    }
256
257
    /**
258
     * @return string
259
     */
260
    private function askForFirstPassword()
261
    {
262
        do {
263
            $password = $this->command->secret('Enter a password');
264
            if ($password == '') {
265
                $this->command->error('Password is required');
266
            }
267
        } while (! $password);
268
269
        return $password;
270
    }
271
272
    /**
273
     * @return string
274
     */
275
    private function askForPasswordConfirmation()
276
    {
277
        do {
278
            $passwordConfirmation = $this->command->secret('Please confirm your password');
279
            if ($passwordConfirmation == '') {
280
                $this->command->error('Password confirmation is required');
281
            }
282
        } while (! $passwordConfirmation);
283
284
        return $passwordConfirmation;
285
    }
286
}
287