Passed
Push — dependabot/npm_and_yarn/docs/w... ( a770a9...3a5b31 )
by
unknown
07:47
created

BlocksController::preview()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 78
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 10.8778

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 46
c 1
b 0
f 0
nc 12
nop 5
dl 0
loc 78
ccs 29
cts 45
cp 0.6444
crap 10.8778
rs 7.9337

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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();
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
        foreach ($block['blocks'] as $childKey => $childBlocks) {
42
            foreach ($childBlocks as $index => $childBlock) {
43
                $childBlock = $blockRepository->buildFromCmsArray($childBlock, true);
44
                $childBlock['child_key'] = $childKey;
45
                $childBlock['position'] = $index + 1;
46
47
                $childBlocksList->push($childBlock);
48
            }
49
        }
50
51 1
        $block['blocks'] = $childBlocksList;
52
53 1
        $newBlock = $blockRepository->createForPreview($block);
54
55 1
        $newBlock->id = 1;
56
57 1
        $blocksCollection->push($newBlock);
58
59
        $block['blocks']->each(function ($childBlock) use ($newBlock, $blocksCollection, $blockRepository) {
60
            $childBlock['parent_id'] = $newBlock->id;
61
            $newChildBlock = $blockRepository->createForPreview($childBlock);
62
            $blocksCollection->push($newChildBlock);
63 1
        });
64
65
        $renderedBlocks = $blocksCollection->where('parent_id', null)->map(function ($block) use ($blocksCollection, $viewFactory, $config) {
66 1
            if ($config->get('twill.block_editor.block_preview_render_childs') ?? true) {
67 1
                $childBlocks = $blocksCollection->where('parent_id', $block->id);
68
                $renderedChildViews = $childBlocks->map(function ($childBlock) use ($viewFactory, $config) {
69
                    $view = $this->getBlockView($childBlock->type, $config);
70
71
                    return $viewFactory->exists($view) ? $viewFactory->make($view, [
72
                        'block' => $childBlock,
73
                    ])->render() : $viewFactory->make('twill::errors.block', [
74
                        'view' => $view,
75
                    ])->render();
76 1
                })->implode('');
77
            }
78
79 1
            $block->childs = $blocksCollection->where('parent_id', $block->id);
80 1
            $block->children = $block->childs;
81
82 1
            $view = $this->getBlockView($block->type, $config);
83
84 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

84
            return $viewFactory->exists($view) ? (/** @scrutinizer ignore-type */ $viewFactory->make($view, [
Loading history...
85 1
                'block' => $block,
86 1
            ])->render() . ($renderedChildViews ?? '')) : $viewFactory->make('twill::errors.block', [
87
                'view' => $view,
88 1
            ])->render();
89
90 1
        })->implode('');
91
92 1
        $view = $viewFactory->exists($config->get('twill.block_editor.block_single_layout'))
93 1
        ? $viewFactory->make($config->get('twill.block_editor.block_single_layout'), [
94 1
            'block' => $block,
95
        ])
96
        : $viewFactory->make('twill::errors.block_layout', [
97 1
            'view' => $config->get('twill.block_editor.block_single_layout'),
98
        ]);
99
100 1
        $viewFactory->inject('content', $renderedBlocks);
101
102 1
        return html_entity_decode($view);
103
    }
104
105
    /**
106
     * Determines a view name for a given block type.
107
     *
108
     * @param string $blockType
109
     * @param Config $config
110
     * @return string
111
     */
112 1
    private function getBlockView($blockType, $config)
113
    {
114 1
        $view = $config->get('twill.block_editor.block_views_path') . '.' . $blockType;
115
116 1
        $customViews = $config->get('twill.block_editor.block_views_mappings');
117
118 1
        if (array_key_exists($blockType, $customViews)) {
119
            $view = $customViews[$blockType];
120
        }
121
122 1
        return $view;
123
    }
124
}
125