Test Failed
Push — master ( 241100...2372f3 )
by Stream
08:09 queued 10s
created

LfmItem   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 37
eloc 61
dl 0
loc 199
rs 9.44
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
use Illuminate\Support\Str;
0 ignored issues
show
Bug introduced by
A parse error occurred: Cannot use Illuminate\Support\Str as Str because the name is already in use
Loading history...
8
9
class LfmItem
10
{
11
    private $lfm;
12
    private $helper;
13
14
    private $columns = ['name', 'url', 'time', 'icon', 'is_file', 'is_image', 'thumb_url'];
15
    public $attributes = [];
16
17
    public function __construct(LfmPath $lfm, Lfm $helper)
18
    {
19
        $this->lfm = $lfm->thumb(false);
20
        $this->helper = $helper;
21
    }
22
23
    public function __get($var_name)
24
    {
25
        if (!array_key_exists($var_name, $this->attributes)) {
26
            $function_name = Str::camel($var_name);
27
            $this->attributes[$var_name] = $this->$function_name();
28
        }
29
30
        return $this->attributes[$var_name];
31
    }
32
33
    public function fill()
34
    {
35
        foreach ($this->columns as $column) {
36
            $this->__get($column);
37
        }
38
39
        return $this;
40
    }
41
42
    public function name()
43
    {
44
        return $this->lfm->getName();
45
    }
46
47
    public function path($type = 'absolute')
48
    {
49
        return $this->lfm->path($type);
50
    }
51
52
    public function isDirectory()
53
    {
54
        return $this->lfm->isDirectory();
55
    }
56
57
    public function isFile()
58
    {
59
        return ! $this->isDirectory();
60
    }
61
62
    /**
63
     * Check a file is image or not.
64
     *
65
     * @param  mixed  $file  Real path of a file or instance of UploadedFile.
66
     * @return bool
67
     */
68
    public function isImage()
69
    {
70
        if (!$this->isDirectory()) {
71
            return Str::startsWith($this->mimeType(), 'image');
72
        }
73
74
        return false;
75
    }
76
77
    /**
78
     * Get mime type of a file.
79
     *
80
     * @param  mixed  $file  Real path of a file or instance of UploadedFile.
81
     * @return string
82
     */
83
    // TODO: uploaded file
84
    public function mimeType()
85
    {
86
        // if ($file instanceof UploadedFile) {
87
        //     return $file->getMimeType();
88
        // }
89
90
        return $this->lfm->mimeType();
91
    }
92
93
    public function extension()
94
    {
95
        return $this->lfm->extension();
96
    }
97
98
    public function url()
99
    {
100
        if ($this->isDirectory()) {
101
            return $this->lfm->path('working_dir');
102
        }
103
104
        return $this->lfm->url();
105
    }
106
107
    public function size()
108
    {
109
        return $this->isFile() ? $this->humanFilesize($this->lfm->size()) : '';
110
    }
111
112
    public function time()
113
    {
114
        if (!$this->isDirectory()) {
115
            return $this->lfm->lastModified();
116
        }
117
118
        return false;
119
    }
120
121
    public function thumbUrl()
122
    {
123
        if ($this->isDirectory()) {
124
            return asset('vendor/' . Lfm::PACKAGE_NAME . '/img/folder.png');
125
        }
126
127
        if ($this->isImage()) {
128
            return $this->lfm->thumb($this->hasThumb())->url(true);
129
        }
130
131
        return null;
132
    }
133
134
    public function icon()
135
    {
136
        if ($this->isDirectory()) {
137
            return 'fa-folder-o';
138
        }
139
140
        if ($this->isImage()) {
141
            return 'fa-image';
142
        }
143
144
        return $this->extension();
145
    }
146
147
    public function type()
148
    {
149
        if ($this->isDirectory()) {
150
            return trans(Lfm::PACKAGE_NAME . '::lfm.type-folder');
151
        }
152
153
        if ($this->isImage()) {
154
            return $this->mimeType();
155
        }
156
157
        return $this->helper->getFileType($this->extension());
158
    }
159
160
    public function hasThumb()
161
    {
162
        if (!$this->isImage()) {
163
            return false;
164
        }
165
166
        if (!$this->lfm->thumb()->exists()) {
167
            return false;
168
        }
169
170
        return true;
171
    }
172
173
    public function shouldCreateThumb()
174
    {
175
        if (!$this->helper->config('should_create_thumbnails')) {
176
            return false;
177
        }
178
179
        if (!$this->isImage()) {
180
            return false;
181
        }
182
183
        if (in_array($this->mimeType(), ['image/gif', 'image/svg+xml'])) {
184
            return false;
185
        }
186
187
        return true;
188
    }
189
190
    public function get()
191
    {
192
        return $this->lfm->get();
193
    }
194
195
    /**
196
     * Make file size readable.
197
     *
198
     * @param  int  $bytes     File size in bytes.
199
     * @param  int  $decimals  Decimals.
200
     * @return string
201
     */
202
    public function humanFilesize($bytes, $decimals = 2)
203
    {
204
        $size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
205
        $factor = floor((strlen($bytes) - 1) / 3);
206
207
        return sprintf("%.{$decimals}f %s", $bytes / pow(1024, $factor), @$size[$factor]);
208
    }
209
}
210