Completed
Push — master ( e7cf2a...7ade54 )
by Arjay
11:03
created

ImageBrowserController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 0 Features 0
Metric Value
dl 0
loc 62
rs 10
c 4
b 0
f 0
wmc 7
lcom 1
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
B index() 0 35 5
A getImageFiles() 0 9 2
1
<?php
2
3
namespace Yajra\CMS\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Collection;
7
use Symfony\Component\Finder\Finder;
8
use Yajra\CMS\Http\NotificationResponse;
9
10
/**
11
 * Class ImageBrowserController
12
 *
13
 * @package Yajra\CMS\Http\Controllers
14
 */
15
class ImageBrowserController extends Controller
16
{
17
    use NotificationResponse;
18
19
    /**
20
     * Get files by directory path.
21
     *
22
     * @param \Illuminate\Http\Request $request
23
     * @return array
24
     */
25
    public function index(Request $request)
26
    {
27
        $currentPath = $request->get('path');
28
        $basePath    = storage_path('app/public/' . config('media.root_dir'));
0 ignored issues
show
Unused Code introduced by
$basePath is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
29
        $dir         = storage_path('app/public/' . $currentPath);
30
        $imageFiles  = $this->getImageFiles($dir);
31
32
        foreach (Finder::create()->in($dir)->sortByType()->directories() as $file) {
33
            $imageFiles->name($file->getBaseName());
34
        }
35
36
        $files = new Collection;
37
        $parts = explode('/', $currentPath);
38
        array_pop($parts);
39
        if ($parts <> '' && $currentPath <> 'media') {
40
            $files->push([
41
                'name'    => '.. Up',
42
                'relPath' => implode('/', $parts),
43
                'type'    => 'dir',
44
                'url'     => url($currentPath),
45
            ]);
46
        }
47
48
        foreach ($imageFiles as $file) {
49
            $path = str_replace(storage_path('app/public/'), '', $file->getRealPath());
50
            $files->push([
51
                'name'    => $file->getFilename(),
52
                'relPath' => $path,
53
                'type'    => $file->getType(),
54
                'url'     => url($path),
55
            ]);
56
        }
57
58
        return response()->json($files->groupBy('type'));
59
    }
60
61
    /**
62
     * Get image files by path.
63
     *
64
     * @param string $path
65
     * @return Finder
66
     */
67
    protected function getImageFiles($path)
68
    {
69
        $finder = Finder::create()->in($path)->sortByType()->depth(0);
70
        foreach (config('media.images_ext') as $file) {
71
            $finder->name('*' . $file);
72
        }
73
74
        return $finder;
75
    }
76
}
77