Completed
Push — master ( eff768...5b347e )
by Marcel
03:22
created

CredentialsServiceProvider::fixConfig()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BeyondCode\Credentials;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Encryption\Encrypter;
7
use Illuminate\Support\ServiceProvider;
8
9
class CredentialsServiceProvider extends ServiceProvider
10
{
11
12
    /**
13
     * Bootstrap the application services.
14
     */
15
    public function boot()
16
    {
17
        $this->publishes([
18
            __DIR__ . '/../config/credentials.php' => config_path('credentials.php'),
19
        ], 'config');
20
21
        $this->mergeConfigFrom(__DIR__ . '/../config/credentials.php', 'credentials');
22
23
        // Update configuration strings
24
        if( !app()->configurationIsCached()) {
25
            $this->fixConfig();
26
        }
27
    }
28
29
    protected function fixConfig()
30
    {
31
        collect(array_dot(config()->all()))->filter(function ($item) {
32
            return is_string($item) && starts_with($item, Credentials::CONFIG_PREFIX);
33
        })->map(function ($item, $key) {
34
            $item = str_replace_first(Credentials::CONFIG_PREFIX, '', $item);
35
36
            config()->set($key, credentials($item));
37
        });
38
    }
39
40
    /**
41
     * Register the application services.
42
     */
43
    public function register()
44
    {
45
        $this->app->bind(Credentials::class, function(){
46
47
            // If the key starts with "base64:", we will need to decode the key before handing
48
            // it off to the encrypter. Keys may be base-64 encoded for presentation and we
49
            // want to make sure to convert them back to the raw bytes before encrypting.
50
            if (Str::startsWith($key = config('credentials.key'), 'base64:')) {
51
                $key = base64_decode(substr($key, 7));
52
            }
53
54
            $encrypter = new Encrypter($key, config('credentials.cipher'));
55
            return new Credentials($encrypter);
56
        });
57
58
        $this->app->bind('command.credentials.edit', EditCredentialsCommand::class);
59
60
        $this->commands([
61
            'command.credentials.edit'
62
        ]);
63
    }
64
}
65