| Total Complexity | 43 |
| Total Lines | 202 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 1 |
Complex classes like LfmItem often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LfmItem, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class LfmItem |
||
| 9 | { |
||
| 10 | private $lfm; |
||
| 11 | private $helper; |
||
| 12 | private $isDirectory; |
||
| 13 | private $mimeType = null; |
||
| 14 | |||
| 15 | private $columns = []; |
||
| 16 | public $attributes = []; |
||
| 17 | |||
| 18 | public function __construct(LfmPath $lfm, Lfm $helper, $isDirectory = false) |
||
| 19 | { |
||
| 20 | $this->lfm = $lfm->thumb(false); |
||
| 21 | $this->helper = $helper; |
||
| 22 | $this->isDirectory = $isDirectory; |
||
| 23 | $this->columns = $helper->config('item_columns') ?: |
||
| 24 | ['name', 'url', 'time', 'icon', 'is_file', 'is_image', 'thumb_url']; |
||
| 25 | } |
||
| 26 | |||
| 27 | public function __get($var_name) |
||
| 28 | { |
||
| 29 | if (!array_key_exists($var_name, $this->attributes)) { |
||
| 30 | $function_name = Str::camel($var_name); |
||
| 31 | $this->attributes[$var_name] = $this->$function_name(); |
||
| 32 | } |
||
| 33 | |||
| 34 | return $this->attributes[$var_name]; |
||
| 35 | } |
||
| 36 | |||
| 37 | public function fill() |
||
| 38 | { |
||
| 39 | foreach ($this->columns as $column) { |
||
| 40 | $this->__get($column); |
||
| 41 | } |
||
| 42 | |||
| 43 | return $this; |
||
| 44 | } |
||
| 45 | |||
| 46 | public function name() |
||
| 47 | { |
||
| 48 | return $this->lfm->getName(); |
||
| 49 | } |
||
| 50 | |||
| 51 | public function path($type = 'absolute') |
||
| 52 | { |
||
| 53 | return $this->lfm->path($type); |
||
| 54 | } |
||
| 55 | |||
| 56 | public function isDirectory() |
||
| 57 | { |
||
| 58 | return $this->isDirectory; |
||
| 59 | } |
||
| 60 | |||
| 61 | public function isFile() |
||
| 62 | { |
||
| 63 | return ! $this->isDirectory(); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Check a file is image or not. |
||
| 68 | * |
||
| 69 | * @param mixed $file Real path of a file or instance of UploadedFile. |
||
| 70 | * @return bool |
||
| 71 | */ |
||
| 72 | public function isImage() |
||
| 73 | { |
||
| 74 | return $this->isFile() && Str::startsWith($this->mimeType(), 'image'); |
||
| 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 | public function mimeType() |
||
| 84 | { |
||
| 85 | if (is_null($this->mimeType)) { |
||
| 86 | $this->mimeType = $this->lfm->mimeType(); |
||
|
|
|||
| 87 | } |
||
| 88 | |||
| 89 | return $this->mimeType; |
||
| 90 | } |
||
| 91 | |||
| 92 | public function extension() |
||
| 93 | { |
||
| 94 | return $this->lfm->extension(); |
||
| 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() |
||
| 109 | } |
||
| 110 | |||
| 111 | public function time() |
||
| 112 | { |
||
| 113 | if (function_exists('config')) { |
||
| 114 | $disk = $this->helper->config('disk'); |
||
| 115 | $driver = $disk ? config("filesystems.disks.$disk.driver") : null; |
||
| 116 | if (($driver == 'bunny' || $driver == 'bunnycdn') && $this->isDirectory()) { |
||
| 117 | return null; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | return $this->lfm->lastModified(); |
||
| 121 | } |
||
| 122 | |||
| 123 | public function thumbUrl() |
||
| 124 | { |
||
| 125 | if ($this->isDirectory()) { |
||
| 126 | return asset('vendor/' . Lfm::PACKAGE_NAME . '/img/folder.png'); |
||
| 127 | } |
||
| 128 | |||
| 129 | if ($this->isImage()) { |
||
| 130 | return $this->lfm->thumb($this->hasThumb())->url(true); |
||
| 131 | } |
||
| 132 | |||
| 133 | return null; |
||
| 134 | } |
||
| 135 | |||
| 136 | public function icon() |
||
| 137 | { |
||
| 138 | if ($this->isDirectory()) { |
||
| 139 | return 'fa-folder-o'; |
||
| 140 | } |
||
| 141 | |||
| 142 | if ($this->isImage()) { |
||
| 143 | return 'fa-image'; |
||
| 144 | } |
||
| 145 | |||
| 146 | return $this->extension(); |
||
| 147 | } |
||
| 148 | |||
| 149 | public function type() |
||
| 150 | { |
||
| 151 | if ($this->isDirectory()) { |
||
| 152 | return trans(Lfm::PACKAGE_NAME . '::lfm.type-folder'); |
||
| 153 | } |
||
| 154 | |||
| 155 | if ($this->isImage()) { |
||
| 156 | return $this->mimeType(); |
||
| 157 | } |
||
| 158 | |||
| 159 | return $this->helper->getFileType($this->extension()); |
||
| 160 | } |
||
| 161 | |||
| 162 | public function hasThumb() |
||
| 163 | { |
||
| 164 | if (!$this->isImage()) { |
||
| 165 | return false; |
||
| 166 | } |
||
| 167 | |||
| 168 | if (!$this->lfm->thumb()->exists()) { |
||
| 169 | return false; |
||
| 170 | } |
||
| 171 | |||
| 172 | return true; |
||
| 173 | } |
||
| 174 | |||
| 175 | public function shouldCreateThumb() |
||
| 190 | } |
||
| 191 | |||
| 192 | public function get() |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Make file size readable. |
||
| 199 | * |
||
| 200 | * @param int $bytes File size in bytes. |
||
| 201 | * @param int $decimals Decimals. |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | public function humanFilesize($bytes, $decimals = 2) |
||
| 212 |