AnyPassServiceProvider::changeUsersDriver()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Imanghafoori\AnyPass;
4
5
use Illuminate\Support\ServiceProvider;
6
7
class AnyPassServiceProvider extends ServiceProvider
8
{
9
    /**
10
     * Bootstrap any application services.
11
     *
12
     * @return void
13
     */
14
    public function boot()
15
    {
16
    }
17
18
    /**
19
     * Register any application services.
20
     *
21
     * @return void
22
     */
23
    public function register()
24
    {
25
        \Auth::provider('eloquentAnyPass', function ($app, array $config) {
26
            return new AnyPassEloquentUserProvider($app['hash'], $config['model']);
27
        });
28
29
        \Auth::provider('databaseAnyPass', function ($app, array $config) {
30
            $connection = $app['db']->connection();
31
32
            return new AnyPassDatabaseUserProvider($connection, $app['hash'], $config['table']);
33
        });
34
35
        if ($this->properConfig()) {
36
            $this->changeUsersDriver();
37
        }
38
    }
39
40
    /**
41
     * @return bool
42
     */
43
    private function properConfig()
44
    {
45
        return env('APP_DEBUG') === true && $this->appEnvIsSafe() && env('ANY_PASS', false) === true;
46
    }
47
48
    private function changeUsersDriver()
49
    {
50
        $driver = config()->get('auth.providers.users.driver');
51
        if (in_array($driver, ['eloquent', 'database'])) {
52
            config()->set('auth.providers.users.driver', $driver.'AnyPass');
53
        }
54
    }
55
56
    /**
57
     * @return bool
58
     */
59
    private function appEnvIsSafe()
60
    {
61
        $csv = env('ANY_PASS_ENVIRONMENTS', 'local,testing');
62
63
        $allowedEnvironments = collect(explode(',', $csv))
64
            ->map(function ($string) {
65
                return trim($string);
66
            })
67
            ->filter()
68
            ->toArray();
69
70
        return in_array(env('APP_ENV'), $allowedEnvironments);
71
    }
72
}
73