Completed
Push — master ( 9b124d...317457 )
by Jasper
03:01
created

EncryptedDataServiceProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 62
ccs 30
cts 32
cp 0.9375
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerStorageDriver() 0 22 2
A boot() 0 4 1
A registerResponseMacro() 0 24 2
1
<?php
2
3
namespace Swis\Laravel\Encrypted;
4
5
use Illuminate\Contracts\Foundation\Application;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Facades\Response;
8
use Illuminate\Support\Facades\Storage;
9
use Illuminate\Support\ServiceProvider;
10
use Illuminate\Support\Str;
11
use League\Flysystem\Adapter\Local as LocalAdapter;
12
use League\Flysystem\Filesystem;
13
use Swis\Flysystem\Encrypted\EncryptedAdapter;
14
15
class EncryptedDataServiceProvider extends ServiceProvider
16
{
17
    /**
18
     * @throws \LogicException
19
     */
20 60
    public function boot()
21
    {
22 60
        $this->registerStorageDriver();
23 60
        $this->registerResponseMacro();
24 60
    }
25
26 60
    protected function registerStorageDriver()
27
    {
28 60
        Storage::extend(
29 60
            'local-encrypted',
30
            function (Application $app, array $config) {
31 3
                $permissions = $config['permissions'] ?? [];
32
33 3
                $links = ($config['links'] ?? null) === 'skip'
34
                    ? LocalAdapter::SKIP_LINKS
35 3
                    : LocalAdapter::DISALLOW_LINKS;
36
37 3
                return new Filesystem(
38 3
                    new EncryptedAdapter(
39 3
                        new LocalAdapter(
40 3
                            $config['root'],
41 3
                            $config['lock'] ?? LOCK_EX,
42 1
                            $links,
43 1
                            $permissions
44
                        ),
45 3
                        $app->make('encrypter')
46
                    ),
47 3
                    Arr::only($config, ['visibility', 'disable_asserts', 'url'])
48
                );
49 60
            }
50
        );
51 60
    }
52
53 60
    protected function registerResponseMacro()
54
    {
55 60
        Response::macro(
56 60
            'downloadEncrypted',
57
            /**
58
             * @param \SplFileInfo|string $file
59
             * @param null $name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
60
             * @param array $headers
61
             * @param string $disposition
62
             *
63
             * @return \Swis\Laravel\Encrypted\BinaryFileResponse
64
             */
65
            function ($file, $name = null, array $headers = [], $disposition = 'attachment') {
66 3
                $response = new BinaryFileResponse($file, 200, $headers, false, $disposition);
67
68 3
                if (null !== $name) {
0 ignored issues
show
introduced by
The condition null !== $name is always false.
Loading history...
69 3
                    return $response->setContentDisposition(
70 3
                        $disposition,
71 1
                        $name,
72 3
                        str_replace('%', '', Str::ascii($name))
73
                    );
74
                }
75
76
                return $response;
77 60
            }
78
        );
79 60
    }
80
}
81