Passed
Push — 2.x ( e8dd26...ba8025 )
by Quentin
07:25
created

IconsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Http\Controllers\Admin;
4
5
use A17\Twill\Services\Blocks\BlockMaker;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Support\Collection;
8
use Str;
9
use Symfony\Component\Finder\SplFileInfo;
10
11
class IconsController extends Controller
12
{
13
    public function __construct(Filesystem $files, BlockMaker $blockMaker)
14
    {
15
        $this->files = $files;
0 ignored issues
show
Bug Best Practice introduced by
The property files does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
16
        $this->blockMaker = $blockMaker;
0 ignored issues
show
Bug Best Practice introduced by
The property blockMaker does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
    }
18
19
    public function index()
20
    {
21
        $icons = collect(
22
            config('twill.block_editor.directories.source.icons')
23
        )->reduce(function (Collection $keep, $path) {
24
            if (!$this->files->exists($path)) {
25
                return $keep;
26
            }
27
28
            $files = collect($this->files->files($path))->map(function (
29
                SplFileInfo $file
30
            ) {
31
                if (in_array($file->getFilename(), config('twill.internal_icons'))) {
32
                    return null;
33
                }
34
35
                return [
36
                    'name' => Str::before($file->getFilename(), '.svg'),
37
                    'url' => route('admin.icons.show', [
38
                        'file' => $file->getFilename(),
39
                    ]),
40
                ];
41
            })->filter();
42
43
            return $keep->merge($files);
44
        }, collect());
45
46
        return view('twill::blocks.icons', compact('icons'));
47
    }
48
49
    public function show($file)
50
    {
51
        $file = $this->blockMaker->getIconFile($file, false);
52
53
        if (!$this->files->exists($file)) {
54
            abort(404);
55
        }
56
57
        return response()->stream(function () use ($file) {
58
            echo $this->files->get($file);
59
        }, 200, ["Content-Type" => "image/svg+xml"]);
60
61
    }
62
}
63