Completed
Pull Request — master (#1121)
by Rafał
11:09
created

DefaultController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
ccs 1
cts 1
cp 1
cc 1
nc 1
nop 4
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2015 Sourcefabric z.u. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2015 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Controller;
18
19
use SWP\Bundle\ContentBundle\Factory\RouteFactoryInterface;
20
use SWP\Bundle\ContentBundle\Model\RouteInterface;
21
use SWP\Bundle\CoreBundle\Context\CachedTenantContextInterface;
22
use SWP\Bundle\CoreBundle\Model\TenantInterface;
23
use SWP\Component\TemplatesSystem\Gimme\Context\Context;
24
use SWP\Component\TemplatesSystem\Gimme\Factory\MetaFactoryInterface;
25
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
26
use Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\HttpFoundation\Response;
29
use Symfony\Component\Routing\Annotation\Route;
30
31 2
class DefaultController extends AbstractController
32
{
33
    private $tenantContext;
34 2
35 2
    private $metaFactory;
36 2
37 2
    private $templateEngineContext;
38
39 2
    private $routeFactory;
40
41
    public function __construct(
42
        CachedTenantContextInterface $tenantContext,
43
        MetaFactoryInterface $metaFactory,
44
        Context $templateEngineContext,
45
        RouteFactoryInterface $routeFactory
46
    ) {
47
        $this->tenantContext = $tenantContext;
48
        $this->metaFactory = $metaFactory;
49
        $this->templateEngineContext = $templateEngineContext;
50 2
        $this->routeFactory = $routeFactory;
51
    }
52 2
53
    /**
54
     * @Route("/", methods={"GET","POST"}, name="homepage")
55
     */
56
    public function indexAction(Request $request): Response
57
    {
58
        /** @var TenantInterface $currentTenant */
59
        $currentTenant = $this->tenantContext->getTenant();
60
        $route = $currentTenant->getHomepage();
61
62
        if (null === $route) {
63
            /** @var RouteInterface $route */
64
            $route = $this->routeFactory->create();
65
            $route->setStaticPrefix('/');
66
            $route->setName('Homepage');
67
            $route->setType('content');
68
            $route->setTemplateName('index.html.twig');
69
            $route->setCacheTimeInSeconds(360);
70
            $request->attributes->set(DynamicRouter::ROUTE_KEY, $route);
71
        }
72
73
        $this->templateEngineContext->setCurrentPage($this->metaFactory->create($route));
74
75
        $response = new Response();
76
        $response->headers->set('Content-Type', 'text/html; charset=UTF-8');
77
78
        return $this->render('index.html.twig', [], $response);
79
    }
80
}
81