Passed
Pull Request — master (#3)
by Jasper
03:15
created

EncryptedDataServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 97.73%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 59
ccs 43
cts 44
cp 0.9773
rs 10
c 2
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setupStorageDriver() 0 36 2
A register() 0 3 1
A boot() 0 7 2
A registerEncrypter() 0 3 1
1
<?php
2
3
namespace Swis\Laravel\Encrypted;
4
5
use Illuminate\Contracts\Foundation\Application;
6
use Illuminate\Filesystem\FilesystemAdapter;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Facades\Storage;
9
use Illuminate\Support\ServiceProvider;
10
use League\Flysystem\Filesystem;
11
use League\Flysystem\Local\LocalFilesystemAdapter;
12
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
13
use League\Flysystem\Visibility;
14
use Swis\Flysystem\Encrypted\EncryptedFilesystemAdapter;
15
use Swis\Laravel\Encrypted\Commands\ReEncryptModels;
16
17
class EncryptedDataServiceProvider extends ServiceProvider
18
{
19 14
    public function register(): void
20
    {
21 14
        $this->registerEncrypter();
22 7
    }
23
24 14
    protected function registerEncrypter(): void
25
    {
26 14
        $this->app->alias('encrypter', 'encrypted-data.encrypter');
27 7
    }
28
29 14
    public function boot(): void
30
    {
31 14
        $this->setupStorageDriver();
32
33 14
        if ($this->app->runningInConsole()) {
34 14
            $this->commands([
35 14
                ReEncryptModels::class,
36 7
            ]);
37
        }
38 7
    }
39
40 14
    protected function setupStorageDriver(): void
41
    {
42 14
        Storage::extend(
43 14
            'local-encrypted',
44 14
            function (Application $app, array $config) {
45 2
                $visibility = PortableVisibilityConverter::fromArray(
46 2
                    $config['permissions'] ?? [],
47 2
                    $config['directory_visibility'] ?? $config['visibility'] ?? Visibility::PRIVATE
48 1
                );
49
50 2
                $links = ($config['links'] ?? null) === 'skip'
51
                    ? LocalFilesystemAdapter::SKIP_LINKS
52 2
                    : LocalFilesystemAdapter::DISALLOW_LINKS;
53
54 2
                $adapter = new EncryptedFilesystemAdapter(
55 2
                    new LocalFilesystemAdapter(
56 2
                        $config['root'],
57 1
                        $visibility,
58 2
                        $config['lock'] ?? LOCK_EX,
59 1
                        $links
60 1
                    ),
61 2
                    $app->make('encrypted-data.encrypter')
62 1
                );
63
64 2
                $driver = new Filesystem(
65 2
                    $adapter,
66 2
                    Arr::only($config, [
67 2
                        'directory_visibility',
68 1
                        'disable_asserts',
69 1
                        'temporary_url',
70 1
                        'url',
71 1
                        'visibility',
72 1
                    ])
73 1
                );
74
75 2
                return new FilesystemAdapter($driver, $adapter, $config);
76 14
            }
77 7
        );
78 7
    }
79
}
80