Completed
Pull Request — master (#169)
by Mikołaj
05:25
created

BlockController::previewAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusCmsPlugin\Controller;
14
15
use BitBag\SyliusCmsPlugin\Entity\BlockInterface;
16
use FOS\RestBundle\View\View;
17
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
18
use Sylius\Component\Resource\ResourceActions;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
22
final class BlockController extends ResourceController
23
{
24
    const BLOCK_TEMPLATE = '@BitBagSyliusCmsPlugin/Shop/Block/show.html.twig';
25
26
    public function renderBlockAction(Request $request): Response
27
    {
28
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
29
30
        $this->isGrantedOr403($configuration, ResourceActions::SHOW);
31
32
        $code = $request->get('code');
33
        $blockResourceResolver = $this->get('bitbag_sylius_cms_plugin.resolver.block_resource');
34
        $block = $blockResourceResolver->findOrLog($code);
35
36
        if (null === $block) {
37
            return new Response();
38
        }
39
40
        $this->eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $block);
41
42
        $view = View::create($block);
43
        $template = $request->get('template') ?? self::BLOCK_TEMPLATE;
44
45
        if ($configuration->isHtmlRequest()) {
46
            $view
47
                ->setTemplate($template)
48
                ->setTemplateVar($this->metadata->getName())
49
                ->setData([
50
                    'configuration' => $configuration,
51
                    'metadata' => $this->metadata,
52
                    'resource' => $block,
53
                    $this->metadata->getName() => $block,
54
                ])
55
            ;
56
        }
57
58
        return $this->viewHandler->handle($configuration, $view);
59
    }
60
61
    public function previewAction(Request $request): Response
62
    {
63
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
64
65
        $this->isGrantedOr403($configuration, ResourceActions::CREATE);
66
        /** @var BlockInterface $block */
67
        $block = $this->newResourceFactory->create($configuration, $this->factory);
68
        $form = $this->resourceFormFactory->create($configuration, $block);
69
70
        $form->handleRequest($request);
71
72
        /** @var BlockInterface $block */
73
        $block = $form->getData();
74
        $defaultLocale = $this->getParameter('locale');
75
76
        $block->setFallbackLocale($request->get('_locale', $defaultLocale));
77
        $block->setCurrentLocale($request->get('_locale', $defaultLocale));
78
79
        $view = View::create()
80
            ->setData([
81
                'resource' => $block,
82
                $this->metadata->getName() => $block,
83
                'blockTemplate' => self::BLOCK_TEMPLATE,
84
            ])
85
            ->setTemplate($configuration->getTemplate(ResourceActions::CREATE . '.html'))
86
        ;
87
88
        return $this->viewHandler->handle($configuration, $view);
89
    }
90
}
91