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

ListBlocks::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 2
rs 10
1
<?php
2
3
namespace A17\Twill\Commands;
4
5
use Illuminate\Support\Str;
6
use A17\Twill\Services\Blocks\Block;
7
use A17\Twill\Services\Blocks\BlockCollection;
8
9
class ListBlocks extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature =
17
        'twill:list:blocks {filter?}' .
18
        '{--t|--twill : List only Twill\'s internal blocks} ' .
19
        '{--c|--custom : List only user custom blocks} ' .
20
        '{--a|--app : List only legacy application blocks}' .
21
        '{--b|--blocks : List only blocks}' .
22
        '{--r|--repeaters : List only repeaters}';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'List all available Twill blocks';
30
31
    /**
32
     * Blocks collection.
33
     *
34
     * @var BlockCollection
35
     */
36
    protected $blockCollection;
37
38 68
    public function __construct(BlockCollection $blockCollection)
39
    {
40 68
        parent::__construct();
41
42 68
        $this->blockCollection = $blockCollection;
43 68
    }
44
45 3
    protected function displayMissingDirectories(): void
46
    {
47 3
        $this->blockCollection
48 3
            ->getMissingDirectories()
49
            ->each(function ($directory) {
50
                $this->error("Directory not found: {$directory}");
51 3
            });
52 3
    }
53
54
    /**
55
     * @param \Illuminate\Support\Collection $blockCollection
56
     * @return mixed
57
     */
58 2
    protected function generateHeaders($blockCollection)
59
    {
60
        return $blockCollection
61 2
            ->first()
62 2
            ->keys()
63
            ->map(function ($key) {
64 2
                return Str::studly($key);
65 2
            })
66 2
            ->toArray();
67
    }
68
69
    /**
70
     * @return \Illuminate\Support\Collection
71
     */
72 3
    protected function getBlocks()
73
    {
74
        $sourceFiltered =
75 3
            $this->option('twill') ||
76 3
            $this->option('custom') ||
77 3
            $this->option('app');
78
79 3
        $typeFiltered = $this->option('blocks') || $this->option('repeaters');
80
81 3
        return $this->blockCollection
82 3
            ->collect()
83
            ->reject(function (Block $block) use ($sourceFiltered) {
84 3
                return $sourceFiltered && !$this->option($block->source);
85 3
            })
86
            ->reject(function (Block $block) use ($typeFiltered) {
87 3
                return $this->dontPassTextFilter($block) ||
88 2
                    ($typeFiltered &&
89 3
                        !$this->option(Str::plural($block->type)));
90 3
            })
91
            ->map(function (Block $block) {
92 2
                return $this->colorize($block->toList());
93 3
            })
94 3
            ->sortBy('title');
95
    }
96
97
    /**
98
     * @return \A17\Twill\Services\Blocks\BlockCollection
99
     */
100 3
    public function getBlockCollection()
101
    {
102 3
        return $this->blockCollection;
103
    }
104
105
    /**
106
     * Executes the console command.
107
     *
108
     * @return void
109
     */
110 3
    public function handle()
111
    {
112 3
        $blockCollection = $this->getBlocks();
113
114 3
        $this->displayMissingDirectories();
115
116 3
        if ($blockCollection->isEmpty()) {
117 1
            $this->error('No blocks found.');
118
119 1
            return;
120
        }
121
122 2
        $this->table(
123 2
            $this->generateHeaders($blockCollection),
124 2
            $blockCollection->toArray()
125
        );
126 2
    }
127
128
    /**
129
     * @param $block
130
     * @return mixed
131
     */
132 2
    public function colorize($block)
133
    {
134 2
        $color = $block['type'] === 'repeater' ? 'green' : 'yellow';
135
136 2
        $block['type'] = "<fg={$color}>{$block['type']}</>";
137
138 2
        return $block;
139
    }
140
141
    /**
142
     * @param \A17\Twill\Services\Blocks\Block $block
143
     * @return bool
144
     */
145 3
    public function dontPassTextFilter(Block $block)
146
    {
147 3
        if (filled($filter = $this->argument('filter'))) {
148
            return !$block
149 2
                ->toList()
150
                ->reduce(function ($keep, $element) use ($filter) {
151 2
                    return $keep ||
152 2
                        Str::contains(
153 2
                            Str::lower($element),
154 2
                            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

154
                            Str::lower(/** @scrutinizer ignore-type */ $filter)
Loading history...
155
                        );
156 2
                }, false);
157
        }
158
159 1
        return false;
160
    }
161
}
162