EloquentAESServiceProvider   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 80
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 13 2
A register() 0 8 1
A registerEncryptor() 0 8 1
A registerOpisSecurityKey() 0 10 3
A key() 0 10 2
1
<?php
2
3
namespace RichardStyles\EloquentAES;
4
5
use Illuminate\Encryption\Encrypter;
6
use Illuminate\Encryption\EncryptionServiceProvider;
7
use Illuminate\Encryption\MissingAppKeyException;
8
use Opis\Closure\SerializableClosure;
9
use RichardStyles\EloquentAES\Command\KeyGenerateCommand;
10
11
class EloquentAESServiceProvider extends EncryptionServiceProvider
12
{
13
    /**
14
     * Bootstrap the application services.
15
     */
16
    public function boot()
17
    {
18
        if ($this->app->runningInConsole()) {
19
            $this->publishes([
20
                __DIR__ . '/../config/eloquentaes.php' => config_path('eloquentaes.php'),
21
            ], 'config');
22
23
            // Registering package commands.
24
            $this->commands([
25
                KeyGenerateCommand::class,
26
            ]);
27
        }
28
    }
29
30
    /**
31
     * Register the application services.
32
     */
33
    public function register()
34
    {
35
        // Automatically apply the package configuration
36
        $this->mergeConfigFrom(__DIR__ . '/../config/eloquentaes.php', 'eloquentaes');
37
38
        $this->registerEncryptor();
39
        $this->registerOpisSecurityKey();
40
    }
41
42
    /**
43
     * Register the encrypter.
44
     *
45
     * @return void
46
     */
47
    protected function registerEncryptor()
48
    {
49
        $this->app->singleton('eloquentaes', function ($app) {
50
            $config = $app->make('config')->get('eloquentaes');
51
52
            return new Encrypter($this->parseKey($config), $config['cipher']);
53
        });
54
    }
55
56
    /**
57
     * Configure Opis Closure signing for security.
58
     *
59
     * @return void
60
     */
61
    protected function registerOpisSecurityKey()
62
    {
63
        $config = $this->app->make('config')->get('eloquentaes');
64
65
        if (! class_exists(SerializableClosure::class) || empty($config['key'])) {
66
            return;
67
        }
68
69
        SerializableClosure::setSecretKey($this->parseKey($config));
70
    }
71
72
    /**
73
     * Extract the encryption key from the given configuration.
74
     *
75
     * @param  array  $config
76
     * @return string
77
     *
78
     * @throws \RuntimeException
79
     */
80
    protected function key(array $config)
81
    {
82
        return tap($config['key'], function ($key) {
83
            if (empty($key)) {
84
                throw new MissingAppKeyException(
85
                    "No eloquent encryption key has been specified."
86
                );
87
            }
88
        });
89
    }
90
}