Completed
Push — master ( f02869...8251a6 )
by Nicolas
03:31
created

ProviderInstaller::fire()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 15

Duplication

Lines 5
Ratio 19.23 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 6
nop 1
dl 5
loc 26
rs 8.5806
c 0
b 0
f 0
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\Services\Composer;
8
9
abstract class ProviderInstaller implements SetupScript
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $driver;
15
16
    /**
17
     * @var Command
18
     */
19
    protected $command;
20
21
    /**
22
     * @var Filesystem
23
     */
24
    protected $finder;
25
26
    /**
27
     * @var Composer
28
     */
29
    protected $composer;
30
31
    /**
32
     * @var Application
33
     */
34
    protected $application;
35
36
    /**
37
     * @param Filesystem     $finder
38
     * @param Composer       $composer
39
     * @param Application    $application
40
     */
41
    public function __construct(Filesystem $finder, Composer $composer, Application $application)
42
    {
43
        $this->finder = $finder;
44
        $this->composer = $composer;
45
        $this->application = $application;
46
        $this->application['env'] = 'local';
47
    }
48
49
    /**
50
     * Fire the install script
51
     * @param  Command $command
52
     * @return mixed
53
     */
54
    public function fire(Command $command)
55
    {
56
        $this->command = $command;
57
58
        // Publish asgard configs
59 View Code Duplication
        if ($this->command->option('verbose')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
            $this->command->call('vendor:publish', ['--provider' => 'Modules\Core\Providers\CoreServiceProvider']);
61
        } else {
62
            $this->command->callSilent('vendor:publish', ['--provider' => 'Modules\Core\Providers\CoreServiceProvider']);
63
        }
64
65
        if (! $this->checkIsInstalled()) {
66
            return $this->command->error('No user driver was installed. Please check the presence of a Service Provider');
67
        }
68
69
        $this->publish();
70
        $this->configure();
71
        $this->migrate();
72
        $this->seed();
73
74
        $this->createFirstUser();
75
76
        if ($this->command->option('verbose')) {
77
            $command->info($this->driver . ' succesfully configured');
78
        }
79
    }
80
81
    /**
82
     * @return mixed
83
     */
84
    abstract public function composer();
85
86
    /**
87
     * Check if the user driver is correctly registered.
88
     * @return bool
89
     */
90
    abstract public function checkIsInstalled();
91
92
    /**
93
     * @return mixed
94
     */
95
    abstract public function publish();
96
97
    /**
98
     * @return mixed
99
     */
100
    abstract public function migrate();
101
102
    /**
103
     * @return mixed
104
     */
105
    abstract public function seed();
106
107
    /**
108
     * @return mixed
109
     */
110
    abstract public function configure();
111
112
    /**
113
     * @param $password
114
     * @return mixed
115
     */
116
    abstract public function getHashedPassword($password);
117
118
    /**
119
     * @param $search
120
     * @param $Driver
121
     */
122
    protected function replaceCartalystUserModelConfiguration($search, $Driver)
123
    {
124
        $driver = strtolower($Driver);
125
126
        $path = base_path("config/cartalyst.{$driver}.php");
127
128
        $config = $this->finder->get($path);
129
130
        $config = str_replace($search, "Modules\\User\\Entities\\{$Driver}\\User", $config);
131
132
        $this->finder->put($path, $config);
133
    }
134
135
    /**
136
     * Set the correct repository binding on the fly for the current request
137
     *
138
     * @param $driver
139
     */
140
    protected function bindUserRepositoryOnTheFly($driver)
141
    {
142
        $this->application->bind(
143
            'Modules\User\Repositories\UserRepository',
144
            "Modules\\User\\Repositories\\$driver\\{$driver}UserRepository"
145
        );
146
        $this->application->bind(
147
            'Modules\User\Repositories\RoleRepository',
148
            "Modules\\User\\Repositories\\$driver\\{$driver}RoleRepository"
149
        );
150
        $this->application->bind(
151
            'Modules\Core\Contracts\Authentication',
152
            "Modules\\User\\Repositories\\$driver\\{$driver}Authentication"
153
        );
154
    }
155
156
    /**
157
     * Create a first admin user
158
     */
159
    protected function createFirstUser()
160
    {
161
        $info = [
162
            'first_name' => $this->askForFirstName(),
163
            'last_name'  => $this->askForLastName(),
164
            'email'      => $this->askForEmail(),
165
            'password'   => $this->getHashedPassword(
166
                $this->askForPassword()
167
            ),
168
        ];
169
170
        $this->application->make(\Modules\User\Repositories\UserRepository::class)->createWithRolesFromCli($info, [1], true);
171
172
        $this->command->info('Admin account created!');
173
    }
174
175
    /**
176
     * @return string
177
     */
178
    private function askForFirstName()
179
    {
180
        do {
181
            $firstname = $this->command->ask('Enter your first name');
182
            if ($firstname == '') {
183
                $this->command->error('First name is required');
184
            }
185
        } while (! $firstname);
186
187
        return $firstname;
188
    }
189
190
    /**
191
     * @return string
192
     */
193
    private function askForLastName()
194
    {
195
        do {
196
            $lastname = $this->command->ask('Enter your last name');
197
            if ($lastname == '') {
198
                $this->command->error('Last name is required');
199
            }
200
        } while (! $lastname);
201
202
        return $lastname;
203
    }
204
205
    /**
206
     * @return string
207
     */
208
    private function askForEmail()
209
    {
210
        do {
211
            $email = $this->command->ask('Enter your email address');
212
            if ($email == '') {
213
                $this->command->error('Email is required');
214
            }
215
        } while (! $email);
216
217
        return $email;
218
    }
219
220
    /**
221
     * @return string
222
     */
223
    private function askForPassword()
224
    {
225
        do {
226
            $password = $this->askForFirstPassword();
227
            $passwordConfirmation = $this->askForPasswordConfirmation();
228
            if ($password != $passwordConfirmation) {
229
                $this->command->error('Password confirmation doesn\'t match. Please try again.');
230
            }
231
        } while ($password != $passwordConfirmation);
232
233
        return $password;
234
    }
235
236
    /**
237
     * @return string
238
     */
239
    private function askForFirstPassword()
240
    {
241
        do {
242
            $password = $this->command->secret('Enter a password');
243
            if ($password == '') {
244
                $this->command->error('Password is required');
245
            }
246
        } while (! $password);
247
248
        return $password;
249
    }
250
251
    /**
252
     * @return string
253
     */
254
    private function askForPasswordConfirmation()
255
    {
256
        do {
257
            $passwordConfirmation = $this->command->secret('Please confirm your password');
258
            if ($passwordConfirmation == '') {
259
                $this->command->error('Password confirmation is required');
260
            }
261
        } while (! $passwordConfirmation);
262
263
        return $passwordConfirmation;
264
    }
265
}
266