Passed
Pull Request — 2.x (#727)
by Bekzat
16:53
created

HasBlocks   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 84.21%

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 36
ccs 16
cts 19
cp 0.8421
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A blocks() 0 3 1
A getBlockView() 0 9 2
A renderBlocks() 0 18 2
1
<?php
2
3
namespace A17\Twill\Models\Behaviors;
4
5
use A17\Twill\Models\Block;
6
7
trait HasBlocks
8
{
9 21
    public function blocks()
10
    {
11 21
        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

11
        return $this->/** @scrutinizer ignore-call */ morphMany(Block::class, 'blockable')->orderBy(config('twill.blocks_table', 'twill_blocks') . '.position', 'asc');
Loading history...
12
    }
13
14 2
    public function renderBlocks($renderChilds = true, $blockViewMappings = [], $data = [])
15
    {
16 2
        return $this->blocks->where('parent_id', null)->map(function ($block) use ($blockViewMappings, $renderChilds, $data) {
17 2
            if ($renderChilds) {
18 2
                $childBlocks = $this->blocks->where('parent_id', $block->id);
19
20 2
                $renderedChildViews = $childBlocks->map(function ($childBlock) use ($blockViewMappings, $data) {
21
                    $view = $this->getBlockView($childBlock->type, $blockViewMappings);
22
                    return view($view, $data)->with('block', $childBlock)->render();
23 2
                })->implode('');
24
            }
25
26 2
            $block->childs = $this->blocks->where('parent_id', $block->id);
27
28 2
            $view = $this->getBlockView($block->type, $blockViewMappings);
29
30 2
            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

30
            return /** @scrutinizer ignore-type */ view($view, $data)->with('block', $block)->render() . ($renderedChildViews ?? '');
Loading history...
31 2
        })->implode('');
32
    }
33
34 2
    private function getBlockView($blockType, $blockViewMappings = [])
35
    {
36 2
        $view = config('twill.block_editor.block_views_path') . '.' . $blockType;
37
38 2
        if (array_key_exists($blockType, $blockViewMappings)) {
39
            $view = $blockViewMappings[$blockType];
40
        }
41
42 2
        return $view;
43
    }
44
}
45