Passed
Push — 2.x ( c00759...e3c99f )
by Quentin
07:55
created

BlocksController::getChildrenBlock()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 8.1239

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 2
dl 0
loc 15
ccs 4
cts 11
cp 0.3636
crap 8.1239
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Http\Controllers\Admin;
4
5
use A17\Twill\Repositories\BlockRepository;
6
use Illuminate\Config\Repository as Config;
7
use Illuminate\Foundation\Application;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Collection;
10
use Illuminate\View\Factory as ViewFactory;
11
12
class BlocksController extends Controller
13
{
14
15
    /**
16
     * Render an HTML preview of a single block.
17
     * This is used by the full screen content editor.
18
     *
19
     * @param BlockRepository $blockRepository
20
     * @param Application $app
21
     * @param ViewFactory $viewFactory
22
     * @param Request $request
23
     * @return string
24
     */
25 1
    public function preview(
26
        BlockRepository $blockRepository,
27
        Application $app,
28
        ViewFactory $viewFactory,
29
        Request $request,
30
        Config $config
31
    ) {
32 1
        $blocksCollection = Collection::make();
33 1
        $childBlocksList = Collection::make();
0 ignored issues
show
Unused Code introduced by
The assignment to $childBlocksList is dead and can be removed.
Loading history...
34
35 1
        if ($request->has('activeLanguage')) {
36 1
            $app->setLocale($request->get('activeLanguage'));
37
        }
38
39 1
        $block = $blockRepository->buildFromCmsArray($request->except('activeLanguage'));
40
41 1
        $childBlocksList = $this->getChildrenBlock($block, $blockRepository);
42 1
        $block['blocks'] = $childBlocksList;
43 1
        $block['children'] = $childBlocksList;
44
45 1
        $newBlock = $blockRepository->createForPreview($block);
46
47 1
        $blockId = 1;
48 1
        $newBlock->id = $blockId;
49
50 1
        $blocksCollection->push($newBlock);
51
52 1
        $this->getChildrenPreview($block['blocks'], $blocksCollection, $newBlock->id, $blockId, $blockRepository);
53
54
        $renderedBlocks = $blocksCollection->where('parent_id', null)->map(function ($block) use ($blocksCollection, $viewFactory, $config) {
55 1
            if ($config->get('twill.block_editor.block_preview_render_childs') ?? true) {
56 1
                $childBlocks = $blocksCollection->where('parent_id', $block->id);
57
                $renderedChildViews = $childBlocks->map(function ($childBlock) use ($viewFactory, $config) {
58
                    $view = $this->getBlockView($childBlock->type, $config);
59
60
                    return $viewFactory->exists($view) ? $viewFactory->make($view, [
61
                        'block' => $childBlock,
62
                    ])->render() : $viewFactory->make('twill::errors.block', [
63
                        'view' => $view,
64
                    ])->render();
65 1
                })->implode('');
66
            }
67
68 1
            $block->childs = $blocksCollection->where('parent_id', $block->id);
69 1
            $block->children = $block->childs;
70
71 1
            $view = $this->getBlockView($block->type, $config);
72
73 1
            return $viewFactory->exists($view) ? ($viewFactory->make($view, [
0 ignored issues
show
Bug introduced by
Are you sure $viewFactory->make($view...' => $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

73
            return $viewFactory->exists($view) ? (/** @scrutinizer ignore-type */ $viewFactory->make($view, [
Loading history...
74 1
                'block' => $block,
75 1
            ])->render() . ($renderedChildViews ?? '')) : $viewFactory->make('twill::errors.block', [
76
                'view' => $view,
77 1
            ])->render();
78
79 1
        })->implode('');
80
81 1
        $view = $viewFactory->exists($config->get('twill.block_editor.block_single_layout'))
82 1
        ? $viewFactory->make($config->get('twill.block_editor.block_single_layout'), [
83 1
            'block' => $block,
84
        ])
85
        : $viewFactory->make('twill::errors.block_layout', [
86 1
            'view' => $config->get('twill.block_editor.block_single_layout'),
87
        ]);
88
89 1
        $viewFactory->inject('content', $renderedBlocks);
90
91 1
        return html_entity_decode($view);
92
    }
93
94 1
    protected function getChildrenBlock($block, $blockRepository)
95
    {
96 1
        $childBlocksList = Collection::make();
97 1
        foreach ($block['blocks'] as $childKey => $childBlocks) {
98
            foreach ($childBlocks as $index => $childBlock) {
99
                $childBlock = $blockRepository->buildFromCmsArray($childBlock, true);
100
                $childBlock['child_key'] = $childKey;
101
                $childBlock['position'] = $index + 1;
102
                if (!empty($childBlock['blocks'])) {
103
                    $childBlock['children'] = $this->getChildrenBlock($childBlock, $blockRepository);
104
                }
105
                $childBlocksList->push($childBlock);
106
            }
107
        }
108 1
        return $childBlocksList;
109
    }
110
111 1
    protected function getChildrenPreview($blocks, $blocksCollection, $parentId, &$blockId, $blockRepository)
112
    {
113
        $blocks->each(function ($childBlock) use (&$blockId, $parentId, $blocksCollection, $blockRepository) {
114
            $childBlock['parent_id'] = $parentId;
115
            $blockId++;
116
            $newChildBlock = $blockRepository->createForPreview($childBlock);
117
            $newChildBlock->id = $blockId;
118
            if (!empty($childBlock['children'])) {
119
                $childrenCollection = Collection::make();
120
                $this->getChildrenPreview($childBlock['children'], $childrenCollection, $newChildBlock->id, $blockId, $blockRepository);
121
                $newChildBlock->setRelation('children', $childrenCollection);
122
            }
123
            $blocksCollection->push($newChildBlock);
124 1
        });
125 1
    }
126
127
    /**
128
     * Determines a view name for a given block type.
129
     *
130
     * @param string $blockType
131
     * @param Config $config
132
     * @return string
133
     */
134 1
    private function getBlockView($blockType, $config)
135
    {
136 1
        $view = $config->get('twill.block_editor.block_views_path') . '.' . $blockType;
137
138 1
        $customViews = $config->get('twill.block_editor.block_views_mappings');
139
140 1
        if (array_key_exists($blockType, $customViews)) {
141
            $view = $customViews[$blockType];
142
        }
143
144 1
        return $view;
145
    }
146
}
147