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

ListBlocks::getBlocks()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 16
c 0
b 0
f 0
nc 6
nop 0
dl 0
loc 23
ccs 0
cts 16
cp 0
crap 56
rs 8.8333
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 1
    public function __construct(BlockCollection $blockCollection)
39
    {
40 1
        parent::__construct();
41
42 1
        $this->blockCollection = $blockCollection;
43 1
    }
44
45
    protected function displayMissingDirectories(): void
46
    {
47
        $this->blockCollection
48
            ->getMissingDirectories()
49
            ->each(function ($directory) {
50
                $this->error("Directory not found: {$directory}");
51
            });
52
    }
53
54
    /**
55
     * @param \Illuminate\Support\Collection $blockCollection
56
     * @return mixed
57
     */
58
    protected function generateHeaders($blockCollection)
59
    {
60
        return $blockCollection
61
            ->first()
62
            ->keys()
63
            ->map(function ($key) {
64
                return Str::studly($key);
65
            })
66
            ->toArray();
67
    }
68
69
    /**
70
     * @return \Illuminate\Support\Collection
71
     */
72
    protected function getBlocks()
73
    {
74
        $sourceFiltered =
75
            $this->option('twill') ||
76
            $this->option('custom') ||
77
            $this->option('app');
78
79
        $typeFiltered = $this->option('blocks') || $this->option('repeaters');
80
81
        return $this->blockCollection
82
            ->collect()
83
            ->reject(function (Block $block) use ($sourceFiltered) {
84
                return $sourceFiltered && !$this->option($block->source);
85
            })
86
            ->reject(function (Block $block) use ($typeFiltered) {
87
                return $this->dontPassTextFilter($block) ||
88
                    ($typeFiltered &&
89
                        !$this->option(Str::plural($block->type)));
90
            })
91
            ->map(function (Block $block) {
92
                return $this->colorize($block->toList());
93
            })
94
            ->sortBy('title');
95
    }
96
97
    /**
98
     * @return \A17\Twill\Services\Blocks\BlockCollection
99
     */
100
    public function getBlockCollection()
101
    {
102
        return $this->blockCollection;
103
    }
104
105
    /**
106
     * Executes the console command.
107
     *
108
     * @return void
109
     */
110
    public function handle()
111
    {
112
        $blockCollection = $this->getBlocks();
113
114
        $this->displayMissingDirectories();
115
116
        if ($blockCollection->isEmpty()) {
117
            $this->error('No blocks found.');
118
119
            return;
120
        }
121
122
        $this->table(
123
            $this->generateHeaders($blockCollection),
124
            $blockCollection->toArray()
125
        );
126
    }
127
128
    /**
129
     * @param $block
130
     * @return mixed
131
     */
132
    public function colorize($block)
133
    {
134
        $color = $block['type'] === 'repeater' ? 'green' : 'yellow';
135
136
        $block['type'] = "<fg={$color}>{$block['type']}</>";
137
138
        return $block;
139
    }
140
141
    /**
142
     * @param \A17\Twill\Services\Blocks\Block $block
143
     * @return bool
144
     */
145
    public function dontPassTextFilter(Block $block)
146
    {
147
        if (filled($filter = $this->argument('filter'))) {
148
            return !$block
149
                ->toList()
150
                ->reduce(function ($keep, $element) use ($filter) {
151
                    return $keep ||
152
                        Str::contains(
153
                            Str::lower($element),
154
                            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
                }, false);
157
        }
158
159
        return false;
160
    }
161
}
162