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

EncryptedDataServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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