ProviderInstaller::fire()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 15

Duplication

Lines 5
Ratio 19.23 %
Metric Value
dl 5
loc 26
rs 8.5806
cc 4
eloc 15
nc 6
nop 1
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')->createWithRoles(
171
            $info,
172
            [1],
173
            true
174
        );
175
176
        $this->command->info('Admin account created!');
177
    }
178
179
    /**
180
     * @return string
181
     */
182
    private function askForFirstName()
183
    {
184
        do {
185
            $firstname = $this->command->ask('Enter your first name');
186
            if ($firstname == '') {
187
                $this->command->error('First name is required');
188
            }
189
        } while (! $firstname);
190
191
        return $firstname;
192
    }
193
194
    /**
195
     * @return string
196
     */
197
    private function askForLastName()
198
    {
199
        do {
200
            $lastname = $this->command->ask('Enter your last name');
201
            if ($lastname == '') {
202
                $this->command->error('Last name is required');
203
            }
204
        } while (! $lastname);
205
206
        return $lastname;
207
    }
208
209
    /**
210
     * @return string
211
     */
212
    private function askForEmail()
213
    {
214
        do {
215
            $email = $this->command->ask('Enter your email address');
216
            if ($email == '') {
217
                $this->command->error('Email is required');
218
            }
219
        } while (! $email);
220
221
        return $email;
222
    }
223
224
    /**
225
     * @return string
226
     */
227
    private function askForPassword()
228
    {
229
        do {
230
            $password = $this->askForFirstPassword();
231
            $passwordConfirmation = $this->askForPasswordConfirmation();
232
            if ($password != $passwordConfirmation) {
233
                $this->command->error('Password confirmation doesn\'t match. Please try again.');
234
            }
235
        } while ($password != $passwordConfirmation);
236
237
        return $password;
238
    }
239
240
    /**
241
     * @return string
242
     */
243
    private function askForFirstPassword()
244
    {
245
        do {
246
            $password = $this->command->secret('Enter a password');
247
            if ($password == '') {
248
                $this->command->error('Password is required');
249
            }
250
        } while (! $password);
251
252
        return $password;
253
    }
254
255
    /**
256
     * @return string
257
     */
258
    private function askForPasswordConfirmation()
259
    {
260
        do {
261
            $passwordConfirmation = $this->command->secret('Please confirm your password');
262
            if ($passwordConfirmation == '') {
263
                $this->command->error('Password confirmation is required');
264
            }
265
        } while (! $passwordConfirmation);
266
267
        return $passwordConfirmation;
268
    }
269
}
270