Test Failed
Pull Request — master (#1143)
by
unknown
04:16
created

LfmStorageRepository::__call()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 2
b 0
f 0
nc 3
nop 2
dl 0
loc 15
rs 9.9666
1
<?php
2
3
namespace UniSharp\LaravelFilemanager;
4
5
use Illuminate\Support\Facades\Storage;
6
7
class LfmStorageRepository
8
{
9
    private $disk;
10
    private $path;
11
    private $helper;
12
13
    public function __construct($storage_path, $helper)
14
    {
15
        $this->helper = $helper;
16
        $this->disk = Storage::disk($this->helper->config('disk'));
17
        $this->path = $storage_path;
18
    }
19
20
    public function __call($function_name, $arguments)
21
    {
22
        if ($function_name == 'directories') {
23
            $directories = $this->disk->directories($this->path, ...$arguments);
0 ignored issues
show
Bug introduced by
$arguments is expanded, but the parameter $recursive of Illuminate\Filesystem\Fi...mAdapter::directories() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

23
            $directories = $this->disk->directories($this->path, /** @scrutinizer ignore-type */ ...$arguments);
Loading history...
24
            $config = $this->helper->config('private_folder_name');
25
26
            if(method_exists(app()->make($config), 'userAuthDirs')) {
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
            if(method_exists(/** @scrutinizer ignore-call */ app()->make($config), 'userAuthDirs')) {
Loading history...
27
                return $this->getAuthDirectories($directories, app()->make($config)->userAuthDirs());
28
            } else {
29
                return $directories;
30
            }
31
            
32
        } else {
33
            // TODO: check function exists
34
            return $this->disk->$function_name($this->path, ...$arguments);
35
        }
36
    }
37
38
    public function rootPath()
39
    {
40
        return $this->disk->path('');
41
    }
42
43
    public function move($new_lfm_path)
44
    {
45
        return $this->disk->move($this->path, $new_lfm_path->path('storage'));
46
    }
47
48
    public function save($file)
49
    {
50
        $nameint = strripos($this->path, "/");
51
        $nameclean = substr($this->path, $nameint + 1);
52
        $pathclean = substr_replace($this->path, "", $nameint);
53
        $this->disk->putFileAs($pathclean, $file, $nameclean);
0 ignored issues
show
Bug introduced by
It seems like $pathclean can also be of type array; however, parameter $path of Illuminate\Filesystem\Fi...temAdapter::putFileAs() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        $this->disk->putFileAs(/** @scrutinizer ignore-type */ $pathclean, $file, $nameclean);
Loading history...
54
    }
55
56
    public function url($path)
57
    {
58
        return $this->disk->url($path);
59
    }
60
61
    public function makeDirectory()
62
    {
63
        $this->disk->makeDirectory($this->path, ...func_get_args());
0 ignored issues
show
Unused Code introduced by
The call to Illuminate\Filesystem\Fi...dapter::makeDirectory() has too many arguments starting with func_get_args(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
        $this->disk->/** @scrutinizer ignore-call */ 
64
                     makeDirectory($this->path, ...func_get_args());

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
64
65
        // some filesystems (e.g. Google Storage, S3?) don't let you set ACLs on directories (because they don't exist)
66
        // https://cloud.google.com/storage/docs/naming#object-considerations
67
        if ($this->disk->has($this->path)) {
0 ignored issues
show
Bug introduced by
The method has() does not exist on Illuminate\Contracts\Filesystem\Filesystem. It seems like you code against a sub-type of Illuminate\Contracts\Filesystem\Filesystem such as Illuminate\Filesystem\FilesystemAdapter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
        if ($this->disk->/** @scrutinizer ignore-call */ has($this->path)) {
Loading history...
68
            $this->disk->setVisibility($this->path, 'public');
69
        }
70
    }
71
72
    public function extension()
73
    {
74
        setlocale(LC_ALL, 'en_US.UTF-8');
75
        return pathinfo($this->path, PATHINFO_EXTENSION);
76
    }
77
78
    public function getAuthDirectories($directories, $auth_dirs)
79
    {
80
        $check = $directories;
81
82
        if(count($auth_dirs)){
83
            // remove unauthorized directories
84
            foreach($check as $i => $directory) {
85
                if(!in_array($directory, $auth_dirs)){
86
                    unset($directories[$i]);
87
                }
88
            }
89
        }
90
91
        return $directories;
92
    }
93
}
94