Completed
Push — 2.0 ( f8782a...a7be7e )
by Nicolas
02:41
created

ProviderInstaller   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 272
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 272
rs 10
wmc 28
lcom 2
cbo 3

19 Methods

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