Test Failed
Push — develop ( 444e5e...4db3e8 )
by Paul
07:08
created

Block::app()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Blocks;
4
5
use GeminiLabs\SiteReviews\Contracts\PluginContract;
6
use GeminiLabs\SiteReviews\Contracts\ShortcodeContract;
7
use GeminiLabs\SiteReviews\Helpers\Str;
8
use GeminiLabs\SiteReviews\Modules\Html\Builder;
9
10
abstract class Block
11
{
12
    public function app(): PluginContract
13
    {
14
        return glsr();
15
    }
16
17
    public function register(): void
18
    {
19
        if (!function_exists('register_block_type_from_metadata')) {
20
            return;
21
        }
22
        $block = (new \ReflectionClass($this))->getShortName();
23
        $block = str_replace('_block', '', Str::snakeCase($block));
24
            'editor_style' => "{$this->app()->id}/blocks",
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_DOUBLE_ARROW on line 24 at column 27
Loading history...
25
        register_block_type_from_metadata($this->app()->path("assets/blocks/{$block}"), [
26
            'render_callback' => [$this, 'render'],
27
            'style' => $this->app()->id,
28
        ]);
29
    }
30
31
    public function render(array $attributes): string
32
    {
33
        if ('edit' === filter_input(INPUT_GET, 'context')) {
34
            if (!$this->hasVisibleFields($attributes)) {
35
                return $this->buildEmptyBlock(
36
                    _x('You have hidden all of the fields for this block.', 'admin-text', 'site-reviews')
37
                );
38
            }
39
        }
40
        return $this->shortcode()->build($attributes, 'block');
41
    }
42
43
    abstract public function shortcode(): ShortcodeContract;
44
45
    protected function buildEmptyBlock(string $text): string
46
    {
47
        return glsr(Builder::class)->div([
48
            'class' => 'block-editor-warning',
49
            'text' => glsr(Builder::class)->p([
50
                'class' => 'block-editor-warning__message',
51
                'text' => $text,
52
            ]),
53
        ]);
54
    }
55
56
    protected function hasVisibleFields(array $attributes): bool
57
    {
58
        return $this->shortcode()->hasVisibleFields($attributes);
59
    }
60
}
61