Passed
Pull Request — 2.x (#1421)
by Quentin
09:12
created

HasBlocks::getBlockView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 9
ccs 0
cts 0
cp 0
crap 6
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A HasBlocks::renderBlocks() 0 3 1
1
<?php
2
3
namespace A17\Twill\Models\Behaviors;
4
5
use A17\Twill\Models\Block;
6
use A17\Twill\Services\Blocks\Block as BlockConfig;
7
8
trait HasBlocks
9 21
{
10
    /**
11 21
     * Defines the one-to-many relationship for block objects.
12
     *
13
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
14 2
     */
15
    public function blocks()
16 2
    {
17 2
        return $this->morphMany(Block::class, 'blockable')->orderBy(
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

17
        return $this->/** @scrutinizer ignore-call */ morphMany(Block::class, 'blockable')->orderBy(
Loading history...
18 2
            config('twill.blocks_table', 'twill_blocks') . '.position',
19
            'asc'
20 2
        );
21
    }
22
23 2
    public function renderNamedBlocks($name = 'default', $renderChilds = true, $blockViewMappings = [], $data = [])
24
    {
25
        return $this->blocks
26 2
            ->filter(function ($block) use ($name) {
27
                return $name === 'default'
28 2
                    ? ($block->editor_name === $name || $block->editor_name === null)
29
                    : $block->editor_name === $name;
30 2
            })
31 2
            ->where('parent_id', null)
32
            ->map(function ($block) use ($blockViewMappings, $renderChilds, $data) {
33
                if ($renderChilds) {
34 2
                    $childBlocks = $this->blocks->where('parent_id', $block->id);
35
36 2
                    $renderedChildViews = $childBlocks->map(function ($childBlock) use ($blockViewMappings, $data) {
37
                        $class = BlockConfig::getForType($childBlock->type);
38 2
                        $view = $class->getBlockView($blockViewMappings);
39
                        $data = $class->getData($data, $childBlock);
40
41
                        return view($view, $data)->with('block', $childBlock)->render();
42 2
                    })->implode('');
43
                }
44
45
                $block->childs = $this->blocks->where('parent_id', $block->id);
46
47
                $class = BlockConfig::getForType($block->type);
48
                $view = $class->getBlockView($blockViewMappings);
49
                $data = $class->getData($data, $block);
50
51
                return view($view, $data)->with('block', $block)->render() . ($renderedChildViews ?? '');
52
            })->implode('');
53
    }
54
55
    /**
56
     * Returns the rendered Blade views for all attached blocks in their proper order.
57
     *
58
     * @param bool $renderChilds Include all child blocks in the rendered output.
59
     * @param array $blockViewMappings Provide alternate Blade views for blocks. Format: `['block-type' => 'view.path']`.
60
     * @param array $data Provide extra data to Blade views.
61
     * @return string
62
     */
63
    public function renderBlocks($renderChilds = true, $blockViewMappings = [], $data = [])
64
    {
65
        return $this->renderNamedBlocks('default', $renderChilds, $blockViewMappings, $data);
66
    }
67
}
68