Completed
Push — master ( 3ef484...ea5778 )
by Paweł
08:46
created

ArticlePreviewController::findRouteOr404()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Content Bundle.
7
 *
8
 * Copyright 2017 Sourcefabric z.ú. 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 2017 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\ContentBundle\Controller;
18
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
21
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
22
use SWP\Bundle\ContentBundle\Model\RouteInterface;
23
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
24
25
class ArticlePreviewController extends Controller
26
{
27
    /**
28
     * @Route("/preview/article/{routeId}/{slug}", options={"expose"=true}, requirements={"slug"=".+", "routeId"="\d+", "token"=".+"}, name="swp_article_preview")
29
     * @Method("GET")
30
     */
31
    public function previewAction(int $routeId, string $slug)
32
    {
33
        /** @var RouteInterface $route */
34
        $route = $this->findRouteOr404($routeId);
35
        /** @var ArticleInterface $article */
36
        $article = $this->findArticleOr404($slug);
37
38
        $metaFactory = $this->get('swp_template_engine_context.factory.meta_factory');
39
        $templateEngineContext = $this->get('swp_template_engine_context');
40
        $templateEngineContext->setCurrentPage($metaFactory->create($route));
41
        $templateEngineContext->getMetaForValue($article);
42
43
        if (null === $route->getArticlesTemplateName()) {
44
            throw $this->createNotFoundException(
45
                sprintf('Template for route with id "%d" (%s) not found!', $route->getId(), $route->getName())
46
            );
47
        }
48
49
        return $this->render($route->getArticlesTemplateName());
50
    }
51
52
    private function findRouteOr404(int $id)
53
    {
54
        if (null === ($route = $this->get('swp.repository.route')->findOneBy(['id' => $id]))) {
55
            throw $this->createNotFoundException(sprintf('Route with id: "%s" not found!', $id));
56
        }
57
58
        return $route;
59
    }
60
61
    private function findArticleOr404(string $slug)
62
    {
63
        if (null === ($article = $this->get('swp.repository.article')->findOneBy(['slug' => $slug]))) {
64
            throw $this->createNotFoundException(sprintf('Article with slug: "%s" not found!', $slug));
65
        }
66
67
        return $article;
68
    }
69
}
70