Passed
Pull Request — 2.x (#597)
by Antonio Carlos
05:31
created

ListIcons::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
namespace A17\Twill\Commands;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Collection;
7
use Illuminate\Filesystem\Filesystem;
8
use Illuminate\Config\Repository as Config;
9
use Symfony\Component\Finder\SplFileInfo;
10
11
class ListIcons extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'twill:list:icons {filter? : Filter icons by name}';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'List available icons';
26
27
    /**
28
     * @var Filesystem
29
     */
30
    protected $files;
31
32
    /**
33
     * @var Config
34
     */
35
    protected $config;
36
37
    /**
38
     * @param Filesystem $files
39
     * @param Config $config
40
     */
41 1
    public function __construct(Filesystem $files, Config $config)
42
    {
43 1
        parent::__construct();
44
45 1
        $this->files = $files;
46 1
        $this->config = $config;
47 1
    }
48
49
    private function isAllowed($icon)
50
    {
51
        if (filled($filter = $this->argument('filter'))) {
52
            return Str::contains(
53
                Str::lower($icon['name']),
54
                Str::lower($filter)
0 ignored issues
show
Bug introduced by
It seems like $filter can also be of type string[]; however, parameter $value of Illuminate\Support\Str::lower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
                Str::lower(/** @scrutinizer ignore-type */ $filter)
Loading history...
55
            );
56
        }
57
58
        return true;
59
    }
60
61
    /**
62
     * @return \Illuminate\Support\Collection
63
     */
64
    protected function getIconList()
65
    {
66
        return collect(
67
            config('twill.block_editor.directories.source.icons')
68
        )->reduce(function (Collection $keep, $path) {
69
            if (!$this->files->exists($path)) {
70
                $this->displayError("Directory not found: {$path}");
0 ignored issues
show
Bug introduced by
The method displayError() does not exist on A17\Twill\Commands\ListIcons. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
                $this->/** @scrutinizer ignore-call */ 
71
                       displayError("Directory not found: {$path}");
Loading history...
71
72
                return $keep;
73
            }
74
75
            $files = collect($this->files->files($path))->map(function (
76
                SplFileInfo $file
77
            ) {
78
                return [
79
                    'name' => Str::before($file->getFilename(), '.svg'),
80
                    'url' => route('admin.icons.show', [
81
                        'file' => $file->getFilename(),
82
                    ]),
83
                ];
84
            });
85
86
            return $keep->merge($files);
87
        }, collect());
88
    }
89
90
    /**
91
     * Executes the console command.
92
     */
93
    public function handle()
94
    {
95
        $icons = $this->getIconList()->filter(function ($icon) {
96
            return $this->isAllowed($icon);
97
        });
98
99
        $this->table(['Icon', 'Preview URL'], $icons->toArray());
100
101
        return parent::handle();
102
    }
103
}
104