FileBrowseController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Harentius\BlogBundle\Admin\Controller;
4
5
use Harentius\BlogBundle\FileManagement\FilePathResolver;
6
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
7
use Symfony\Component\Finder\Finder;
8
use Symfony\Component\Finder\SplFileInfo;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
12
class FileBrowseController extends AbstractController
13
{
14
    /**
15
     * @var FilePathResolver
16
     */
17
    private $assetsResolver;
18
19
    /**
20
     * FileBrowseController constructor.
21
     * @param FilePathResolver $assetsResolver
22
     */
23
    public function __construct(FilePathResolver $assetsResolver)
24
    {
25
        $this->assetsResolver = $assetsResolver;
26
    }
27
28
    /**
29
     * @param Request $request
30
     * @param string $type
31
     * @return Response
32
     */
33
    public function __invoke(Request $request, $type = 'image')
34
    {
35
        if (!in_array($type, ['image', 'audio'], true)) {
36
            throw new \InvalidArgumentException(sprintf("Unknown files type '%s", $type));
37
        }
38
39
        $directory = $this->assetsResolver->assetPath($type);
40
        $files = [];
41
        $finder = new Finder();
42
        $finder
43
            ->files()
44
            ->in($directory)
45
            ->ignoreDotFiles(true)
46
        ;
47
48
        /** @var SplFileInfo $file */
49
        foreach ($finder as $file) {
50
            $uri = $this->assetsResolver->pathToUri($file->getPathname());
51
52
            if ($uri) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $uri of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
53
                $files[$uri] = pathinfo($uri, PATHINFO_BASENAME);
54
            }
55
        }
56
57
        return $this->render(sprintf('@HarentiusBlog/Admin/ckeditor/ck_browse_%ss.html.twig', $type), [
58
            'func_num' => $request->query->get('CKEditorFuncNum'),
59
            'files' => $files,
60
        ]);
61
    }
62
}
63