Passed
Push — dependabot/npm_and_yarn/docs/u... ( 28266c )
by
unknown
10:55 queued 02:13
created

HasBlocks::renderBlocks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Models\Behaviors;
4
5
use A17\Twill\Models\Block;
6
7
trait HasBlocks
8
{
9
    /**
10
     * Defines the one-to-many relationship for block objects.
11
     *
12
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
13
     */
14
    public function blocks()
15
    {
16
        return $this->morphMany(Block::class, 'blockable')->orderBy(config('twill.blocks_table', 'twill_blocks') . '.position', 'asc');
0 ignored issues
show
Bug introduced by
It seems like morphMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

16
        return $this->/** @scrutinizer ignore-call */ morphMany(Block::class, 'blockable')->orderBy(config('twill.blocks_table', 'twill_blocks') . '.position', 'asc');
Loading history...
17
    }
18
19
    public function renderNamedBlocks($name = 'default', $renderChilds = true, $blockViewMappings = [], $data = [])
20
    {
21
        return $this->blocks
22
            ->filter(function ($block) use ($name) {
23
                return $name === 'default'
24
                ? ($block->editor_name === $name || $block->editor_name === null)
25
                : $block->editor_name === $name;
26
            })
27
            ->where('parent_id', null)
28
            ->map(function ($block) use ($blockViewMappings, $renderChilds, $data) {
29
                if ($renderChilds) {
30
                    $childBlocks = $this->blocks->where('parent_id', $block->id);
31
32
                    $renderedChildViews = $childBlocks->map(function ($childBlock) use ($blockViewMappings, $data) {
33
                        $view = $this->getBlockView($childBlock->type, $blockViewMappings);
34
                        return view($view, $data)->with('block', $childBlock)->render();
35
                    })->implode('');
36
                }
37
38
                $block->childs = $this->blocks->where('parent_id', $block->id);
39
40
                $view = $this->getBlockView($block->type, $blockViewMappings);
41
42
                return view($view, $data)->with('block', $block)->render() . ($renderedChildViews ?? '');
0 ignored issues
show
Bug introduced by
Are you sure view($view, $data)->with...ock', $block)->render() of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

42
                return /** @scrutinizer ignore-type */ view($view, $data)->with('block', $block)->render() . ($renderedChildViews ?? '');
Loading history...
43
            })->implode('');
44
    }
45
46
    /**
47
     * Returns the rendered Blade views for all attached blocks in their proper order.
48
     *
49
     * @param bool $renderChilds Include all child blocks in the rendered output.
50
     * @param array $blockViewMappings Provide alternate Blade views for blocks. Format: `['block-type' => 'view.path']`.
51
     * @param array $data Provide extra data to Blade views.
52
     * @return string
53
     */
54
    public function renderBlocks($renderChilds = true, $blockViewMappings = [], $data = [])
55
    {
56
        return $this->renderNamedBlocks('default', $renderChilds, $blockViewMappings, $data);
57
    }
58
59
    private function getBlockView($blockType, $blockViewMappings = [])
60
    {
61
        $view = config('twill.block_editor.block_views_path') . '.' . $blockType;
62
63
        if (array_key_exists($blockType, $blockViewMappings)) {
64
            $view = $blockViewMappings[$blockType];
65
        }
66
67
        return $view;
68
    }
69
}
70