Completed
Pull Request — master (#95)
by
unknown
01:20
created

PageController::renderLinkAction()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
c 0
b 0
f 0
rs 8.8571
cc 3
eloc 20
nc 3
nop 1
1
<?php
2
3
/**
4
 * This file was created by the 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 FOS\RestBundle\View\View;
16
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
17
use Sylius\Component\Resource\ResourceActions;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
21
/**
22
 * @author Mikołaj Król <[email protected]>
23
 */
24
final class PageController extends ResourceController
25
{
26
    /**
27
     * @param Request $request
28
     *
29
     * @return Response
30
     */
31
    public function renderLinkAction(Request $request): Response
32
    {
33
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
34
35
        $this->isGrantedOr403($configuration, ResourceActions::SHOW);
36
37
        $code = $request->get('code');
38
        $pageResourceResolver = $this->get('bitbag_sylius_cms_plugin.resolver.page_resource');
39
40
        $page = $pageResourceResolver->findOrLog($code);
41
42
        if (null === $page) {
43
            return new Response();
44
        }
45
46
        $this->eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $page);
47
48
        $view = View::create($page);
49
50
        if ($configuration->isHtmlRequest()) {
51
            $view
52
                ->setTemplate($configuration->getTemplate(ResourceActions::SHOW . '.html'))
53
                ->setTemplateVar($this->metadata->getName())
54
                ->setData([
55
                    'configuration' => $configuration,
56
                    'metadata' => $this->metadata,
57
                    'resource' => $page,
58
                    $this->metadata->getName() => $page,
59
                ])
60
            ;
61
        }
62
63
        return $this->viewHandler->handle($configuration, $view);
64
    }
65
}
66