Passed
Pull Request — 2.x (#1397)
by Harings
11:55
created

Block::renderNamedBlocks()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 1
nop 4
dl 0
loc 20
ccs 0
cts 0
cp 0
crap 6
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Models;
4
5
use A17\Twill\Models\Behaviors\HasFiles;
6
use A17\Twill\Models\Behaviors\HasMedias;
7
use A17\Twill\Models\Behaviors\HasPresenter;
8
use A17\Twill\Models\Behaviors\HasRelated;
9
use Illuminate\Database\Eloquent\Model as BaseModel;
10
11
class Block extends BaseModel
12
{
13
    use HasMedias, HasFiles, HasPresenter, HasRelated;
0 ignored issues
show
introduced by
The trait A17\Twill\Models\Behaviors\HasFiles requires some properties which are not provided by A17\Twill\Models\Block: $role, $pivot, $uuid, $files, $locale
Loading history...
introduced by
The trait A17\Twill\Models\Behaviors\HasMedias requires some properties which are not provided by A17\Twill\Models\Block: $medias, $role, $pivot, $metadatas, $uuid, $height, $lqip_data, $crop_h, $locale, $video, $crop, $crop_w, $width
Loading history...
introduced by
The trait A17\Twill\Models\Behaviors\HasRelated requires some properties which are not provided by A17\Twill\Models\Block: $relatedItems, $related
Loading history...
14
15
    public $timestamps = false;
16
17
    protected $fillable = [
18
        'blockable_id',
19
        'blockable_type',
20
        'position',
21
        'content',
22
        'type',
23
        'child_key',
24
        'parent_id',
25
        'editor_name',
26
    ];
27
28
    protected $casts = [
29
        'content' => 'array',
30
    ];
31
32
    protected $with = ['medias'];
33
34
    public function scopeEditor($query, $name = 'default')
35
    {
36
        return $name === 'default' ?
37
            $query->where('editor_name', $name)->orWhereNull('editor_name') :
38
            $query->where('editor_name', $name);
39
    }
40
41
    public function blockable()
42
    {
43
        return $this->morphTo();
44
    }
45
46
    public function children()
47
    {
48
        return $this->hasMany('A17\Twill\Models\Block', 'parent_id');
49
    }
50
51
    public function input($name)
52
    {
53
        return $this->content[$name] ?? null;
0 ignored issues
show
Bug introduced by
The property content does not seem to exist on A17\Twill\Models\Block. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
54
    }
55
56
    public function translatedInput($name, $forceLocale = null)
57
    {
58
        $value = $this->content[$name] ?? null;
0 ignored issues
show
Bug introduced by
The property content does not seem to exist on A17\Twill\Models\Block. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
59
60
        $locale = $forceLocale ?? (
61
            config('translatable.use_property_fallback', false) && (!array_key_exists(app()->getLocale(), $value ?? []))
0 ignored issues
show
introduced by
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

61
            config('translatable.use_property_fallback', false) && (!array_key_exists(app()->/** @scrutinizer ignore-call */ getLocale(), $value ?? []))
Loading history...
62
            ? config('translatable.fallback_locale')
63
            : app()->getLocale()
64
        );
65
66
        return $value[$locale] ?? null;
67
    }
68
69
    public function browserIds($name)
70
    {
71
        return isset($this->content['browsers']) ? ($this->content['browsers'][$name] ?? []) : [];
0 ignored issues
show
Bug introduced by
The property content does not seem to exist on A17\Twill\Models\Block. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
72
    }
73
74
    public function checkbox($name)
75
    {
76
        return isset($this->content[$name]) && ($this->content[$name][0] ?? $this->content[$name] ?? false);
0 ignored issues
show
Bug introduced by
The property content does not seem to exist on A17\Twill\Models\Block. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
77
    }
78
79
    public function renderBlocks($editor, $renderChilds = false, $blockViewMappings = [], $data = []): string
80 22
    {
81
        return $this->renderNamedBlocks($editor, $renderChilds, $blockViewMappings, $data);
82 22
    }
83
84
    public function renderNamedBlocks($editor, $renderChilds = false, $blockViewMappings = [], $data = []): string
85
    {
86
        $blocks = self::where('parent_id', $this->id)->where('child_key', $editor)->get();
87
88
        return $blocks->map(function ($block) use ($blockViewMappings, $renderChilds, $data) {
89
                if ($renderChilds) {
90
                    $childBlocks = self::where('parent_id', $block->id)->get();
91
92
                    $renderedChildViews = $childBlocks->map(function ($childBlock) use ($blockViewMappings, $data) {
93
                        $view = $this->getBlockView($childBlock->type, $blockViewMappings);
94
                        return view($view, $data)->with('block', $childBlock)->render();
95
                    })->implode('');
96
                }
97
98
                $block->childs = self::where('parent_id', $block->id)->get();
99
100
                $view = $this->getBlockView($block->type, $blockViewMappings);
101
102
                return view($view, $data)->with('block', $block)->render() . ($renderedChildViews ?? '');
103
            })->implode('');
104
    }
105
106
    private function getBlockView($blockType, $blockViewMappings = []): string
107
    {
108
        $view = config('twill.block_editor.block_views_path') . '.' . $blockType;
109
110
        if (array_key_exists($blockType, $blockViewMappings)) {
111
            $view = $blockViewMappings[$blockType];
112
        }
113
114
        return $view;
115
    }
116
117
    public function getPresenterAttribute()
118
    {
119
        if (($presenter = config('twill.block_editor.block_presenter_path')) != null) {
120
            return $presenter;
121
        }
122
123
        return null;
124
    }
125
126
    public function getTable()
127
    {
128
        return config('twill.blocks_table', 'twill_blocks');
129
    }
130
}
131