CredentialsServiceProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 7
dl 0
loc 65
rs 10
c 0
b 0
f 0

3 Methods

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