Passed
Push — master ( e5c411...122de6 )
by Stream
03:47
created

LfmItem::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace UniSharp\LaravelFilemanager;
4
5
use Illuminate\Support\Str;
6
use Symfony\Component\HttpFoundation\File\UploadedFile;
7
8
class LfmItem
9
{
10
    private $lfm;
11
    private $helper;
12
13
    private $columns = ['name', 'url', 'time', 'icon', 'is_file', 'is_image', 'thumb_url'];
14
    public $attributes = [];
15
16
    public function __construct(LfmPath $lfm, Lfm $helper)
17
    {
18
        $this->lfm = $lfm->thumb(false);
19
        $this->helper = $helper;
20
    }
21
22
    public function __get($var_name)
23
    {
24
        if (!array_key_exists($var_name, $this->attributes)) {
25
            $function_name = Str::camel($var_name);
26
            $this->attributes[$var_name] = $this->$function_name();
27
        }
28
29
        return $this->attributes[$var_name];
30
    }
31
32
    public function fill()
33
    {
34
        foreach ($this->columns as $column) {
35
            $this->__get($column);
36
        }
37
38
        return $this;
39
    }
40
41
    public function name()
42
    {
43
        return $this->lfm->getName();
44
    }
45
46
    public function path($type = 'absolute')
47
    {
48
        return $this->lfm->path($type);
49
    }
50
51
    public function isDirectory()
52
    {
53
        return $this->lfm->isDirectory();
54
    }
55
56
    public function isFile()
57
    {
58
        return ! $this->isDirectory();
59
    }
60
61
    /**
62
     * Check a file is image or not.
63
     *
64
     * @param  mixed  $file  Real path of a file or instance of UploadedFile.
65
     * @return bool
66
     */
67
    public function isImage()
68
    {
69
        if (!$this->isDirectory()) {
70
            return Str::startsWith($this->mimeType(), 'image');
71
        }
72
73
        return false;
74
    }
75
76
    /**
77
     * Get mime type of a file.
78
     *
79
     * @param  mixed  $file  Real path of a file or instance of UploadedFile.
80
     * @return string
81
     */
82
    // TODO: uploaded file
83
    public function mimeType()
84
    {
85
        // if ($file instanceof UploadedFile) {
86
        //     return $file->getMimeType();
87
        // }
88
89
        return $this->lfm->mimeType();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->lfm->mimeType() could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
Bug introduced by
The method mimeType() does not exist on UniSharp\LaravelFilemanager\LfmPath. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

89
        return $this->lfm->/** @scrutinizer ignore-call */ mimeType();
Loading history...
90
    }
91
92
    public function extension()
93
    {
94
        return $this->lfm->extension();
0 ignored issues
show
Bug introduced by
The method extension() does not exist on UniSharp\LaravelFilemanager\LfmPath. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

94
        return $this->lfm->/** @scrutinizer ignore-call */ extension();
Loading history...
95
    }
96
97
    public function url()
98
    {
99
        if ($this->isDirectory()) {
100
            return $this->lfm->path('working_dir');
101
        }
102
103
        return $this->lfm->url();
104
    }
105
106
    public function size()
107
    {
108
        return $this->isFile() ? $this->humanFilesize($this->lfm->size()) : '';
0 ignored issues
show
Bug introduced by
The method size() does not exist on UniSharp\LaravelFilemanager\LfmPath. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

108
        return $this->isFile() ? $this->humanFilesize($this->lfm->/** @scrutinizer ignore-call */ size()) : '';
Loading history...
109
    }
110
111
    public function time()
112
    {
113
        if (!$this->isDirectory()) {
114
            return $this->lfm->lastModified();
0 ignored issues
show
Bug introduced by
The method lastModified() does not exist on UniSharp\LaravelFilemanager\LfmPath. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

114
            return $this->lfm->/** @scrutinizer ignore-call */ lastModified();
Loading history...
115
        }
116
117
        return false;
118
    }
119
120
    public function thumbUrl()
121
    {
122
        if ($this->isDirectory()) {
123
            return asset('vendor/' . Lfm::PACKAGE_NAME . '/img/folder.png');
0 ignored issues
show
Bug introduced by
The function asset 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

123
            return /** @scrutinizer ignore-call */ asset('vendor/' . Lfm::PACKAGE_NAME . '/img/folder.png');
Loading history...
124
        }
125
126
        if ($this->isImage()) {
127
            return $this->lfm->thumb($this->hasThumb())->url(true);
0 ignored issues
show
Unused Code introduced by
The call to UniSharp\LaravelFilemanager\LfmPath::url() has too many arguments starting with true. ( Ignorable by Annotation )

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

127
            return $this->lfm->thumb($this->hasThumb())->/** @scrutinizer ignore-call */ url(true);

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...
128
        }
129
130
        return null;
131
    }
132
133
    public function icon()
134
    {
135
        if ($this->isDirectory()) {
136
            return 'fa-folder-o';
137
        }
138
139
        if ($this->isImage()) {
140
            return 'fa-image';
141
        }
142
143
        return $this->extension();
144
    }
145
146
    public function type()
147
    {
148
        if ($this->isDirectory()) {
149
            return trans(Lfm::PACKAGE_NAME . '::lfm.type-folder');
0 ignored issues
show
Bug introduced by
The function trans 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

149
            return /** @scrutinizer ignore-call */ trans(Lfm::PACKAGE_NAME . '::lfm.type-folder');
Loading history...
150
        }
151
152
        if ($this->isImage()) {
153
            return $this->mimeType();
154
        }
155
156
        return $this->helper->getFileType($this->extension());
157
    }
158
159
    public function hasThumb()
160
    {
161
        if (!$this->isImage()) {
162
            return false;
163
        }
164
165
        if (!$this->lfm->thumb()->exists()) {
0 ignored issues
show
Bug introduced by
The method exists() does not exist on UniSharp\LaravelFilemanager\LfmPath. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

165
        if (!$this->lfm->thumb()->/** @scrutinizer ignore-call */ exists()) {
Loading history...
166
            return false;
167
        }
168
169
        return true;
170
    }
171
172
    public function shouldCreateThumb()
173
    {
174
        if (!$this->helper->config('should_create_thumbnails')) {
175
            return false;
176
        }
177
178
        if (!$this->isImage()) {
179
            return false;
180
        }
181
182
        if (in_array($this->mimeType(), ['image/gif', 'image/svg+xml'])) {
183
            return false;
184
        }
185
186
        return true;
187
    }
188
189
    public function get()
190
    {
191
        return $this->lfm->get();
0 ignored issues
show
Bug introduced by
The method get() does not exist on UniSharp\LaravelFilemanager\LfmPath. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

191
        return $this->lfm->/** @scrutinizer ignore-call */ get();
Loading history...
192
    }
193
194
    /**
195
     * Make file size readable.
196
     *
197
     * @param  int  $bytes     File size in bytes.
198
     * @param  int  $decimals  Decimals.
199
     * @return string
200
     */
201
    public function humanFilesize($bytes, $decimals = 2)
202
    {
203
        $size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
204
        $factor = floor((strlen($bytes) - 1) / 3);
205
206
        return sprintf("%.{$decimals}f %s", $bytes / pow(1024, $factor), @$size[$factor]);
207
    }
208
}
209