Completed
Push — master ( 2ffbee...6662a0 )
by recca
03:16
created

Options::getRoots()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0729

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 1
nop 0
dl 0
loc 19
ccs 12
cts 14
cp 0.8571
crap 5.0729
rs 8.8571
c 0
b 0
f 0
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 \Recca0120\Elfinder\LaravelSession $session
0 ignored issues
show
Bug introduced by
There is no parameter named $session. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

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