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

ListBlocks::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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

159
                        Str::lower(/** @scrutinizer ignore-type */ $filter)
Loading history...
160
                    );
161 2
                }, false);
162
        }
163
164 1
        return false;
165
    }
166
}
167