Completed
Push — master ( e128d9...602fbb )
by recca
05:49 queued 02:31
created

Options::createTrashDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
1
<?php
2
3
namespace Recca0120\Elfinder;
4
5
use Closure;
6
use ArrayObject;
7
use Illuminate\Support\Arr;
8
use Illuminate\Http\Request;
9
use Illuminate\Filesystem\Filesystem;
10
use Illuminate\Contracts\Routing\UrlGenerator;
11
12
class Options extends ArrayObject
13
{
14
    /**
15
     * $request.
16
     *
17
     * @var \Illuminate\Http\Request
18
     */
19
    protected $request;
20
21
    /**
22
     * $files.
23
     *
24
     * @var \Illuminate\Filesystem\Filesystem
25
     */
26
    protected $files;
27
28
    /**
29
     * $request.
30
     *
31
     * @var \Illuminate\Contracts\Routing\UrlGenerator
32
     */
33
    protected $urlGenerator;
34
35
    /**
36
     * $config.
37
     *
38
     * @var array
39
     */
40
    protected $config;
41
42
    /**
43
     * $options.
44
     *
45
     * @var array
46
     */
47
    protected $options;
48
49
    /**
50
     * __construct.
51
     *
52
     * @param \Illuminate\Http\Request $request
53
     * @param \Illuminate\Filesystem\Filesystem $files
54
     * @param \Illuminate\Contracts\Routing\UrlGenerator $urlGenerator
55
     * @param array $config
56
     */
57 3
    public function __construct(Request $request, Filesystem $files, UrlGenerator $urlGenerator, $config = [])
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
58
    {
59 3
        $this->request = $request;
60 3
        $this->files = $files;
61 3
        $this->urlGenerator = $urlGenerator;
62 3
        $this->config = $config;
63 3
        $this->options = Arr::get($this->config, 'options', []);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Illuminate\Support\Arr:...ig, 'options', array()) of type * is incompatible with the declared type array of property $options.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
64
65 3
        parent::__construct(array_merge($this->options, [
66 3
            'roots' => $this->getRoots(),
67 3
        ]));
68 3
    }
69
70
    /**
71
     * getRoots.
72
     *
73
     * @return array
74
     */
75 3
    protected function getRoots()
76
    {
77 3
        $accessControl = Arr::get($this->config, 'accessControl');
78 3
        $roots = Arr::get($this->options, 'roots', []);
79 3
        $user = $this->request->user();
80
81 3
        return array_values(array_filter(array_map(function ($disk) use ($user, $accessControl) {
82 2
            $disk['driver'] = empty($disk['driver']) === true ? 'LocalFileSystem' : $disk['driver'];
83 2
            $disk['autoload'] = true;
84
85 2
            if (empty($disk['path']) === false && ($disk['path'] instanceof Closure) === true) {
86 1
                $disk['path'] = call_user_func($disk['path']);
87 1
            }
88 2
            $method = 'create'.$disk['driver'].'Driver';
89 2
            $method = method_exists($this, $method) === true ? $method : 'createDefaultDriver';
90
91 2
            return call_user_func_array([$this, $method], [$disk, $user, $accessControl]);
92 3
        }, $roots)));
93
    }
94
95
    /**
96
     * createDefaultDriver.
97
     *
98
     * @param array $disk
99
     * @param mixed $user
100
     * @param mixed $accessControl
101
     * @param bool $makeDirectory
102
     * @return array
103
     */
104 2
    protected function createDefaultDriver($disk, $user, $accessControl = null, $makeDirectory = false)
105
    {
106 2
        if (strpos($disk['path'], '{user_id}') !== false) {
107 2
            if (is_null($user) === true) {
108 1
                return;
109
            }
110
111 1
            $userId = $user->id;
112 1
            $disk['path'] = str_replace('{user_id}', $userId, $disk['path']);
113 1
            $disk['URL'] = str_replace('{user_id}', $userId, $disk['URL']);
114 1
        }
115
116 2
        if ($makeDirectory === true && $this->files->exists($disk['path']) === false) {
117 2
            $this->files->makeDirectory($disk['path'], 0755, true);
118 2
        }
119
120 2
        $disk['URL'] = $this->urlGenerator->to($disk['URL']);
121
122 2
        return array_merge([
123 2
            'accessControl' => $accessControl,
124 2
            'autoload' => true,
125 2
            'mimeDetect' => 'internal',
126 2
            'tmbBgColor' => 'transparent',
127 2
            'tmbCrop' => false,
128 2
            'utf8fix' => true,
129 2
        ], $disk);
130
    }
131
132
    /**
133
     * createLocalFileSystemDriver.
134
     *
135
     * @param  array $disk
136
     * @param  mixed $user
137
     * @param  mixed $accessControl
138
     * @return array
139
     */
140 2
    protected function createLocalFileSystemDriver($disk, $user, $accessControl = null)
141
    {
142 2
        return $this->createDefaultDriver($disk, $user, $accessControl, true);
143
    }
144
145
    /**
146
     * createTrashDriver.
147
     *
148
     * @param  array $disk
149
     * @param  mixed $user
150
     * @param  mixed $accessControl
151
     * @return array
152
     */
153 2
    protected function createTrashDriver($disk, $user, $accessControl = null)
154
    {
155 2
        return $this->createDefaultDriver(array_merge([
156 2
            'id' => 1,
157 2
        ], $disk), $user, $accessControl, true);
158
    }
159
}
160