KeyManagerServiceProvider   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 17 1
A boot() 0 39 4
A registerCipherSweet() 0 14 2
1
<?php
2
3
namespace Bytesfield\KeyManager;
4
5
use Bytesfield\KeyManager\Commands\ActivateApiCredentialCommand;
6
use Bytesfield\KeyManager\Commands\ActivateClientCommand;
7
use Bytesfield\KeyManager\Commands\ChangeKeysCommand;
8
use Bytesfield\KeyManager\Commands\CreateClientCommand;
9
use Bytesfield\KeyManager\Commands\GenerateEncryptionKeyCommand;
10
use Bytesfield\KeyManager\Commands\GetApiKeyCommand;
11
use Bytesfield\KeyManager\Commands\InstallKeyManagerCommand;
12
use Bytesfield\KeyManager\Commands\SuspendApiCredentialCommand;
13
use Bytesfield\KeyManager\Commands\SuspendClientCommand;
14
use Bytesfield\KeyManager\Exceptions\Handler;
15
use Bytesfield\KeyManager\Middlewares\AuthenticateClient;
16
use Illuminate\Routing\Router;
17
use Illuminate\Support\ServiceProvider;
18
use ParagonIE\CipherSweet\Backend\ModernCrypto;
19
use ParagonIE\CipherSweet\CipherSweet;
20
use ParagonIE\CipherSweet\KeyProvider\StringProvider;
21
22
class KeyManagerServiceProvider extends ServiceProvider
23
{
24
    /**
25
     * Register services.
26
     *
27
     * @return void
28
     */
29
    public function register()
30
    {
31
        $this->registerCipherSweet();
32
        $this->app->singleton(KeyManagerInterface::class, KeyManager::class);
33
34
        $this->app->singleton(
35
            \Illuminate\Contracts\Debug\ExceptionHandler::class,
36
            Handler::class
37
        );
38
39
        $this->mergeConfigFrom(__DIR__.'/config/keymanager.php', 'keymanager');
40
41
        $this->app->bind('key-manager', function ($app) {
42
            return new KeyManager($app->request);
0 ignored issues
show
Unused Code introduced by
The call to KeyManager::__construct() has too many arguments starting with $app->request.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
43
        });
44
        $this->app->alias('key-manager', "Bytesfield\KeyManager\KeyManager");
45
    }
46
47
    /**
48
     * Bootstrap services.
49
     *
50
     * @return void
51
     */
52
    public function boot()
53
    {
54
        //Loads Package Migration
55
        $this->loadMigrationsFrom(__DIR__.'/database/migrations');
56
57
        //Register Package Middleware
58
        $router = $this->app->make(Router::class);
59
        $router->aliasMiddleware('auth.client', AuthenticateClient::class);
60
61
        if ($this->app->runningInConsole()) {
62
63
            //Publishes Package Config
64
            $this->publishes([
65
                __DIR__.'/config/keymanager.php' => config_path('keymanager.php'),
66
            ], 'config');
67
68
            //Publishes Migrations
69
            if (! class_exists('CreateKeyClientsTable') && ! class_exists('CreateKeyApiCredentialsTable')) {
70
                $this->publishes([
71
                    __DIR__.'/database/migrations/2020_12_19_075709_create_key_clients_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'1'.'_create_key_clients_table.php'),
72
                    __DIR__.'/database/migrations/2020_12_19_075855_create_key_api_credentials_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'2'.'_create_key_api_credentials_table.php'),
73
                ], 'migrations');
74
            }
75
76
            //Register Package Console Commands
77
            $this->commands([
78
                GenerateEncryptionKeyCommand::class,
79
                CreateClientCommand::class,
80
                GetApiKeyCommand::class,
81
                ChangeKeysCommand::class,
82
                SuspendClientCommand::class,
83
                ActivateClientCommand::class,
84
                SuspendApiCredentialCommand::class,
85
                ActivateApiCredentialCommand::class,
86
                InstallKeyManagerCommand::class,
87
88
            ]);
89
        }
90
    }
91
92
    /**
93
     * Register CipherSweet library.
94
     *
95
     * @return void
96
     */
97
    private function registerCipherSweet(): void
98
    {
99
        $this->app->singleton(CipherSweet::class, function () {
100
            $key = config('keymanager.api_encryption_key');
101
102
            if (empty($key)) {
103
                throw new \RuntimeException(
104
                    'Encryption key for key manager service is not specified.'
105
                );
106
            }
107
108
            return new CipherSweet(new StringProvider($key), new ModernCrypto());
109
        });
110
    }
111
}
112